如果 google 个“ContactsApp.getContact”联系人下不存在联系人,请添加联系人
Add contact if contact does not exist under google contacts with “ContactsApp.getContact”
我在 Check if contact exists under google contacts with "ContactsApp.getContact" 找到了以下代码作为对同一问题的答复,但遇到了一些问题。我正在尝试创建一个 Google 脚本来检查 Google 调查的受访者是否已经在 'System Group: My Contacts' 中拥有名片,如果没有,则创建名片然后添加他们的名片phone个。
我已更新以下代码以引用我的字段,但我收到行 if(emailjson[email.toLowerCase()].id)
的错误 - 这是为了检查该联系人是否存在:
TypeError: Cannot read property "id" from undefined. (line 36, file "Code")
我以为是IF语句有问题,但我对Javascript(或JSON)不是很熟悉。作为一个 Excel 和 Access 的人,我习惯于 IF 语句有一个 IF something is null, then do this, 所以我尝试添加 === undefined
,将代码更改为 if(emailjson[email.toLowerCase()].id === undefined)
但是没有解决问题。在查看执行记录时,它一碰到没有名片的人就挂了。
如果我从我的电子表格的开头删除新的联系人并返回到原始代码(没有 === undefined
),那么它会为每个已经有名片的人创建重复的名片,直到它到达下一个新人,然后再次出错。
对于已经有名片的人,如果电子表格中的 phone 数字只是数字,我也会在下一行收到错误消息,if(!emailjson[email.toLowerCase()]['phones'][phone.replace(/[_)(\s.-]/g,'')]) {
。
TypeError: Cannot find function replace in object 2024313437. (line 37, file "Code")
如果我将格式添加到 phone 号码,那么它不会抛出错误(尽管正如我之前提到的,它会创建重复的联系人)。奇怪的是,我 运行 函数 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)
}
}
}
示例数据:
您的主要问题是您的函数 emailsasJSON()
将 json 对象字符串化(即它是一个字符串),因此您无法成功访问 JSON 的密钥对值目的。为了解决这个问题,我只是解析了对象以确保它是一致的,但只要你没有将它转换成字符串就可以了。
下一个变化是第一个if条件。您实际上不需要检查 id 来查看元素是否存在,因为只需检查元素本身就可以完成工作(尽管检查 id 也可以)。
最后,您为新联系人构建 json 对象的方式不正确,因此我已相应地对其进行更正以添加适当的键值对。
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};
}
}
emailjson = JSON.stringify(emailjson);
emailjson = JSON.parse(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()]) { //check if email exists
if(!emailjson[email.toLowerCase()]['phones'][phone.toString().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.toString().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);
var newContactId = newcontact.getId();
var phoneNumber = phone.toString().replace(/[_)(\s.-]/g,'');
ContactsApp.getContactById(newContactId).addPhone(ContactsApp.Field.MOBILE_PHONE, phone);
emailjson[email.toLowerCase()] = {id:newContactId,phones:{phoneNumber : 1}}
clientsgroup.addContact(newcontact)
}
}
}
参考
- Guide for working with JSON 在 JavaScript
我在 Check if contact exists under google contacts with "ContactsApp.getContact" 找到了以下代码作为对同一问题的答复,但遇到了一些问题。我正在尝试创建一个 Google 脚本来检查 Google 调查的受访者是否已经在 'System Group: My Contacts' 中拥有名片,如果没有,则创建名片然后添加他们的名片phone个。
我已更新以下代码以引用我的字段,但我收到行 if(emailjson[email.toLowerCase()].id)
的错误 - 这是为了检查该联系人是否存在:
TypeError: Cannot read property "id" from undefined. (line 36, file "Code")
我以为是IF语句有问题,但我对Javascript(或JSON)不是很熟悉。作为一个 Excel 和 Access 的人,我习惯于 IF 语句有一个 IF something is null, then do this, 所以我尝试添加 === undefined
,将代码更改为 if(emailjson[email.toLowerCase()].id === undefined)
但是没有解决问题。在查看执行记录时,它一碰到没有名片的人就挂了。
如果我从我的电子表格的开头删除新的联系人并返回到原始代码(没有 === undefined
),那么它会为每个已经有名片的人创建重复的名片,直到它到达下一个新人,然后再次出错。
对于已经有名片的人,如果电子表格中的 phone 数字只是数字,我也会在下一行收到错误消息,if(!emailjson[email.toLowerCase()]['phones'][phone.replace(/[_)(\s.-]/g,'')]) {
。
TypeError: Cannot find function replace in object 2024313437. (line 37, file "Code")
如果我将格式添加到 phone 号码,那么它不会抛出错误(尽管正如我之前提到的,它会创建重复的联系人)。奇怪的是,我 运行 函数 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)
}
}
}
示例数据:
您的主要问题是您的函数 emailsasJSON()
将 json 对象字符串化(即它是一个字符串),因此您无法成功访问 JSON 的密钥对值目的。为了解决这个问题,我只是解析了对象以确保它是一致的,但只要你没有将它转换成字符串就可以了。
下一个变化是第一个if条件。您实际上不需要检查 id 来查看元素是否存在,因为只需检查元素本身就可以完成工作(尽管检查 id 也可以)。
最后,您为新联系人构建 json 对象的方式不正确,因此我已相应地对其进行更正以添加适当的键值对。
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};
}
}
emailjson = JSON.stringify(emailjson);
emailjson = JSON.parse(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()]) { //check if email exists
if(!emailjson[email.toLowerCase()]['phones'][phone.toString().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.toString().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);
var newContactId = newcontact.getId();
var phoneNumber = phone.toString().replace(/[_)(\s.-]/g,'');
ContactsApp.getContactById(newContactId).addPhone(ContactsApp.Field.MOBILE_PHONE, phone);
emailjson[email.toLowerCase()] = {id:newContactId,phones:{phoneNumber : 1}}
clientsgroup.addContact(newcontact)
}
}
}
参考
- Guide for working with JSON 在 JavaScript