我想刪除一個自定義字段的基礎上它的值,但它死於測試。當前代碼:刪除自定義字段谷歌應用腳本
function testDelete2() {
var contacts = ContactsApp.findContactGroup('test').getContacts();
for (var i in contacts) {
var checkfield = contacts[i].getUserDefinedField('testlabel')
if (checkfield == 'testvalue') {
var customFields = contacts[i].getCustomFields();
customFields.deleteCustomField('testlabel');
}
}
}
我得到這個錯誤:TypeError:在對象CustomField中找不到函數deleteCustomField。
不知道這意味着什麼。請幫忙。我一遍又一遍地閱讀此頁,並沒有幫助:https://developers.google.com/apps-script/service_contacts
我甚至試過這種變化,沒有工作:
customFields.getLabel('testlabel').deleteCustomField();
此外,有沒有與任何樣品簡單文檔的任何地方如何處理谷歌聯繫人自定義字段?添加,刪除,只是獲得價值似乎都不可能。我很欣賞這個問題的幫助,但也不介意在簡單的示例中查找指南。
使用嗶嘰的偉大代碼爲靈感想出了這個代碼刪除(將添加完整的代碼與刪除/添加很快):
UPDATE:簡化的過程(不知道爲什麼沒有在嘗試了這一點開始,也許我沒有,但被編碼錯誤的,反正)採取了刪除/添加自定義字段,只是更新該自定義字段的值
function testUpdateDues() {
var duescos = ContactsApp.findContactGroup('z8 - Assoc').getContacts();
for (var i in duescos) {
var customFields = duescos[i].getCustomFields();
for (var n in customFields) {
if (customFields[n].getLabel() == 'Dues Amount' && customFields[n].getValue() == 'unstated'){
customFields[n].setValue('$ 500');
}
}
}
}
最後編輯允許我添加/編輯任何自定義字段根據谷歌聯絡小組的分配(感謝Serge的協助!!)與腳本中的基於時間的觸發器:
function UpdateRegion1() {
UpdateCustomField('Reg 1 - Pan', 'Region' , 'Region 1 - Panhandle');
}
function UpdateCustomField(group, customlabel, customvalue) {
var contacts = ContactsApp.findContactGroup(group).getContacts();
for (var i in contacts) {
var fields = new Array();
var customFields = contacts[i].getCustomFields();
for(var n in customFields) {
fields.push(customFields[n].getLabel());
}
if (fields.indexOf(customlabel)==-1){
contacts[i].addCustomField(customlabel, customvalue);
}
for(var j in customFields) {
if (customFields[j].getLabel() == customlabel && customFields[j].getValue() != customvalue){
customFields[j].setValue(customvalue);
}
}
}
}
你爲什麼要加上'使用Utilities.sleep(4000)'添加自定義字段?你沒有嘗試過嗎? –
是的,當一個聯繫人被編輯爲「太快」時,我得到了etag錯誤 - 因爲一個字段被刪除,然後另一個添加在一個聯繫人中,我添加它允許刪除「採取」。我編輯單個聯繫人連續動作的每個腳本都向我發送了etag錯誤,如果我沒有包含4秒的延遲 - 討厭這樣做,因爲它會減慢它的速度,但我已經搜索並搜索,似乎是隻有有效的解決方案 – user1783229
fyi - 我會爲您的答案投一個加號,但系統仍然不會允許我,因爲我是新的 – user1783229