2017-01-02 81 views
1

我目前正在使用此腳本將所有聯繫人抓取到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); 
}; 
+2

多久你的腳本需要執行,是這個A G套房或消費者帳戶(帳戶的消費者是那些以@ gmail.com結尾)。消費者賬戶每天的總觸發執行時間爲1小時,這意味着如果您希望每30分鐘觸發一次,腳本的平均耗時不會超過75秒。由於您只有1次從Google通訊錄讀取數據,並且有1次寫入電子表格,因此您已正確處理所有內容。 – Vytautas

+0

這是一個G Suite帳戶。它需要大約2分鐘來運行腳本..但我不記得它採取了很久以前。也許是因爲我現在有更多的聯繫方式... –

+0

無論哪種方式,我應該有6小時的執行時間,應該是綽綽有餘。我是否在提取聯繫信息時使用了不同的配額? –

回答

1

感謝Zaq,我減少了get*呼叫的數量在我的腳本 - 這些電話是什麼抽乾了谷歌服務的配額。

腳本現在需要一小部分時間來運行。 (從2分鐘到20秒)

儘管它們不影響配額,但我使用了mapjoin的JavaScript數組方法來縮短一些代碼。

我的最終結果...

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

};