Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 如何改进此代码以减少调用GoogleAPI的次数_Google Apps Script_Google Sheets_Google Contacts Api - Fatal编程技术网

Google apps script 如何改进此代码以减少调用GoogleAPI的次数

Google apps script 如何改进此代码以减少调用GoogleAPI的次数,google-apps-script,google-sheets,google-contacts-api,Google Apps Script,Google Sheets,Google Contacts Api,我必须将google联系人与电子表格同步,为此我必须调用API,因此可能需要时间在google电子表格中设置值,我如何克服这个问题,以减少调用google API的次数或仅调用一次 var preContacts = group.getContacts(); var rowNo = 2; for(var y = 0; y < preContacts.length; y++) { var FNameContact = preContacts[y].getGivenName

我必须将google联系人与电子表格同步,为此我必须调用API,因此可能需要时间在google电子表格中设置值,我如何克服这个问题,以减少调用google API的次数或仅调用一次

var preContacts = group.getContacts();
  var rowNo = 2;

  for(var y = 0; y < preContacts.length; y++) {

    var FNameContact = preContacts[y].getGivenName();
    var PhoneNoArray = preContacts[y].getPhones();
    var PhoneNoContact = getPhoneNumbers(PhoneNoArray);
    var emailArray = preContacts[y].getEmails();
    var emailAdressContact = getEmailAddresses(emailArray);
    //var customFieldsArray = preContacts.getCustomFields();

    sheettab_bulk_cdb.getRange("J"+rowNo).setValue(emailAdressContact);
    sheettab_bulk_cdb.getRange("G"+rowNo).setValue(emailAdressContact);
    sheettab_bulk_cdb.getRange("F"+rowNo).setValue(emailAdressContact);
    sheettab_bulk_cdb.getRange("A"+rowNo).setValue(emailAdressContact);

  rowNo++;
  }

对您来说,最重要的优化是使用单个setValues调用将数据写入电子表格,而不是使用当前的许多单单元格setValues操作。详细阅读对的答案

不过,为了利用setValues,您必须重新安排电子表格,以便您正在编写的联系人信息是连续的。考虑这样一个电子表格:

通过这种设置,我们可以将所有联系人信息收集到一个二维数组中,然后像这样拨打一个电话:

sheet.getRange(_row_,_col_,data.length,data[0].length).setValues(data);
。。。其中_row_uu,_col_u是我们要写入数据的范围的左上角

剧本 下面是一个完整的脚本,它将检索组的所有联系人,并填充该示例电子表格。它可以很容易地扩展到包括其他联系信息

/**
 * Retrieves all contacts for a group. If groupName isn't passed as
 * a parameter, user will be prompted.
 */
function getContactGroup(groupName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  groupName = groupName || Browser.inputBox('Get Contact Group', 'Group name:', Browser.Buttons.OK_CANCEL);
  if (groupName === 'cancel') {
    ss.toast('Task canceled', 'Status', 3);
    return;
  }

  var group = ContactsApp.getContactGroup(groupName);
  if (!group) {
    ss.toast('Group not found', 'Status', 3);
    return;
  }
  else {
    var data = []; // Array to hold contact info for spreadsheet
    var contacts = group.getContacts();
    if (contacts.length == 0) {
      ss.toast('No contacts in group', 'Status', 3);
      return;
    }
    else {
      for (var i=0; i<contacts.length; i++) {
        var contactInfo = []
        // Build a row of contact information
        contactInfo.push( contacts[i].getFullName() );
        contactInfo.push( getPhoneNumbers(contacts[i].getPhones()) );
        contactInfo.push( getEmailAddresses( contacts[i].getEmails()) );
        contactInfo.push( contacts[i].getLastUpdated() );

        // Add row to data array
        data.push( contactInfo );
      }
    }
  }

  // Output all contact info to spreadsheet in one shot.
  var sheet = ss.getActiveSheet();
  var headers = 1; // # rows containing headers
  sheet.getDataRange().offset(headers, 0).clearContent();   // Clear old content
  sheet.getRange(1+headers,1,data.length,data[0].length).setValues(data);
};

function getPhoneNumbers( phones ) {
  // Convert array of phones to comma-separated string
  var phoneList = [];
  for (var phone=0; phone < phones.length; phone++) {
    var phoneEntry = phones[phone].getLabel() + ' ' + phones[phone].getPhoneNumber();
    if (phones[phone].isPrimary()) {
      // Add primary phone at front
      phoneList.unshift(phoneEntry);
    }
    else {
      // Add other phones at end
      phoneList.push(phoneEntry);
    }
  }
  return phoneList.join(', ');
}

function getEmailAddresses( emails ) {
  // Convert array of emails to comma-separated string
  var emailList = [];
  for (var email=0; email < emails.length; email++) {
    var emailEntry = emails[email].getLabel() + ' ' + emails[email].getAddress();
    if (emails[email].isPrimary()) {
      // Add primary email at front
      emailList.unshift(emailEntry);
    }
    else {
      // Add other emails at end
      emailList.push(emailEntry);
    }
  }
  return emailList.join(', ');
}

/**
 * Adds a custom menu to the active spreadsheet
 */
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Copy contact group to sheet",
    functionName : "getContactGroup"
  }];
  sheet.addMenu("Script Center Menu", entries);
};
脚本版本2 这不像以前的版本那样高效,因为每一列数据都是单独编写的。它也将更难维护和扩展,因为它有更多的代码。仍然很简单,但更多

对于上面显示的示例表,列编号是正确的,因此需要针对您自己的电子表格进行调整

/**
 * Retrieves all contacts for a group. If groupName isn't passed as
 * a parameter, user will be prompted.
 *
 * Version 2: This version allows the columns of contact info to be
 * non-contiguous, but is less efficient than version 1.
 */
function getContactGroup2(groupName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  groupName = groupName || Browser.inputBox('Get Contact Group', 'Group name:', Browser.Buttons.OK_CANCEL);
  if (groupName === 'cancel') {
    ss.toast('Task canceled', 'Status', 3);
    return;
  }

  var group = ContactsApp.getContactGroup(groupName);
  if (!group) {
    ss.toast('Group not found', 'Status', 3);
    return;
  }
  else {
    var contactName = []; // Arrays to hold contact info for spreadsheet
    var contactPhone = [];
    var contactEmail = [];
    var contactLastUpdated = [];

    var contacts = group.getContacts();
    if (contacts.length == 0) {
      ss.toast('No contacts in group', 'Status', 3);
      return;
    }
    else {
      ss.toast('Starting Copy', 'Status', 3);

      for (var i=0; i<contacts.length; i++) {
        // Record contact information in column arrays
        contactName.push( [contacts[i].getFullName()] );
        contactPhone.push( [getPhoneNumbers(contacts[i].getPhones())] );
        contactEmail.push( [getEmailAddresses( contacts[i].getEmails())] );
        contactLastUpdated.push( [contacts[i].getLastUpdated()] );
      }
    }
  }

  ss.toast('Storing to sheet', 'Status', 3);

  // Output all contact info to spreadsheet in one shot per column.
  var sheet = ss.getActiveSheet();
  var headers = 1; // # rows containing headers

  var columnName = 1,  // Column numbers to receive contact info
      columnPhone = 2,
      columnEmail = 3,
      columnLastUpdated = 4;
  sheet.getRange(1+headers,columnName,contactName.length,1).setValues(contactName);
  sheet.getRange(1+headers,columnPhone,contactPhone.length,1).setValues(contactPhone);
  sheet.getRange(1+headers,columnEmail,contactEmail.length,1).setValues(contactEmail);
  sheet.getRange(1+headers,columnLastUpdated,contactLastUpdated.length,1).setValues(contactLastUpdated);

  ss.toast('Operation complete', 'Status', 3);
};

对您来说,最重要的优化是使用单个setValues调用将数据写入电子表格,而不是使用当前的许多单单元格setValues操作。详细阅读对的答案

不过,为了利用setValues,您必须重新安排电子表格,以便您正在编写的联系人信息是连续的。考虑这样一个电子表格:

通过这种设置,我们可以将所有联系人信息收集到一个二维数组中,然后像这样拨打一个电话:

sheet.getRange(_row_,_col_,data.length,data[0].length).setValues(data);
。。。其中_row_uu,_col_u是我们要写入数据的范围的左上角

剧本 下面是一个完整的脚本,它将检索组的所有联系人,并填充该示例电子表格。它可以很容易地扩展到包括其他联系信息

/**
 * Retrieves all contacts for a group. If groupName isn't passed as
 * a parameter, user will be prompted.
 */
function getContactGroup(groupName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  groupName = groupName || Browser.inputBox('Get Contact Group', 'Group name:', Browser.Buttons.OK_CANCEL);
  if (groupName === 'cancel') {
    ss.toast('Task canceled', 'Status', 3);
    return;
  }

  var group = ContactsApp.getContactGroup(groupName);
  if (!group) {
    ss.toast('Group not found', 'Status', 3);
    return;
  }
  else {
    var data = []; // Array to hold contact info for spreadsheet
    var contacts = group.getContacts();
    if (contacts.length == 0) {
      ss.toast('No contacts in group', 'Status', 3);
      return;
    }
    else {
      for (var i=0; i<contacts.length; i++) {
        var contactInfo = []
        // Build a row of contact information
        contactInfo.push( contacts[i].getFullName() );
        contactInfo.push( getPhoneNumbers(contacts[i].getPhones()) );
        contactInfo.push( getEmailAddresses( contacts[i].getEmails()) );
        contactInfo.push( contacts[i].getLastUpdated() );

        // Add row to data array
        data.push( contactInfo );
      }
    }
  }

  // Output all contact info to spreadsheet in one shot.
  var sheet = ss.getActiveSheet();
  var headers = 1; // # rows containing headers
  sheet.getDataRange().offset(headers, 0).clearContent();   // Clear old content
  sheet.getRange(1+headers,1,data.length,data[0].length).setValues(data);
};

function getPhoneNumbers( phones ) {
  // Convert array of phones to comma-separated string
  var phoneList = [];
  for (var phone=0; phone < phones.length; phone++) {
    var phoneEntry = phones[phone].getLabel() + ' ' + phones[phone].getPhoneNumber();
    if (phones[phone].isPrimary()) {
      // Add primary phone at front
      phoneList.unshift(phoneEntry);
    }
    else {
      // Add other phones at end
      phoneList.push(phoneEntry);
    }
  }
  return phoneList.join(', ');
}

function getEmailAddresses( emails ) {
  // Convert array of emails to comma-separated string
  var emailList = [];
  for (var email=0; email < emails.length; email++) {
    var emailEntry = emails[email].getLabel() + ' ' + emails[email].getAddress();
    if (emails[email].isPrimary()) {
      // Add primary email at front
      emailList.unshift(emailEntry);
    }
    else {
      // Add other emails at end
      emailList.push(emailEntry);
    }
  }
  return emailList.join(', ');
}

/**
 * Adds a custom menu to the active spreadsheet
 */
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Copy contact group to sheet",
    functionName : "getContactGroup"
  }];
  sheet.addMenu("Script Center Menu", entries);
};
脚本版本2 这不像以前的版本那样高效,因为每一列数据都是单独编写的。它也将更难维护和扩展,因为它有更多的代码。仍然很简单,但更多

对于上面显示的示例表,列编号是正确的,因此需要针对您自己的电子表格进行调整

/**
 * Retrieves all contacts for a group. If groupName isn't passed as
 * a parameter, user will be prompted.
 *
 * Version 2: This version allows the columns of contact info to be
 * non-contiguous, but is less efficient than version 1.
 */
function getContactGroup2(groupName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  groupName = groupName || Browser.inputBox('Get Contact Group', 'Group name:', Browser.Buttons.OK_CANCEL);
  if (groupName === 'cancel') {
    ss.toast('Task canceled', 'Status', 3);
    return;
  }

  var group = ContactsApp.getContactGroup(groupName);
  if (!group) {
    ss.toast('Group not found', 'Status', 3);
    return;
  }
  else {
    var contactName = []; // Arrays to hold contact info for spreadsheet
    var contactPhone = [];
    var contactEmail = [];
    var contactLastUpdated = [];

    var contacts = group.getContacts();
    if (contacts.length == 0) {
      ss.toast('No contacts in group', 'Status', 3);
      return;
    }
    else {
      ss.toast('Starting Copy', 'Status', 3);

      for (var i=0; i<contacts.length; i++) {
        // Record contact information in column arrays
        contactName.push( [contacts[i].getFullName()] );
        contactPhone.push( [getPhoneNumbers(contacts[i].getPhones())] );
        contactEmail.push( [getEmailAddresses( contacts[i].getEmails())] );
        contactLastUpdated.push( [contacts[i].getLastUpdated()] );
      }
    }
  }

  ss.toast('Storing to sheet', 'Status', 3);

  // Output all contact info to spreadsheet in one shot per column.
  var sheet = ss.getActiveSheet();
  var headers = 1; // # rows containing headers

  var columnName = 1,  // Column numbers to receive contact info
      columnPhone = 2,
      columnEmail = 3,
      columnLastUpdated = 4;
  sheet.getRange(1+headers,columnName,contactName.length,1).setValues(contactName);
  sheet.getRange(1+headers,columnPhone,contactPhone.length,1).setValues(contactPhone);
  sheet.getRange(1+headers,columnEmail,contactEmail.length,1).setValues(contactEmail);
  sheet.getRange(1+headers,columnLastUpdated,contactLastUpdated.length,1).setValues(contactLastUpdated);

  ss.toast('Operation complete', 'Status', 3);
};

谢谢但是我的工作表中的列不是连续的,那么我如何设置每列中的值呢?如果你真的不能重新排列电子表格,你可以通过写入每列数据而不是单个单元格来获得一些性能优势。请使用版本2脚本查看更新的答案。谢谢。但是我的工作表中的列不是连续的,那么我如何设置每列中的值呢?如果你真的不能重新排列电子表格,你可以通过写入每列数据而不是单个单元格来获得一些性能优势。使用版本2脚本查看更新的答案。