【问题标题】:Import Birthday's contacts from Gmail into a spreadsheet将生日的联系人从 Gmail 导入电子表格
【发布时间】:2014-09-03 19:56:59
【问题描述】:

我正在使用 GoogleAppsScript@google.com 提供的自定义版本的“Contact Listings.gs”脚本将来自 Gmail 联系人的数据导入 Google 电子表格。 我无法导入生日。

这是我尝试使用的代码:

function listContacts() {
  var parameters = SpreadsheetApp.getActiveRange().getValues()[0];
  var group = parameters.shift();

  var people;
  if ((group) && (group!=""))  {
    var contactGroup = ContactsApp.findContactGroup(group);
    if (contactGroup)
      people = contactGroup.getContacts();
  } else
    people = ContactsApp.getAllContacts();

  if (!people)
    return;

  // grab all the contacts
  var retvalues = new Array();
  for (var i in people) {    
    var value = new Array();
    value.push(people[i].getFullName());
    value.push(people[i].getHomeAddress());
    value.push(people[i].getPrimaryEmail());
    value.push(people[i].getMobilePhone());
    value.push(people[i].getHomePhone());
    value.push(people[i].getDates(ContactsApp.Field.BIRTHDAY));

    for (var j in parameters) {
      var info = people[i]["get"+parameters[j]]();
      value.push(info);
    }
    retvalues.push(value);
  }

  var r;
  if ((group) || (parameters.length>0))
    r = SpreadsheetApp.getActiveRange().getRowIndex()+1;
  else
    r = SpreadsheetApp.getActiveRange().getRowIndex();

  var c = SpreadsheetApp.getActiveRange().getColumnIndex();
  if (retvalues.length>0)
        SpreadsheetApp.getActiveSheet().getRange(r,c,retvalues.length,retvalues[0].length).setValues(retvalues);
  else
    SpreadsheetApp.getActiveSheet().getRange(r,c).setValue("No contacts found.");
}

脚本用“DateField”而不是可读的生日日期填充单元格。

有什么想法吗?

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    getDates() 方法返回一个对象数组,每个对象都有specific methods 来检索日期或月份。

    在你的代码中替换行

    value.push(people[i].getDates(ContactsApp.Field.BIRTHDAY));
    

    使用此代码

    var dates = [];
    var birthdays = people[i].getDates(ContactsApp.Field.BIRTHDAY);
    for (var i in birthdays) {
      dates.push(birthdays[i].getMonth()+'/'+birthdays[i].getDay());
    }
    value.push(dates.join(' + '));// in case there is more than 1 birthday  !?!
    

    编辑:

    以下评论

    要以数字日期格式显示值,请在代码末尾添加以下行:

    SpreadsheetApp.getActiveSheet().getRange(1,6,retvalues.length,1).setNumberFormat('d/MM/yyyy');
    

    【讨论】:

    • 太棒了!非常感谢。但是有没有办法将月份返回为数字而不是字母?
    • 您的电子表格可以做到这一点,只需使用列格式...如果需要,我使用 / 分隔符强制他的值成为电子表格中的日期。
    • 或使用脚本,只需在脚本末尾添加SpreadsheetApp.getActiveSheet().getRange(1,6,retvalues.length,1).setNumberFormat('d/MM/yyyy');
    • 无意冒犯,我认为我可以接受的不仅仅是唯一的答案。
    【解决方案2】:

    我以前从未在ContactApp 中玩过日期,我不得不说,这太可怕了。多么疯狂的DateField 对象。这是我的解决方案:

    function listContacts() {
      var contactGroup = ContactsApp.findContactGroup('System Group: Family');
      if( !contactGroup ) throw 'Group not found';
      var people = contactGroup.getContacts();
    
      var values = [['FullName','Address','Email','Mobile','Phone','Birthday']];
      for( var i in people ) {    
        var birthday = people[i].getDates(ContactsApp.Field.BIRTHDAY);
        values.push([
          people[i].getFullName(),
          people[i].getHomeAddress(),
          people[i].getPrimaryEmail(),
          people[i].getMobilePhone(),
          people[i].getHomePhone(),
          birthday.length ? parseDateField(birthday[0]) : '',
        ]);
      }
    
      var s = SpreadsheetApp.getActiveSheet();
      s.getDataRange().clear();
      s.getRange(1,1,values.length,values[0].length).setValues(values);
    }
    
    function parseDateField(df) {
      return new Date(
        df.getYear(),
        {JANUARY:0,FEBRUARY:1,MARCH:2,APRIL:3,MAY:4,JUNE:5,JULY:6,
         AUGUST:7,SEPTEMBER:8,OCTOBER:9,NOVEMBER:10,DECEMBER:11}[df.getMonth()],
        df.getDay());
    }
    

    此解决方案与使用“/”分隔符并让电子表格解析它的不同之处在于:这不依赖电子表格,如果您想直接在代码中使用此日期,这可能很重要,例如CalendarApp

    【讨论】:

    • 感谢 Henrique :-) 随着我在代码中添加的最后一次更新,这些值肯定是电子表格中的日期,但您的解决方案确实允许 直接使用日期作为 JS 日期...谢谢,赞成!
    • @Sergeinsas Yours 也是一个不错的解决方案,我只是在发布后才看到它(将近半小时后!)。但顺便说一句,这个 DateField 对象是不必要的愚蠢。我真的很感动!
    • 你是对的......我想这个想法是生日不应该有一个“年”属性(也没有时间价值),因为它的周期性......但这很愚蠢,我同意。
    • 非常感谢你们,你们让我开心!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多