我想与同事共享一张gsheet,他们应该能够运行脚本将联系人从工作表复制到自己的联系人。 我想创建一个MyCorp1 ContactGroup并添加联系人。我想检查用户是否已创建此组。找不到检查方式。有解决方法吗?不幸的是,如果我尝试创建两次或更多组,它将创建重复。
提前致谢 的Gabor
function addCont(row){
var contact = ContactsApp.createContact(row[1], row[0], row[5]);
//if not empty
if (row[3] != "") {contact.setMobilePhone(row[3]);}
if (row[4] != "") {contact.setWorkPhone(row[4]);}
var myGroup = ContactsApp.getContactGroup('MyCorp1');
myGroup.addContact(contact);
}
//-----------------------------------------------------------------------------------
function delcreateGRP(){
var myGroup = ContactsApp.getContactGroup('MyCorp1');
myGroup.deleteGroup();
ContactsApp.createContactGroup('MyCorp1');
}
//-----------------------------------------------------------------------------------
function copyContacts() {
var startRow = 1; // First row of data to process
// This is the Request Spreadsheet
var ss = SpreadsheetApp.openById(REQUEST_SPREADSHEET_ID);
var sheet = ss.getSheets()[0];
// Fetch all the data from the Request Spreadsheet
// getRowsData was reused from Reading Spreadsheet Data using JavaScript Objects
//var data = getRowsData(sheet);
//var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
Logger.log('datalength: ' + data.length);
for (var i = startRow; i < data.length; ++i ) {
var row = data[i];
row.rowNumber = i + 2;
addCont(row);
}
}
答案 0 :(得分:0)
如果没有具有给定名称的联系人组,.getContactGroup('Group Name')
方法返回null,则可以使用if / else语句来完成工作。
var group = ContactsApp.getContactGroup('Group Name');
if(group) {
//do something if the group exists
} else {
//do something if the group DOES NOT exist
}
以下是我使用的文档:https://developers.google.com/apps-script/reference/contacts/contacts-app#getContactGroup(String)