达到用户配额限制
Hitting User Quota Limits
我目前正在使用此脚本将我所有的联系人抓取到 Google Sheet 中。理想情况下,我希望尽可能经常使用它 运行。将触发器设置为每 1 小时一次,我收到以下配额限制。
Temporary problem - please try again later and consider using batch operations. The user is over quota.
是否有更有效的方法来批处理以下脚本,以便它可以 运行 更频繁?或者可能仅当联系人已 updated/created?
function onOpen()
{
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
menuEntries.push( {name: "Read Contacts", functionName: "readContacts"} );
spreadsheet.addMenu("Contacts", menuEntries);
};
function readContacts() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Contacts");
sheet.clear();
var group = ContactsApp.getContactGroup('Apptivo Contacts');
var contacts = ContactsApp.getContactsByGroup(group);
//var contacts = ContactsApp.getContacts();
var ContactArray = new Array();
var ContactArrays = [];
ContactArray = [];
ContactArray.push("");
ContactArray.push("FullName");
ContactArray.push("Emails");
ContactArray.push("PhoneNumbers");
//ContactArray.push("HomePhone");
//ContactArray.push("WorkPhone");
ContactArray.push("Company");
ContactArray.push("Job Title");
ContactArray.push("Notes");
ContactArray.push("HomeAddress");
ContactArray.push("WorkAddress");
ContactArray.push("URL");
ContactArray.push("Groups");
//ContactArray.push("Group1");
//ContactArray.push("Group2");
ContactArrays.push(ContactArray);
for (var i=0;i<contacts.length;i++)
{
ContactArray = [];
ContactArray.push("");
ContactArray.push(contacts[i].getFullName());
//Emails
var Emails = "";
for ( var g=0;g<contacts[i].getEmails().length;g++)
{
Emails += contacts[i].getEmails()[g].getAddress();
if (g + 1 == contacts[i].getEmails().length) break
Emails += "\n";
}
try{ContactArray.push(Emails);}
catch(e){ContactArray.push("N/A")}
//Phone Numbers
var Phones = "";
for ( var g=0;g<contacts[i].getPhones().length;g++)
{
if (contacts[i].getPhones()[g].getLabel() == "MOBILE_PHONE") {
Phones += "C: "
} else if (contacts[i].getPhones()[g].getLabel() == "WORK_PHONE") {
Phones += "W: "
} else if (contacts[i].getPhones()[g].getLabel() == "HOME_PHONE") {
Phones += "H: "
} else if (contacts[i].getPhones()[g].getLabel() == "HOME_FAX") {
Phones += "F: "
} else if (contacts[i].getPhones()[g].getLabel() == "WORK_FAX") {
Phones += "F: "
} else {
Phones += "O: "
}
Phones += contacts[i].getPhones()[g].getPhoneNumber();
if (g + 1 == contacts[i].getPhones().length) break
Phones += "\n" ;
}
try{ContactArray.push(Phones);}
catch(e){ContactArray.push("N/A")}
try{ContactArray.push(contacts[i].getCompanies()[0].getCompanyName());}
catch(e){ContactArray.push("N/A")}
try{ContactArray.push(contacts[i].getCompanies()[0].getJobTitle());}
catch(e){ContactArray.push("N/A")}
ContactArray.push(contacts[i].getNotes());
//Addresses
var homeAddress = "" , workAddress = "";
for ( var g=0;g<contacts[i].getAddresses().length;g++)
{
if (contacts[i].getAddresses()[g].getLabel() == "HOME_ADDRESS") {
homeAddress += contacts[i].getAddresses()[g].getAddress();
} else if (contacts[i].getAddresses()[g].getLabel() == "WORK_ADDRESS") {
workAddress += contacts[i].getAddresses()[g].getAddress();
}
}
try{ContactArray.push(homeAddress);}
catch(e){ContactArray.push("N/A")}
try{ContactArray.push(workAddress);}
catch(e){ContactArray.push("N/A")}
//ContactArray.push(contacts[i].getAddresses().getAddress());
try{ContactArray.push(contacts[i].getUrls()[0].getAddress());}
catch(e){ContactArray.push("N/A")}
var ListofGroups = "";
for ( var g=0;g<contacts[i].getContactGroups().length;g++)
{
ListofGroups += contacts[i].getContactGroups()[g].getName();
ListofGroups += " | ";
}
try{ContactArray.push(ListofGroups);}
catch(e){ContactArray.push("N/A")}
//try{ContactArray.push(contacts[i].getContactGroups()[1].getName());}
//catch(e){ContactArray.push("N/A")}
//try{ContactArray.push(contacts[i].getContactGroups()[2].getName());}
//catch(e){ContactArray.push("N/A")}
ContactArrays.push(ContactArray);
}
sheet.getRange(1,1,ContactArrays.length,ContactArrays[0].length).setValues(ContactArrays);
};
感谢 Zaq,我减少了脚本中的 get*
调用次数——这些调用耗尽了 Google 服务配额。
脚本现在只需 运行 的一小部分时间。 (从 2 分钟到不到 20 秒)
虽然它们不影响配额,但我使用 JavaScript 数组的 map
和 join
方法来缩短一些代码。
我的最终结果...
function readContacts() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Contacts");
var group = ContactsApp.getContactGroup('Apptivo Contacts');
var contacts = ContactsApp.getContactsByGroup(group);
var ContactArray = new Array();
var ContactArrays = [];
ContactArray = [];
ContactArray.push("");
ContactArray.push("FullName");
ContactArray.push("Emails");
ContactArray.push("PhoneNumbers");
ContactArray.push("Company");
ContactArray.push("Job Title");
ContactArray.push("Notes");
ContactArray.push("HomeAddress");
ContactArray.push("WorkAddress");
ContactArray.push("URL");
ContactArray.push("Groups");
ContactArrays.push(ContactArray);
for (var i=0;i<contacts.length;i++)
{
ContactArray = [];
ContactArray.push("");
//FullName
ContactArray.push(contacts[i].getFullName());
//Emails
var Emails = contacts[i].getEmails().map(function(email) {
return email.getAddress();
}).join("\n");
try{ContactArray.push(Emails);}
catch(e){ContactArray.push("N/A")}
//Phone Numbers
var Phones = "";
var contactPhones = contacts[i].getPhones();
for ( var g=0;g<contactPhones.length;g++)
{
if (contactPhones[g].getLabel() == "MOBILE_PHONE") {
Phones += "C: "
} else if (contactPhones[g].getLabel() == "WORK_PHONE") {
Phones += "W: "
} else if (contactPhones[g].getLabel() == "HOME_PHONE") {
Phones += "H: "
} else if (contactPhones[g].getLabel() == "HOME_FAX") {
Phones += "F: "
} else if (contactPhones[g].getLabel() == "WORK_FAX") {
Phones += "F: "
} else {
Phones += "O: "
}
Phones += contactPhones[g].getPhoneNumber();
if (g + 1 == contactPhones.length) break
Phones += "\n" ;
}
try{ContactArray.push(Phones);}
catch(e){ContactArray.push("N/A")}
//Company
var contactCompany = contacts[i].getCompanies();
try{ContactArray.push(contactCompany[0].getCompanyName());}
catch(e){ContactArray.push("N/A")}
//JobTitle
try{ContactArray.push(contactCompany[0].getJobTitle());}
catch(e){ContactArray.push("N/A")}
//Notes
ContactArray.push(contacts[i].getNotes());
//Addresses
var homeAddress = "" , workAddress = "";
var contactAddresses = contacts[i].getAddresses();
for ( var g=0;g<contactAddresses.length;g++)
{
if (contactAddresses[g].getLabel() == "HOME_ADDRESS") {
homeAddress += contactAddresses[g].getAddress();
} else if (contactAddresses[g].getLabel() == "WORK_ADDRESS") {
workAddress += contactAddresses[g].getAddress();
}
}
//Home
try{ContactArray.push(homeAddress);}
catch(e){ContactArray.push("N/A")}
//Work
try{ContactArray.push(workAddress);}
catch(e){ContactArray.push("N/A")}
//URLs
try{ContactArray.push(contacts[i].getUrls()[0].getAddress());}
catch(e){ContactArray.push("N/A")}
//Groups
var Groups = contacts[i].getContactGroups().map(function(group) {
return group.getName();
}).join(" | ");
try{ContactArray.push(Groups);}
catch(e){ContactArray.push("N/A")}
ContactArrays.push(ContactArray);
}
//If Array is not blank(to avoid quota issues)
if (12 < ContactArrays.length) {
// Re-populate sheet
sheet.clear();
sheet.getRange(1,1,ContactArrays.length,ContactArrays[0].length).setValues(ContactArrays);
}
};
我目前正在使用此脚本将我所有的联系人抓取到 Google Sheet 中。理想情况下,我希望尽可能经常使用它 运行。将触发器设置为每 1 小时一次,我收到以下配额限制。
Temporary problem - please try again later and consider using batch operations. The user is over quota.
是否有更有效的方法来批处理以下脚本,以便它可以 运行 更频繁?或者可能仅当联系人已 updated/created?
function onOpen()
{
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
menuEntries.push( {name: "Read Contacts", functionName: "readContacts"} );
spreadsheet.addMenu("Contacts", menuEntries);
};
function readContacts() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Contacts");
sheet.clear();
var group = ContactsApp.getContactGroup('Apptivo Contacts');
var contacts = ContactsApp.getContactsByGroup(group);
//var contacts = ContactsApp.getContacts();
var ContactArray = new Array();
var ContactArrays = [];
ContactArray = [];
ContactArray.push("");
ContactArray.push("FullName");
ContactArray.push("Emails");
ContactArray.push("PhoneNumbers");
//ContactArray.push("HomePhone");
//ContactArray.push("WorkPhone");
ContactArray.push("Company");
ContactArray.push("Job Title");
ContactArray.push("Notes");
ContactArray.push("HomeAddress");
ContactArray.push("WorkAddress");
ContactArray.push("URL");
ContactArray.push("Groups");
//ContactArray.push("Group1");
//ContactArray.push("Group2");
ContactArrays.push(ContactArray);
for (var i=0;i<contacts.length;i++)
{
ContactArray = [];
ContactArray.push("");
ContactArray.push(contacts[i].getFullName());
//Emails
var Emails = "";
for ( var g=0;g<contacts[i].getEmails().length;g++)
{
Emails += contacts[i].getEmails()[g].getAddress();
if (g + 1 == contacts[i].getEmails().length) break
Emails += "\n";
}
try{ContactArray.push(Emails);}
catch(e){ContactArray.push("N/A")}
//Phone Numbers
var Phones = "";
for ( var g=0;g<contacts[i].getPhones().length;g++)
{
if (contacts[i].getPhones()[g].getLabel() == "MOBILE_PHONE") {
Phones += "C: "
} else if (contacts[i].getPhones()[g].getLabel() == "WORK_PHONE") {
Phones += "W: "
} else if (contacts[i].getPhones()[g].getLabel() == "HOME_PHONE") {
Phones += "H: "
} else if (contacts[i].getPhones()[g].getLabel() == "HOME_FAX") {
Phones += "F: "
} else if (contacts[i].getPhones()[g].getLabel() == "WORK_FAX") {
Phones += "F: "
} else {
Phones += "O: "
}
Phones += contacts[i].getPhones()[g].getPhoneNumber();
if (g + 1 == contacts[i].getPhones().length) break
Phones += "\n" ;
}
try{ContactArray.push(Phones);}
catch(e){ContactArray.push("N/A")}
try{ContactArray.push(contacts[i].getCompanies()[0].getCompanyName());}
catch(e){ContactArray.push("N/A")}
try{ContactArray.push(contacts[i].getCompanies()[0].getJobTitle());}
catch(e){ContactArray.push("N/A")}
ContactArray.push(contacts[i].getNotes());
//Addresses
var homeAddress = "" , workAddress = "";
for ( var g=0;g<contacts[i].getAddresses().length;g++)
{
if (contacts[i].getAddresses()[g].getLabel() == "HOME_ADDRESS") {
homeAddress += contacts[i].getAddresses()[g].getAddress();
} else if (contacts[i].getAddresses()[g].getLabel() == "WORK_ADDRESS") {
workAddress += contacts[i].getAddresses()[g].getAddress();
}
}
try{ContactArray.push(homeAddress);}
catch(e){ContactArray.push("N/A")}
try{ContactArray.push(workAddress);}
catch(e){ContactArray.push("N/A")}
//ContactArray.push(contacts[i].getAddresses().getAddress());
try{ContactArray.push(contacts[i].getUrls()[0].getAddress());}
catch(e){ContactArray.push("N/A")}
var ListofGroups = "";
for ( var g=0;g<contacts[i].getContactGroups().length;g++)
{
ListofGroups += contacts[i].getContactGroups()[g].getName();
ListofGroups += " | ";
}
try{ContactArray.push(ListofGroups);}
catch(e){ContactArray.push("N/A")}
//try{ContactArray.push(contacts[i].getContactGroups()[1].getName());}
//catch(e){ContactArray.push("N/A")}
//try{ContactArray.push(contacts[i].getContactGroups()[2].getName());}
//catch(e){ContactArray.push("N/A")}
ContactArrays.push(ContactArray);
}
sheet.getRange(1,1,ContactArrays.length,ContactArrays[0].length).setValues(ContactArrays);
};
感谢 Zaq,我减少了脚本中的 get*
调用次数——这些调用耗尽了 Google 服务配额。
脚本现在只需 运行 的一小部分时间。 (从 2 分钟到不到 20 秒)
虽然它们不影响配额,但我使用 JavaScript 数组的 map
和 join
方法来缩短一些代码。
我的最终结果...
function readContacts() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Contacts");
var group = ContactsApp.getContactGroup('Apptivo Contacts');
var contacts = ContactsApp.getContactsByGroup(group);
var ContactArray = new Array();
var ContactArrays = [];
ContactArray = [];
ContactArray.push("");
ContactArray.push("FullName");
ContactArray.push("Emails");
ContactArray.push("PhoneNumbers");
ContactArray.push("Company");
ContactArray.push("Job Title");
ContactArray.push("Notes");
ContactArray.push("HomeAddress");
ContactArray.push("WorkAddress");
ContactArray.push("URL");
ContactArray.push("Groups");
ContactArrays.push(ContactArray);
for (var i=0;i<contacts.length;i++)
{
ContactArray = [];
ContactArray.push("");
//FullName
ContactArray.push(contacts[i].getFullName());
//Emails
var Emails = contacts[i].getEmails().map(function(email) {
return email.getAddress();
}).join("\n");
try{ContactArray.push(Emails);}
catch(e){ContactArray.push("N/A")}
//Phone Numbers
var Phones = "";
var contactPhones = contacts[i].getPhones();
for ( var g=0;g<contactPhones.length;g++)
{
if (contactPhones[g].getLabel() == "MOBILE_PHONE") {
Phones += "C: "
} else if (contactPhones[g].getLabel() == "WORK_PHONE") {
Phones += "W: "
} else if (contactPhones[g].getLabel() == "HOME_PHONE") {
Phones += "H: "
} else if (contactPhones[g].getLabel() == "HOME_FAX") {
Phones += "F: "
} else if (contactPhones[g].getLabel() == "WORK_FAX") {
Phones += "F: "
} else {
Phones += "O: "
}
Phones += contactPhones[g].getPhoneNumber();
if (g + 1 == contactPhones.length) break
Phones += "\n" ;
}
try{ContactArray.push(Phones);}
catch(e){ContactArray.push("N/A")}
//Company
var contactCompany = contacts[i].getCompanies();
try{ContactArray.push(contactCompany[0].getCompanyName());}
catch(e){ContactArray.push("N/A")}
//JobTitle
try{ContactArray.push(contactCompany[0].getJobTitle());}
catch(e){ContactArray.push("N/A")}
//Notes
ContactArray.push(contacts[i].getNotes());
//Addresses
var homeAddress = "" , workAddress = "";
var contactAddresses = contacts[i].getAddresses();
for ( var g=0;g<contactAddresses.length;g++)
{
if (contactAddresses[g].getLabel() == "HOME_ADDRESS") {
homeAddress += contactAddresses[g].getAddress();
} else if (contactAddresses[g].getLabel() == "WORK_ADDRESS") {
workAddress += contactAddresses[g].getAddress();
}
}
//Home
try{ContactArray.push(homeAddress);}
catch(e){ContactArray.push("N/A")}
//Work
try{ContactArray.push(workAddress);}
catch(e){ContactArray.push("N/A")}
//URLs
try{ContactArray.push(contacts[i].getUrls()[0].getAddress());}
catch(e){ContactArray.push("N/A")}
//Groups
var Groups = contacts[i].getContactGroups().map(function(group) {
return group.getName();
}).join(" | ");
try{ContactArray.push(Groups);}
catch(e){ContactArray.push("N/A")}
ContactArrays.push(ContactArray);
}
//If Array is not blank(to avoid quota issues)
if (12 < ContactArrays.length) {
// Re-populate sheet
sheet.clear();
sheet.getRange(1,1,ContactArrays.length,ContactArrays[0].length).setValues(ContactArrays);
}
};