首先,我知道,有一個similar question但它只是相似,不一樣:)谷歌Apps腳本與我們聯繫服務錯誤
這個腳本導入谷歌聯繫人 - >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接觸」後,我收到此錯誤信息:
Unexpected exception upon serializing continuation
我無法弄清楚什麼是錯的。
更新2012-07-26:
我有一個額外的錯誤:
Dienst-Fehler: ContactsApp: Temporary problem - please try again later and consider using batch operations. The user is over quota. (Zeile 96)
這是關於該行:var groups = contacts[i].getContactGroups();
我已經試了一次後...錯誤仍然來臨。總是我在我的列表中得到結果。該錯誤出現在循環的各種迭代之後。
更新2012-07-27:
我var groups = contacts[i].getContactGroups();
後添加Utilities.sleep(2000);
。這已經足夠了。有沒有這麼多的聯繫...
由於此問題與Google Apps腳本有關,並且與App Engine無關,因此我編輯了標籤。 – 2012-07-24 15:47:19
@Adam Crossland謝謝 – Sebastian 2012-07-25 06:50:03