【发布时间】:2020-11-22 23:50:33
【问题描述】:
我在Check if contact exists under google contacts with "ContactsApp.getContact" 找到了以下代码作为对同一问题的回复,但遇到了一些问题。我正在尝试创建一个 Google 脚本来检查 Google 调查的受访者是否已经在“系统组:我的联系人”中拥有一张联系人卡片,如果没有,请创建一个联系人卡片,然后添加他们的电话号码。
我已更新以下代码以引用我的字段,但收到 if(emailjson[email.toLowerCase()].id) 行的错误 - 这是为了检查该联系人是否存在:
TypeError:无法从未定义中读取属性“id”。 (第 36 行,文件“代码”)
我认为 IF 语句有问题,但我对 Javascript(或 JSON)并不完全熟悉。作为 Excel 和 Access 人员,我习惯于 IF 语句中的 IF 为空,然后执行此操作,因此我尝试添加 === undefined,将代码更改为 if(emailjson[email.toLowerCase()].id === undefined),但这并没有解决问题。在查看执行记录时,它一碰到没有名片的人就挂断了。
如果我从电子表格的开头删除新联系人并返回到原始代码(不带 === undefined),那么它会为已经有卡片的每个人创建重复的联系人卡片,直到它到达下一个人谁是新的,然后又出错了。
对于已经有名片的人,如果电子表格中的电话号码只是数字,我也会在下一行收到错误消息,if(!emailjson[email.toLowerCase()]['phones'][phone.replace(/[_)(\s.-]/g,'')]) {。
TypeError:在对象 2024313437 中找不到函数替换。(第 37 行,文件“代码”)
如果我为电话号码添加格式,那么它不会引发错误(尽管正如我之前提到的,它会创建一个重复的联系人)。奇怪的是,我之后运行 emailsasJSON 函数,它只显示一张联系人卡片而不是重复卡片,即使在 Google 联系人屏幕上我每次尝试运行脚本时都会看到一张联系人卡片。
提前感谢您的想法。
function emailsasJSON() {
var emailjson = {}
var myContacts = ContactsApp.getContactGroup('System Group: My Contacts').getContacts();
for (var i = 0; i < myContacts.length; i++) {
var emails = myContacts[i].getEmails();
var phonesobj = myContacts[i].getPhones();
var phones = {}
for (var j = 0; j < phonesobj.length; j++) {
phones[phonesobj[j].getPhoneNumber().replace(/[_)(\s.-]/g,'')] = 1;
}
for (var j = 0; j < emails.length; j++) {
emailjson[emails[j].getAddress().toLowerCase()] = {id: myContacts[i].getId(), phones: phones};
}
}
Logger.log(JSON.stringify(emailjson))
return emailjson;
}
function addClient() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNew = ss.getActiveSheet();
var clientsgroup = ContactsApp.getContactGroup('System Group: My Contacts')
//this is where we will insert the function from above to get the emailjson obj
var emailjson = emailsasJSON()
var contactarray = sheetNew.getDataRange().getValues();
for (var i = 1 ; i < contactarray.length; i++){
var name = contactarray[i][1]
var email = contactarray[i][4]
var phone = contactarray[i][3]
if(emailjson[email.toLowerCase()].id) { //check if email exists
if(!emailjson[email.toLowerCase()]['phones'][phone.replace(/[_)(\s.-]/g,'')]) { //if email exists but phone doesn't, add phone
ContactsApp.getContactById(emailjson[email.toLowerCase()].id).addPhone(ContactsApp.Field.MOBILE_PHONE, phone)
emailjson[email.toLowerCase()]['phones'][phone.replace(/[_)(\s.-]/g,'')] = 1; //add it to the emailjson object in case there are more iterations of this contact in the sheet
}
} else { //add new contact if it doesn't exist
var newcontact = ContactsApp.createContact(name.split(' ')[0],name.split(' ')[1], email)
newcontact.addPhone(ContactsApp.Field.MOBILE_PHONE, phone)
emailjson[email.toLowerCase()]['id'] = newcontact.getId();
emailjson[email.toLowerCase()]['phones'][phone.toString().replace(/[_)(\s.-]/g,'')] = 1;
clientsgroup.addContact(newcontact)
}
}
}
【问题讨论】:
-
您能否为您的 google 表格和联系人组中的值添加示例?
-
如果同时记录 emailjson 和电子邮件数组
Logger.log(emailjson, emailjosn[email.toLowercase()].id),会得到什么? -
@MateoRandwolf -
Logger.log(emailjson,emailjson[emails.toLowerCase()].id)返回 > 类型错误:在对象 EmailField 中找不到函数 toLowerCase。如果我删除.toLowerCase(),那么我会得到 > TypeError: Cannot read property "id" from undefined。 (第 18 行,文件“代码”) -
@HarshanaSerasinghe - 按要求添加示例数据。
标签: javascript google-apps-script google-api google-contacts-api