首先,我知道,有一个类似的问题,但它只是相似而不是相同:)
此脚本在电子表格中导入 Google Contacts -> Contacts Sevices。
function onOpen()
{
var menuItems = [{name: "Gmail Contact Import", functionName: "getContacts"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("import", menuItems);
}
function getContacts()
{
var groupName = "MyGroup";
var contacts;
var sheet = SpreadsheetApp.getActiveSheet();
if (groupName) {
var contactGroup = ContactsApp.findContactGroup(groupName);
if (contactGroup) {
contacts = contactGroup.getContacts();
}
}
else {
contacts = ContactsApp.getAllContacts();
}
if (!contacts) {
Browser.msgBox("no contacts found!");
return;
}
sheet.clear();
sheet.getRange("A1").setValue("forename");
sheet.getRange("B1").setValue("lastname");
sheet.getRange("C1").setValue("private email");
sheet.getRange("D1").setValue(groupName + " email");
sheet.getRange("E1").setValue("phone");
sheet.getRange("F1").setValue("mobile");
sheet.getRange("G1").setValue("street");
sheet.getRange("H1").setValue("zip");
sheet.getRange("I1").setValue("city");
sheet.getRange("J1").setValue("group");
sheet.getRange("K1").setValue("date1");
sheet.getRange("L1").setValue("date2");
sheet.getRange("M1").setValue("date3");
sheet.getRange("N1").setValue("date4");
for ( var i = 0; i < contacts.length; i++) {
var addressArray = new Array();
addressArray = contacts[i].getAddresses(ContactsApp.Field.HOME_ADDRESS);
var addressArrayLength = addressArray.length;
for ( var ii = 0; ii < addressArrayLength; ii++ ) {
var address = addressArray[ii].getAddress();
var addressSplit = address.split(/\r\n|\r|\n/);
var street = addressSplit[0];
var city = addressSplit[1].split(" ")[0];
var zip = addressSplit[1].split(" ")[1];
}
var homePhone = contacts[i].getHomePhone().replace("0049-","0").toString();
var mobilePhone = contacts[i].getMobilePhone().replace("0049-","0").toString();
var fireMail = ""
var privateMail = "";
var mailArray = new Array();
mailArray = contacts[i].getEmails(ContactsApp.Field.HOME_EMAIL);
var mailArrayLength = mailArray.length;
for ( var ii = 0; ii < mailArrayLength; ii++ ) {
privateMail = contacts[i].getEmails(ContactsApp.Field.HOME_EMAIL)[ii].getAddress();
}
if (!privateMail)
{
mailArray = contacts[i].getEmails(ContactsApp.Field.WORK_EMAIL);
mailArrayLength = mailArray.length;
for ( var ii = 0; ii < mailArrayLength; ii++ ) {
privateMail = contacts[i].getEmails(ContactsApp.Field.WORK_EMAIL)[ii].getAddress();
}
}
mailArray = contacts[i].getEmails(groupName);
mailArrayLength = mailArray.length;
for ( var ii = 0; ii < mailArrayLength; ii++ ) {
fireMail = contacts[i].getEmails(groupName)[ii].getAddress();
}
sheet.getRange(1*i+2, 1, 1, 1).setValue(contacts[i].getGivenName());
sheet.getRange(1*i+2, 2, 1, 1).setValue(contacts[i].getFamilyName());
sheet.getRange(1*i+2, 3, 1, 1).setValue(privateMail);
sheet.getRange(1*i+2, 4, 1, 1).setValue(fireMail);
sheet.getRange(1*i+2, 5, 1, 1).setValue(homePhone);
sheet.getRange(1*i+2, 6, 1, 1).setValue(mobilePhone);
sheet.getRange(1*i+2, 7, 1, 1).setValue(street);
sheet.getRange(1*i+2, 8, 1, 1).setValue(zip);
sheet.getRange(1*i+2, 9, 1, 1).setValue(city);
var groups = contacts[i].getContactGroups();
for ( var j = 0; j < groups.length; j++) {
var name = groups[j].getName();
switch (name) {
case "group1":
sheet.getRange(1*i+2, 10, 1, 1).setValue(1);
break;
case "group2":
sheet.getRange(1*i+2, 10, 1, 1).setValue(2);
break;
case "group3":
sheet.getRange(1*i+2, 10, 1, 1).setValue(3);
break;
case "group4":
sheet.getRange(1*i+2, 10, 1, 1).setValue(4);
break;
case "group5":
sheet.getRange(1*i+2, 10, 1, 1).setValue(5);
break;
default:
break;
}
if (name.indexOf("datetrigger1") > -1)
{
var dateArray = contacts[i].getDates("date1");
var dateArrayLength = dateArray.length;
for ( var ii = 0; ii < dateArrayLength; ii++ ) {
var date = contacts[i].getDates("date1")[ii];
var day = date.getDay();
var month = date.getMonth();
var year = date.getYear();
sheet.getRange(1*i+2, 11, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
}
sheet.getRange(1*i+2, 11, 1, 1).setBackgroundColor('green');
}
if (name.indexOf("datetrigger2") > -1)
{
var dateArray = contacts[i].getDates("date2");
var dateArrayLength = dateArray.length;
for ( var ii = 0; ii < dateArrayLength; ii++ ) {
var date = contacts[i].getDates("date2")[ii];
var day = date.getDay();
var month = date.getMonth();
var year = date.getYear();
sheet.getRange(1*i+2, 12, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
}
sheet.getRange(1*i+2, 12, 1, 1).setBackgroundColor('green');
}
if (name.indexOf("datetrigger3") > -1)
{
var dateArray = contacts[i].getDates("date3");
var dateArrayLength = dateArray.length;
for ( var ii = 0; ii < dateArrayLength; ii++ ) {
var date = contacts[i].getDates("date3")[ii];
var day = date.getDay();
var month = date.getMonth();
var year = date.getYear();
sheet.getRange(1*i+2, 13, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
}
sheet.getRange(1*i+2, 13, 1, 1).setBackgroundColor('green');
}
if (name.indexOf("datetrigger4") > -1)
{
var dateArray = contacts[i].getDates("date4");
var dateArrayLength = dateArray.length;
if (dateArrayLength > 0) {
for ( var ii = 0; ii < dateArrayLength; ii++ ) {
var date = contacts[i].getDates("date4")[ii];
var day = date.getDay();
var month = date.getMonth();
var year = date.getYear();
sheet.getRange(1*i+2, 14, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
}
sheet.getRange(1*i+2, 14, 1, 1).setBackgroundColor('green');
}
}
}
}
Browser.msgBox(contacts.length + " contacts imported.");
}
工作正常,但在“导入 xy 联系人”消息后,我收到以下错误消息:
序列化延续时出现意外异常
我无法弄清楚出了什么问题。
2012 年 7 月 26 日更新:
我还有一个错误:
Dienst-Fehler:ContactsApp:临时问题 - 请稍后再试并考虑使用批处理操作。用户超出配额。(泽勒96)
这是关于这一行的:var groups = contacts[i].getContactGroups();
我已经稍后再试了......错误仍然存在。我总是在我的列表中得到结果。该错误发生在循环的各种迭代之后。
2012 年 7 月 27 日更新:
我Utilities.sleep(2000);
在之后添加var groups = contacts[i].getContactGroups();
。这已经足够了。没有那么多联系方式...