您可以使用的最重要的优化是使用单个调用而不是您当前拥有setValues()
的许多单单元格操作将数据写入电子表格。setValue()
阅读更快的答案:ScriptDb 还是 SpreadsheetApp?详情。
但是,要利用setValues()
,您必须重新排列电子表格,以便您编写的联系信息是连续的。考虑一个这样排列的电子表格:
通过这样的设置,我们可以将所有联系信息收集到一个二维数组中,然后像这样进行一次调用:
sheet.getRange(_row_,_col_,data.length,data[0].length).setValues(data);
..._row_,_col_
我们要写入数据的范围的左上角在哪里。
脚本
这是一个完整的脚本,它将检索一个组的所有联系人,并填充该示例电子表格。它可以轻松扩展以包含其他联系信息。
/**
* 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);
};