Google Apps脚本:从电子表格中检索日期并写入联系人

时间:2015-07-07 20:57:55

标签: google-apps-script google-contacts

使用Google Apps脚本,我正在尝试使用电子表格编写日期并将其作为日期字段添加到联系人。具体来说,我似乎无法将从电子表格中读取的javascript日期的月份转换为month enum可以使用的contact.addDate method

var months = ContactsApp.Month.values;
var birthdate = new Date( spreadsheet_date );
var month = months[ birthdate.getMonth() ];
contact.addDate(ContactsApp.Field.BIRTHDAY, 
     month, birthdate.getDate(), birthdate.getFullYear() );

4 个答案:

答案 0 :(得分:1)

有很多方法可以接近,因为我看到开关是最简单的,但是没有任何一个班轮,因为Javascript中没有内置月份名称:

var month = birthdate.getMonth();
switch(month){
   case 0:
      month = ContactsApp.Month.JANUARY;
      break;
   case 1:
      month = ContactsApp.Month.FEBRUARY;
      break;
   [...]
   case 11:
      month = ContactsApp.Month.DECEMBER
      break;
}

答案 1 :(得分:1)

另一种模式......

var arr = [
    CalendarApp.Month.JANUARY,
    CalendarApp.Month.FEBRUARY,
    CalendarApp.Month.MARCH,
    ...
];
var month = arr[birthdate.getMonth()];

答案 2 :(得分:0)

在我有另一个问题的帖子中查看我的做法,这里

Google Contact "Birthday" field not showing when set from Apps Script

您需要从枚举到月份名称的映射。这样可以避免使用长switch语句。此解决方案最初是由@tehhowch提供给我的。

答案 3 :(得分:0)

var month = months[ birthdate.getMonth() ];
var monthAsInt={JANUARY:1,FEBRUARY:2,MARCH:3,APRIL:4,MAY:5,JUNE:6,JULY:7
  ,AUGUST:8,SEPTEMBER:9,OCTOBER:10,NOVEMBER:11,DECEMBER:12}[month];

作为功能;

function monthToInt(month) {
  return {JANUARY:1,FEBRUARY:2,MARCH:3,APRIL:4,MAY:5,JUNE:6,JULY:7
      ,AUGUST:8,SEPTEMBER:9,OCTOBER:10,NOVEMBER:11,DECEMBER:12}[month];
}