达到用户配额限制

时间:2017-01-02 03:11:36

标签: google-apps-script quota

我目前正在使用此脚本将我的所有联系人抓取到Google表格中。理想情况下,我希望尽可能经常运行。如果触发器设置为每1小时,我会收到以下配额限制。

  

临时问题 - 请稍后再试,并考虑使用批处理操作。用户超过配额。

是否有更有效的方法批处理以下脚本以便它可以更频繁地运行?或者可能只有在更新/创建联系人时?

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);
};

1 个答案:

答案 0 :(得分:1)

感谢Zaq,我减少了脚本中get*次来电的数量 - 这些来电耗尽了Google服务配额。

脚本现在只需要一小部分时间来运行。 (从2分钟到20秒不到)

虽然它们不会影响配额,但我使用JavaScript数组的mapjoin方法来缩短某些代码。

我的最终结果......

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);
   }

};