创建自定义字段时出错

时间:2012-11-05 12:21:39

标签: google-apps-script

我有一个脚本,它为我的联系人使用自定义字段。 它会在生日前一周向我发送邮件,并提供选项。 (提醒)

在生日本身,脚本可以向该人发送电子邮件。

但我在自定义字段中出错:

> 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";
            }

2 个答案:

答案 0 :(得分:0)

这是一个已知问题,已在问题跟踪器中提交,但尚未更新状态。在那里添加你的明星和评论以提高其优先级。

检查Issue 1528

答案 1 :(得分:0)

我现在已经解决了。

我在函数顶部添加try并以:

结束
  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);

}