如何从 contacts.google.com 中提取联系人的 URL
How to extract contact's URL from contacts.google.com
以下脚本帮助我获取 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
?
我还需要获取每个联系人的标签(或组)。请帮忙。
我建议您解析 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()
或 getId()
。
参考资料
好的。在 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);
}
以下脚本帮助我获取 https://contacts.google.com.
中每个联系人的姓名和 URLfunction 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
?
我还需要获取每个联系人的标签(或组)。请帮忙。
我建议您解析 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()
或 getId()
。
参考资料
好的。在 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);
}