2012-11-05 173 views
0

我有一個腳本正在爲我的聯繫人使用自定義字段。 它在選項的一個生日前一週給我發一封郵件。 (提醒)創建自定義字段時出錯

生日本身,腳本可以發送電子郵件給該人。

但我得到一個錯誤與自定義字段:

> Exception on line: 157 -> Service error: ContactsApp: Mismatch: etags = ["RHY5cTVSLit7I2A9WhNREUwPTgU."], version = [1352115891110001] 

如果我運行該腳本兩次就消失了。每次自定義字段將被添加。

的代碼是這樣的:

function getMonth(month_num) { 
    var month; 
    switch (month_num) { 
    case 1: 
     month = ContactsApp.Month.JANUARY; 
     break; 
    case 2: 
     month = ContactsApp.Month.FEBRUARY; 
     break; 
    case 3: 
     month = ContactsApp.Month.MARCH; 
     break; 
    case 4: 
     month = ContactsApp.Month.APRIL; 
     break; 
    case 5: 
     month = ContactsApp.Month.MAY; 
     break; 
    case 6: 
     month = ContactsApp.Month.JUNE; 
     break; 
    case 7: 
     month = ContactsApp.Month.JULY; 
     break; 
    case 8: 
     month = ContactsApp.Month.AUGUST; 
     break; 
    case 9: 
     month = ContactsApp.Month.SEPTEMBER; 
     break; 
    case 10: 
     month = ContactsApp.Month.OCTOBER; 
     break; 
    case 11: 
     month = ContactsApp.Month.NOVEMBER; 
     break; 
    case 12: 
     month = ContactsApp.Month.DECEMBER; 
     break; 
    } 
    return(month);  
} 

function getMonthT(month_num) { 
    var monthT; 
    switch (month_num) { 
    case 1: 
     monthT = "januari"; 
     break; 
    case 2: 
     monthT = "februarie"; 
     break; 
    case 3: 
     monthT = "maart"; 
     break; 
    case 4: 
     monthT = "april"; 
     break; 
    case 5: 
     monthT = "mei"; 
     break; 
    case 6: 
     monthT = "juni"; 
     break; 
    case 7: 
     monthT = "juli"; 
     break; 
    case 8: 
     monthT = "augustus"; 
     break; 
    case 9: 
     monthT = "september"; 
     break; 
    case 10: 
     monthT = "oktober"; 
     break; 
    case 11: 
     monthT = "november"; 
     break; 
    case 12: 
     monthT = "december"; 
     break; 
    } 
    return(monthT);  
} 

function sendReminderBirthday() { 
    try { 
    var date = new Date; 
    for (var loop=0; loop<2; loop++) { 
     if (loop==1) { 
     date.setDate(date.getDate()+7) 
     } 
     var month_num = date.getMonth()+1; 
     var day = date.getDate(); 
     var month = getMonth(month_num); 
     var monthT = getMonthT(month_num); 
     for (var cf=0; cf<2; cf++) { 
     if (cf==0) { 
      var CAF = ContactsApp.Field.BIRTHDAY; 
      var event = "Verjaardag"; 
      var bodyReminderEvent = "De jarige wordt "; 
      var bodyReminderEventX = " jaar oud. Geboren in het jaar "; 
      var bodyEvent = "verjaardag"; 
      var bodyEventX = "je"; 
      var pictureBlob = UrlFetchApp.fetch("http://s13.postimage.org/548mzsu2v/Verjaardag.jpg").getBlob().setName("pictureBlob"); 
     } if (cf==1) { 
      var CAF = ContactsApp.Field.ANNIVERSARY; 
      var event = "Trouwdag"; 
      var bodyReminderEvent = "De trouwdag is "; 
      var bodyReminderEventX = " jaar geleden. Ze zijn getrouwd in het jaar "; 
      var bodyEvent = "trouwdag"; 
      var bodyEvent = "jullie"; 
      var pictureBlob = UrlFetchApp.fetch("http://s13.postimage.org/hhlh6pjrb/trouwdag.jpg").getBlob().setName("pictureBlob"); 
     } 

     var contacts = ContactsApp.getContactsByDate(month, day, CAF); 
     var myEmail = Session.getEffectiveUser().getEmail(); 

     for (var i=0; i<contacts.length; i++) { 
      if (contacts[i]) { 
      var nickname = contacts[i].getNickname(); 
      var givenname = nickname || contacts[i].getGivenName(); 
      var fullname = contacts[i].getFullName(); 
      var primaryEmail = contacts[i].getPrimaryEmail(); 
      var note = contacts[i].getNotes() || "Geen notitie's aanwezig"; 
      var contactID = contacts[i].getId(); 
      var contactID = contactID.replace("http://www.google.com/m8/feeds/contacts/dennis.pater%40gmail.com/base/", ""); 
      var dateEvent = date.getDate() + " " + monthT; 
      var datesContact = contacts[i].getDates(CAF); 
      for (var dc=0; dc<datesContact.length; dc++) { 
       if (datesContact[dc]) { 
       var dateContactY = datesContact[dc].getYear(); 
       } 
      } 
      var age = date.getYear() - dateContactY; 

      var sendBirthdayMails = contacts[i].getCustomFields("Auto Mail"); 
      for (var s=0; s<sendBirthdayMails.length; s++) { 
       if (sendBirthdayMails[s]) { 
       var sendBirthdayMail = sendBirthdayMails[s].getValue(); 
       } 
      } 
      if (s==0) { 
       contacts[i].addCustomField("Auto Mail", "Nee"); 
       var sendBirthdayMail = "Nee"; 
       var CustumField = ""; 
      } 

      var sendPostcards = contacts[i].getCustomFields("Kaartje?"); 
      for (var sp=0; sp<sendPostcards.length; sp++) { 
       if (sendPostcards[sp]) { 
       var sendPostcard = sendPostcards[sp].getValue(); 
       } 
      } 
      if (sp==0) { 
       contacts[i].addCustomField("Kaartje?", "Nee"); 
       var sendPostcard = "Nee"; 
      } 

      var GetPressents = contacts[i].getCustomFields("Cadeau?"); 
      for (var gp=0; gp<GetPressents.length; gp++) { 
       if (GetPressents[gp]) { 
       var GetPressent = GetPressents[gp].getValue(); 
       } 
      } 
      if (gp==0) { 
       contacts[i].addCustomField("Cadeau?", "Nee"); 
       var GetPressent = "Nee"; 
      } 
      var EmailBody = ""; 
      if (sendBirthdayMail=="Ja") { 
       var EmailBody = "Hallo " + givenname + ", <br/><br/>" 
        + "Van harte gefeliciteerd met je "+ age + "e " + bodyEvent + ". Wij wensen " + bodyEventX + " een hele fijne dag toe. <br/><br/>" 
        + "<img src='cid:picture'> <br/><br/><br/>" 
        + "Groetjes, <br/>" 
        + "Dennis & Minou"; 
       if (loop==0) { 
       MailApp.sendEmail(primaryEmail, "Van harte", "", {htmlBody: EmailBody, bcc:myEmail, inlineImages: {picture:pictureBlob} }); 
       } 
      } 
      var Adress = contacts[i].getAddresses(ContactsApp.Field.HOME_ADDRESS); 
      for (var a=0; a<Adress.length; a++) { 
       if (Adress[a]) { 
       var adres = Adress[a].getAddress(); 
       var adresValue = adresValue + " <br/><br/> " + adres; 
       } 
      } 
      var MailCard = "Kaartje versturen: <b>Nee</b>, wil je toch een kaartje sturen? Kijk even op <a href='http://kaartwereld.nl'>'Kaartwereld'</a>. <br/> <i>" + adres + "</i><br/><br/> "; 
      if (sendPostcard=="Ja") { 
       var MailCard = "Verstuur een kaartje via <a href='http://kaartwereld.nl'>'Kaartwereld'</a>. <br/>" 
        + "<i>" + adres + "</i><br/><br/"; 
      } 

      if (loop==1){ 
       if (GetPressent=="Ja") { 
       var Title = "Dennis's list"; 
       var allLists = Tasks.Tasklists.list().getItems(); 
       var id; 
       for (var i in allLists) { 
        if (Title == allLists[i].getTitle()) { 
        id = allLists[i].getId(); 
        } 
       } 

       if (!id) { 
        Logger.log("Tasklist not found"); 
       } else { 
        var tasksName = "Cadeau kopen voor " + givenname; 
        var newTask = Tasks.newTask() 
         .setTitle(tasksName); 
        var inserted = Tasks.Tasks.insert(newTask, id); 
        var getList = Tasks.Tasks.list(id).getItems(); 
        for (var gl in getList) { 
        if (tasksName==getList[gl].getTitle()) { 
         getList[gl].setDue(dateEvent + ", " + dateContactY); 
         getList[gl].setNotes(note); 
        } 
        } 

        var GetPressent = "Ja, de taak is toegevoegd" 
         Logger.log("Task added"); 
       } 
       } 
       if (EmailBody==""){ 
       } else { 
       var EmailBody = "<br/><br/><br/><b>Voorbeeld</b><br/>" + EmailBody; 
       } 
       var ReminderEmailBody = event + " van " + givenname + " op " + dateEvent + ". <br/>" 
        + bodyReminderEvent + age + bodyReminderEventX + dateContactY + ". <br/><br/>" + MailCard 
        + "Moeten we hiervoor even een cadeautje ophalen: <b>" + GetPressent + "</b>. <br/>" 
        + "Ontvangt deze persoon een automatich email bedricht: <b>" + sendBirthdayMail + "</b>. <br/>" 
        + "Notitie's: <br/>" 
        + "<i>" + note + "</i><br/><br/>" 
        + "Om de notitie's te wijzigen of andere wijzigingen door te geven kun je de contact <a href='https://mail.google.com/mail/u/0/#contact/" + contactID + "'>" + givenname + "</a> veranderen.<br/><br/>" 
        + "Dennis" 
        + EmailBody; 
       MailApp.sendEmail(myEmail, event + " van " + givenname + " op " + dateEvent, "", {htmlBody: ReminderEmailBody, inlineImages: {picture:pictureBlob} }); 
      } 
     } 
     } 
    } 
    } 
    } 
    catch (e) { 
    //Browser.msgBox("Error: " + e.message); 
    MailApp.sendEmail(myEmail, "Error report", e.name + ' on line: ' + e.lineNumber + ' -> ' + e.message); 
    } 
} 

這裏談到的錯誤之一:

if (sp==0) { 
      contacts[i].addCustomField("Kaartje?", "Nee"); 
      var sendPostcard = "Nee"; 

和錯誤之二:

if (gp==0) { 
       contacts[i].addCustomField("Cadeau?", "Nee"); 
       var GetPressent = "Nee"; 
      } 

回答

0

我已經解決現在它。

我加試在函數的頂部和結尾:

catch (e) { 
//Browser.msgBox("Error: " + e.message); 
MailApp.sendEmail(myEmail, "Error report", e.name + ' on line: ' + e.lineNumber + ' -> ' + e.message + "your link/ text"); 
var x = x || 0; 
if (x == 12) { 
    return; 
} 
var x = x+1; 
var restart = sendReminderBirthday(x); 

}

0

這是一個已知的問題,並已在被提交問題跟蹤器,但沒有狀態更新。在那裏添加你的明星和評論以提高其優先級。

檢查Issue 1528

+0

@ Dennis1986 - 我看到與ETag的問題已經被報告解決。丹尼斯的問題是否已解決? – Mogsdad