簡體   English   中英

使用應用程序腳本從谷歌表中刪除 email 時如何從谷歌聯系人中刪除聯系信息

[英]how to remove contact info from Google contact when email removed from google sheet using apps script

我正在開發一個應用程序腳本,該腳本將聯系信息(姓名和電子郵件)從谷歌表寫入谷歌聯系人。

google sheet 上有 3 列,第一列是時間戳,第二列是 email,第三列是名稱。

Apps 腳本可以工作,因為將聯系人添加到谷歌聯系人,但是,我希望當從谷歌表中刪除任何 email 時,在谷歌表更新/編輯時,該聯系人信息也會從谷歌聯系人中刪除。 我嘗試了一些,但卡住了,無法成功。 我是 Apps 腳本的新手。

如果幫助我編寫腳本,我將不勝感激。

這是我得到的:

function AddToGoogleContacts() {

var sheet = SpreadsheetApp.getActive().getSheetByName("sheet1");  
for (var i = 0; i < sheet.getLastRow()-1; i++) { 

var contactEmail = sheet.getRange(i+2,2,1,2).getValue(); 
var myContact = ContactsApp.getContact(contactEmail); 

 if (myContact === null){  ContactsApp.createContact(sheet.getRange(i+2,3,1,2).getValue(),"",sheet.getRange(i+2,2,1,2).getValue());  

 }
 }
 } 
function delGoogleContact(email) {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("sheet1");
  const vs = sh.getRange(2,1,sh.getLastRow()-1,3).getValues();
  vs.forEach((r,i)=>{
    if(r[1]==email){
      ContactsApp.deleteContact(ContactsApp.getContact(email));
    } 
  });
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM