I am new to the Apps script and trying to compare two email lists, emails from google contacts and emails from a google sheet (column B),
I want to delete email/s from Google Contacts that are not listed in the google sheet. I got partial code from here and trying to modify it for my use, however, I stuck in the last part to detect the email that is not in the google sheet and then delete it from google contact. Here is what I could put together, Extracting emails from Google contacts and Google sheet works fine. running the following code does not work, but I don't get any error either.
Edited: I am not sure the first part of the code that extracts data from google contacts is in one array, because each email is printed like this when used console.log: [email1@email.com]
Appreciate your help to make it work.
function deleteContacts() {
var contactEmails = ContactsApp.getContacts().map(function(contact) {
return contact.getEmailAddresses();
//console.log("Google Emails:" + contactEmails);
var sheet = SpreadsheetApp.getActive().getSheetByName("sheet2");
var ColEmails = sheet.getRange('B2:B').getValues().map(function(value) {
return value[0];
}).filter(function(val) {
return val != "";
})
//console.log("Sheet Emails:" + ColEmails);
for (var i = 0; i < contactEmails.length; i++) {
if (ColEmails.indexOf(contactEmails[i]) == -1) {
var contact = ContactsApp.getContactsByEmailAddress(contactEmails[i]);
ContactsApp.deleteContact(contact);
}
}
});
}