Class DateField

日期字段

已弃用。请改用 People API 高级服务

联系人中的日期字段。

此类仅供“通讯录”服务使用,Apps Script 中其他位置使用的日期使用 JavaScript 的标准 Date 对象。

方法

方法返回类型简介
getLabel()Object获取此字段的标签。
setLabel(label)DateField设置此字段的标签。
方法返回类型简介
deleteDateField()void删除此日期。
getDay()Integer获取此日期所在的一月中的第几天。
getMonth()Month获取此日期所在的月份。
getYear()Integer获取此日期的年份。
setDate(month, day)DateField将日期设置为今天,不指定年份。
setDate(month, day, year)DateField将日期设置为当天。
setLabel(label)DateField设置此字段的标签,例如“生日”或“周年纪念”。

详细文档

getLabel()

获取此字段的标签。这可以是 Field、ExtendedField 或 String。

// Logs the label for all the address fields associated with contact
// 'John Doe'. This method can be similarly called for any field that has
// a label.
const contacts = ContactsApp.getContactsByName('John Doe');
const addressFields = contacts[0].getAddresses();
for (let i = 0; i < addressFields.length; i++) {
  Logger.log(addressFields[i].getLabel());
}

返回

Object - 此字段的标签

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.google.com/m8/feeds

setLabel(label)

设置此字段的标签。

// Sets the label to 'Apartment' for the first address field associated
// with contact 'John Doe'. This method can be similarly called for any
// field that has a label.
const contacts = ContactsApp.getContactsByName('John Doe');
const addressFields = contacts[0].getAddresses();
addressFields[0].setLabel('Apartment');

参数

名称类型说明
labelString此字段的新标签

返回

DateField - this 字段,适用于链式调用

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.google.com/m8/feeds

已弃用的方法

已弃用。此函数已废弃,不应在新脚本中使用。

删除此日期。

// Deletes all the dates that are set for contact 'John Doe'
const contacts = ContactsApp.getContactsByName('John Doe');
const dates = contacts[0].getDates();
for (let i = 0; i < dates.length; i++) {
  dates[i].deleteDateField();
}

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.google.com/m8/feeds

已弃用。此函数已废弃,不应在新脚本中使用。

获取此日期所在的一月中的第几天。

注意:对于标准 JavaScript Date 对象, getDay() 方法会改为返回一周中的第几天。

// Logs the day of the birthday for contact 'John Doe'
const contacts = ContactsApp.getContactsByName('John Doe');
const birthday = contacts[0].getDates(ContactsApp.Field.BIRTHDAY)[0];
Logger.log(birthday.getDay());

返回

Integer - 日期

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.google.com/m8/feeds

已弃用。此函数已废弃,不应在新脚本中使用。

获取此日期所在的月份。

// Logs the month of the birthday for contact 'John Doe'
const contacts = ContactsApp.getContactsByName('John Doe');
const birthday = contacts[0].getDates(ContactsApp.Field.BIRTHDAY)[0];
Logger.log(birthday.getMonth());

返回

Month - 月份

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.google.com/m8/feeds

已弃用。此函数已废弃,不应在新脚本中使用。

获取此日期的年份。

// Logs the year of the birthday for contact 'John Doe'
const contacts = ContactsApp.getContactsByName('John Doe');
const birthday = contacts[0].getDates(ContactsApp.Field.BIRTHDAY)[0];
Logger.log(birthday.getYear());

返回

Integer - 年份

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.google.com/m8/feeds

已弃用。此函数已废弃,不应在新脚本中使用。

将日期设置为今天,不指定年份。

此方法仅适用于不需要年份的日期字段,例如生日。

// Sets the birthday for contact 'John Doe' to April 1
const contacts = ContactsApp.getContactsByName('John Doe');
const birthday = contacts[0].getDates(ContactsApp.Field.BIRTHDAY)[0];
birthday.setDate(ContactsApp.Month.APRIL, 1);

参数

名称类型说明
monthMonth月份
dayInteger这一天

返回

DateField - 此日期,适用于链式调用

抛出

Error - 如果此日期字段需要年份

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.google.com/m8/feeds

已弃用。此函数已废弃,不应在新脚本中使用。

将日期设置为今天。

// Sets the birthday for contact 'John Doe' to April 1, 1980
const contacts = ContactsApp.getContactsByName('John Doe');
const birthday = contacts[0].getDates(ContactsApp.Field.BIRTHDAY)[0];
birthday.setDate(ContactsApp.Month.APRIL, 1, 1980);

参数

名称类型说明
monthMonth月份
dayInteger这一天
yearInteger

返回

DateField - 此日期,适用于链式调用

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.google.com/m8/feeds

已弃用。此函数已废弃,不应在新脚本中使用。

设置此字段的标签,例如“生日”或“周年纪念”。

// Retrieves the first date that's set for contact 'John Doe' and re-labels
// it as an anniversary
const contacts = ContactsApp.getContactsByName('John Doe');
const firstDate = contacts[0].getDates()[0];
firstDate.setLabel(ContactsApp.Field.ANNIVERSARY);

参数

名称类型说明
labelField新标准标签

返回

DateField - this 字段,适用于链式调用

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.google.com/m8/feeds