如何从contacts.google.com提取联系人的URL

时间:2018-11-04 03:02:38

标签: google-apps-script google-contacts

以下脚本可帮助我获取https://contacts.google.com中每个联系人的姓名和URL。

function extract_contacts() {
  var s = '';
  var contacts = ContactsApp.getContacts();
    for (var i in contacts) {
      var name = contacts[i].getFullName();
      var id = contacts[i].getId();
      var url = contacts[i].getUrls();
      s+=name+" ·· "+url+"\r";
    }
  Logger.log(s);
}

问题是getUrls结果显示UrlField,而不是该字段中保存的实际URL,如下所示:

friend ·· UrlField
another_friend ·· UrlField,UrlField

我应该用什么代替getUrls? 我还需要获取每个联系人的标签(或组)。请帮忙。

2 个答案:

答案 0 :(得分:2)

我建议您解析class Program { /* I assume you are trying to 1. Create an array of integers 2. Store random numbers (between 0 and 100) inside that array 3. Print the numbers in the array You have alot of reading to do as theres alot of fundemental mistakes in both your approach and code. */ static void Main(string[] args) { // creating an array with random numbers ArrayMethods m = new ArrayMethods(); int[] nums1; nums1 = m.CreateRandomlyFilledArray(10); m.Printarray(nums1); } class ArrayMethods { /* - First you have to fill the array with random numbers In your solution, you have created "CreateRandomlyFilledArray". 1. You created the a new array of integers which is good 2. The way you attempted to fill the new array is incorrect */ public int[] CreateRandomlyFilledArray(int size) { int[] newNums = new int[size]; Random numGen = new Random(); // This will be used to generate random numbers for (int elementNum = 0; elementNum < newNums.Length; elementNum++) { // here we will put a random number in every position of the array using the random number generator newNums[elementNum] = numGen.Next(0, 100); // we pass in you minimum and maximum into the next function and it will return a random number between them } // here we will return the array with the random numbers return newNums; } /* - This function prints out each item in an integer array 1. You do not need to a return value as you will not be returning any thing so, Use "void". */ public void Printarray(int[] value) { for (int i = 0; i < value.Length; i++) { Console.WriteLine("value is: {0}", value[i]); } } } } 数组并按以下方式获取地址:

URLField

对于组来说,相同的内容使用function extract_contacts() { var s = ''; var contacts = ContactsApp.getContacts(); for (var i in contacts) { var adresses =""; var name = contacts[i].getFullName(); var id = contacts[i].getId(); var urlFields = contacts[i].getUrls(); for (var j = 0; j < urlFields.length; j++) adresses += urlFields[j].getAddress() + " "; s+=name+" ·· "+ adresses +"\r"; } Logger.log(s); } 并通过数组进行解析,然后使用getContactGroups()getName()

参考

URLField Class

contactGroup Class

答案 1 :(得分:1)

好。在JSmith的帮助下,并添加了一些替换和字母排序之后,就是这样。最后,该脚本将结果打印到“ Google协作平台”页面:

function extractContacts() {
  var s = '';
  var contacts = ContactsApp.getContacts();
    for (var i in contacts) {

      var adresses = "";
      var groups = "";
      var name = contacts[i].getFullName();
      var id = contacts[i].getId();

      var urlFields = contacts[i].getUrls();
      for (var j = 0; j < urlFields.length; j++)
        adresses += '* <a href="'+urlFields[j].getAddress()+'" target="_blank">'+urlFields[j].getAddress()+'</a><br>';

      var group = contacts[i].getContactGroups();
      for (var k = 0; k < group.length; k++)
        groups += group[k].getName() + " · ";

      if(adresses==''){var groups=''; var name='';}
      s+='<h3>'+groups+' · '+name+'</h3><blockquote>'+adresses+"</blockquote>#####";
    }

var s = s.replace(/System Group: My Contacts · /g,"");
var s = s.replace(/,/g,"&&&&&");

var aorder = s.split("#####").sort(function (a, b) {
  return a.localeCompare(b);
});
var s = aorder.toString();
var s = s.replace(/,/g,'<br>');
var s = s.replace(/&&&&&/g,',');
var s = s.replace(/·  ·/g,':');
var s = s.replace(/<br><h3> · <\/h3>/g,'');

var site = SitesApp.getSite('site','clicamiento');
var home = site.getChildByName('/home');
home.setHtmlContent('<h1>CLICAMIENTO</h1>'+s);

}
相关问题