function matchAndReplace(){
var oldDomain = "@olddomain.com";
var newDomain = "@newdomain.com";
// retrieve all the user's contacts
var contacts = ContactsApp.getContacts();
Logger.log('num '+contacts.length);
for (var i = 0; i < contacts.length; i++) {
var emails = contacts[i].getEmails();
Logger.log(emails.length+' emails');
for (var e in emails) {
var email = emails[e].getAddress();
//emails[e].setLabel(ContactsApp.Field.WORK_EMAIL);
Logger.log(email);
if (email.indexOf(oldDomain) !== -1) {
//remove
emails[e].deleteEmailField(); <-- error
Logger.log(email+' removed');
//add
var newEmail = email.split("@")[0]+newDomain;
var newEmailField = contacts[i].addEmail(ContactsApp.Field.WORK_EMAIL, newEmail);
Logger.log(newEmail+' added');
}
}
//break;
}
}
错误:
Service error: ContactsApp: Entry does not have any fields set. (line 20, file "Code")
答案 0 :(得分:1)
您可能遇到暂时服务问题,因为您的代码没有明显错误。但是,您可以提高效率并消除所有删除/添加操作,这可能会降低错误的可能性。
试试这个:
/**
* Find all contacts that have email addresses in the oldDomain,
* and change them to be the same ID in the newDomain.
*
* @param {String} oldDomain E.g. 'oldExample.com'
* @param {String} newDomain E.g. 'newExample.org'
*
* @returns {Number} Count of updated contacts.
*/
function migrateDomain(oldDomain, newDomain){
// Validate arguments
if (arguments.length !== 2) throw new Error( 'Missing arguments.');
oldDomain = '@' + oldDomain;
newDomain = '@' + newDomain;
// retrieve all the user's contacts that have email in oldDomain
var contacts = ContactsApp.getContactsByEmailAddress(oldDomain);
Logger.log('num '+contacts.length);
for (var i = 0; i < contacts.length; i++) {
var emails = contacts[i].getEmails();
Logger.log(emails.length+' emails');
for (var e=0; e<emails.length; e++) {
var email = emails[e].getAddress();
Logger.log(email);
if (email.indexOf(oldDomain) !== -1) {
// change
var newEmail = email.split("@")[0]+newDomain;
emails[e].setAddress(newEmail);
Logger.log(' changed to '+newEmail);
}
}
}
return i;
}