Class CalendarApp

CalendarApp

允許指令碼讀取及更新使用者的 Google 日曆。這個類別可直接存取使用者的預設日曆,以及擷取使用者擁有或訂閱的其他日曆。

屬性

屬性類型說明
ColorColor列舉代表 Google 日曆服務提供的具名顏色。
EventColorEventColor列舉代表 Google 日曆服務提供的已命名活動顏色。
GuestStatusGuestStatus此列舉代表了活動來賓可擁有的活動狀態。
MonthMonth以列舉表示一年中的月份。
VisibilityVisibility代表事件可見性的列舉。
WeekdayWeekday列舉代表星期幾。

方法

方法傳回類型簡短說明
createAllDayEvent(title, date)CalendarEvent建立新的全天活動。
createAllDayEvent(title, startDate, endDate)CalendarEvent建立可以橫跨多天的新全天活動。
createAllDayEvent(title, startDate, endDate, options)CalendarEvent建立可以橫跨多天的新全天活動。
createAllDayEvent(title, date, options)CalendarEvent建立新的全天活動。
createAllDayEventSeries(title, startDate, recurrence)CalendarEventSeries建立新的全天活動系列。
createAllDayEventSeries(title, startDate, recurrence, options)CalendarEventSeries建立新的全天活動系列。
createCalendar(name)Calendar建立使用者擁有的新日曆。
createCalendar(name, options)Calendar建立使用者擁有的新日曆。
createEvent(title, startTime, endTime)CalendarEvent建立新活動。
createEvent(title, startTime, endTime, options)CalendarEvent建立新活動。
createEventFromDescription(description)CalendarEvent根據任意形式的說明建立活動。
createEventSeries(title, startTime, endTime, recurrence)CalendarEventSeries建立新的活動系列。
createEventSeries(title, startTime, endTime, recurrence, options)CalendarEventSeries建立新的活動系列。
getAllCalendars()Calendar[]取得使用者擁有或訂閱的所有日曆。
getAllOwnedCalendars()Calendar[]取得使用者擁有的所有日曆。
getCalendarById(id)Calendar取得具有指定 ID 的日曆。
getCalendarsByName(name)Calendar[]取得具備使用者擁有或訂閱名稱的所有日曆。
getColor()String取得日曆的顏色。
getDefaultCalendar()Calendar取得使用者的預設日曆。
getDescription()String取得日曆的說明。
getEventById(iCalId)CalendarEvent取得具有指定 ID 的事件。
getEventSeriesById(iCalId)CalendarEventSeries取得具有指定 ID 的事件系列。
getEvents(startTime, endTime)CalendarEvent[]取得在指定時間範圍內發生的所有事件。
getEvents(startTime, endTime, options)CalendarEvent[]取得在指定時間範圍內發生且符合指定條件的所有事件。
getEventsForDay(date)CalendarEvent[]取得在特定日期發生的所有事件。
getEventsForDay(date, options)CalendarEvent[]取得在指定日期發生且符合特定條件的所有事件。
getId()String取得日曆的 ID。
getName()String取得日曆的名稱。
getOwnedCalendarById(id)Calendar取得具備指定 ID 的日曆 (如果使用者擁有該日曆)。
getOwnedCalendarsByName(name)Calendar[]取得所有含特定使用者名稱的日曆。
getTimeZone()String取得日曆的時區。
isHidden()Boolean指定是否要在使用者介面中隱藏該日曆。
isMyPrimaryCalendar()Boolean判斷日曆是否為有效使用者的主要日曆。
isOwnedByMe()Boolean決定日曆是否歸你所有。
isSelected()Boolean決定是否要在使用者介面中顯示日曆中的活動。
newRecurrence()EventRecurrence建立新的週期物件,可用於建立事件重複規則。
setColor(color)Calendar設定日曆的顏色。
setDescription(description)Calendar設定日曆的說明。
setHidden(hidden)Calendar設定是否在使用者介面中顯示日曆。
setName(name)Calendar設定日曆的名稱。
setSelected(selected)Calendar設定是否要在使用者介面中顯示日曆的活動。
setTimeZone(timeZone)Calendar設定日曆的時區。
subscribeToCalendar(id)Calendar讓使用者以指定 ID 訂閱日曆 (如果使用者可以訂閱)。
subscribeToCalendar(id, options)Calendar讓使用者以指定 ID 訂閱日曆 (如果使用者可以訂閱)。

內容詳盡的說明文件

createAllDayEvent(title, date)

建立新的全天活動。

// Creates an all-day event for the moon landing and logs the ID.
var event = CalendarApp.getDefaultCalendar().createAllDayEvent('Apollo 11 Landing',
    new Date('July 20, 1969'));
Logger.log('Event ID: ' + event.getId());

參數

名稱類型說明
titleString活動的名稱。
dateDate事件日期 (只會使用日期;系統會忽略時間)。

回攻員

CalendarEvent:已建立的活動。

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createAllDayEvent(title, startDate, endDate)

建立可以橫跨多天的新全天活動。

// Creates an all-day event for the Woodstock festival (August 15th to 17th) and logs the ID.
var event = CalendarApp.getDefaultCalendar().createAllDayEvent('Woodstock Festival',
    new Date('August 15, 1969'),
    new Date('August 18, 1969'));
Logger.log('Event ID: ' + event.getId());

參數

名稱類型說明
titleString活動的名稱。
startDateDate活動開始的日期 (只會使用日期;系統會忽略該時間)。
endDateDate活動結束的日期 (只會使用日期;系統會忽略該時間)。結束日期不會包含此日期。

回攻員

CalendarEvent:已建立的活動。

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createAllDayEvent(title, startDate, endDate, options)

建立可以橫跨多天的新全天活動。

// Creates an all-day event for the Woodstock festival (August 15th to 17th) and logs the ID.
var event = CalendarApp.getDefaultCalendar().createAllDayEvent('Woodstock Festival',
    new Date('August 15, 1969'),
    new Date('August 18, 1969'),
    {location: 'Bethel, White Lake, New York, U.S.', sendInvites: true});
Logger.log('Event ID: ' + event.getId());

參數

名稱類型說明
titleString活動的名稱。
startDateDate活動開始的日期 (只會使用日期;系統會忽略該時間)。
endDateDate活動結束的日期 (只會使用日期;系統會忽略該時間)。結束日期不會包含此日期。
optionsObject指定進階參數的 JavaScript 物件,如下所示。

進階參數

名稱類型說明
descriptionString事件的說明。
locationString事件的位置。
guestsString應新增為訪客的電子郵件地址清單,以半形逗號分隔。
sendInvitesBoolean是否要傳送電子郵件邀請 (預設:false)。

回攻員

CalendarEvent:已建立的活動。

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createAllDayEvent(title, date, options)

建立新的全天活動。

// Creates an all-day event for the moon landing and logs the ID.
var event = CalendarApp.getDefaultCalendar().createAllDayEvent('Apollo 11 Landing',
    new Date('July 20, 1969'),
    {location: 'The Moon'});
Logger.log('Event ID: ' + event.getId());

參數

名稱類型說明
titleString活動的名稱。
dateDate事件日期 (只會使用日期;系統會忽略時間)。
optionsObject指定進階參數的 JavaScript 物件,如下所示。

進階參數

名稱類型說明
descriptionString事件的說明。
locationString事件的位置。
guestsString應新增為訪客的電子郵件地址清單,以半形逗號分隔。
sendInvitesBoolean是否要傳送電子郵件邀請 (預設:false)。

回攻員

CalendarEvent:已建立的活動。

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createAllDayEventSeries(title, startDate, recurrence)

建立新的全天活動系列。

// Creates an event series for a no-meetings day, taking place every Wednesday in 2013.
var eventSeries = CalendarApp.getDefaultCalendar().createAllDayEventSeries('No Meetings',
    new Date('January 2, 2013 03:00:00 PM EST'),
    CalendarApp.newRecurrence().addWeeklyRule()
        .onlyOnWeekday(CalendarApp.Weekday.WEDNESDAY)
        .until(new Date('January 1, 2014')));
Logger.log('Event Series ID: ' + eventSeries.getId());

參數

名稱類型說明
titleString系列活動的名稱
startDateDate系列中第一個事件的日期 (僅使用日期;系統會忽略時間)
recurrenceEventRecurrence系列活動的重複設定

回攻員

CalendarEventSeries:已建立活動系列

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createAllDayEventSeries(title, startDate, recurrence, options)

建立新的全天活動系列。

// Creates an event series for a no-meetings day, taking place every Wednesday in 2013.
var eventSeries = CalendarApp.getDefaultCalendar().createAllDayEventSeries('No Meetings',
    new Date('January 2, 2013 03:00:00 PM EST'),
    CalendarApp.newRecurrence().addWeeklyRule()
        .onlyOnWeekday(CalendarApp.Weekday.WEDNESDAY)
        .until(new Date('January 1, 2014')),
    {guests: 'everyone@example.com'});
Logger.log('Event Series ID: ' + eventSeries.getId());

參數

名稱類型說明
titleString系列活動的名稱
startDateDate系列中第一個事件的日期 (僅使用日期;系統會忽略時間)
recurrenceEventRecurrence系列活動的重複設定
optionsObject指定進階參數的 JavaScript 物件,如下所示

進階參數

名稱類型說明
descriptionString系列中事件的說明
locationString系列中事件的位置
guestsString應新增為系列活動的邀請對象的電子郵件地址清單 (以半形逗號分隔)
sendInvitesBoolean是否要傳送邀請電子郵件 (預設:false)

回攻員

CalendarEventSeries:已建立活動系列

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createCalendar(name)

建立使用者擁有的新日曆。

// Creates a new calendar named "Travel Plans".
var calendar = CalendarApp.createCalendar('Travel Plans');
Logger.log('Created the calendar "%s", with the ID "%s".',
    calendar.getName(), calendar.getId());

參數

名稱類型說明
nameString新日曆的名稱

回攻員

Calendar:新建立的日曆

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createCalendar(name, options)

建立使用者擁有的新日曆。

// Creates a new calendar named "Travel Plans" with a summary and color.
var calendar = CalendarApp.createCalendar('Travel Plans', {
  summary: 'A calendar to plan my travel schedule.',
  color: CalendarApp.Color.BLUE
});
Logger.log('Created the calendar "%s", with the ID "%s".',
    calendar.getName(), calendar.getId());

參數

名稱類型說明
nameString新日曆的名稱
optionsObject指定進階參數的 JavaScript 物件,如下所示

進階參數

名稱類型說明
locationString日曆的位置
summaryString日曆的說明
timeZoneString日曆要設為的時區,以「長」格式指定 (例如「America/New_York」,如 Joda.org 列出)
colorString十六進位顏色字串 ("#rrggbb") 或 CalendarApp.Colors 中的值
hiddenBoolean是否在使用者介面中隱藏日曆 (預設:false)
selectedBoolean是否在使用者介面中顯示日曆事件 (預設值:true)

回攻員

Calendar:新建立的日曆

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createEvent(title, startTime, endTime)

建立新事件。

如果未指定時區,系統會根據指令碼的時區解讀時間值,這可能會與日曆的時區不同。

// Creates an event for the moon landing and logs the ID.
var event = CalendarApp.getDefaultCalendar().createEvent('Apollo 11 Landing',
    new Date('July 20, 1969 20:00:00 UTC'),
    new Date('July 21, 1969 21:00:00 UTC'));
Logger.log('Event ID: ' + event.getId());

參數

名稱類型說明
titleString活動的名稱
startTimeDate事件開始的日期和時間
endTimeDate活動結束的日期和時間

回攻員

CalendarEvent:已建立的活動

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createEvent(title, startTime, endTime, options)

建立新事件。

如果未指定時區,系統會根據指令碼的時區解讀時間值,這可能會與日曆的時區不同。

// Creates an event for the moon landing and logs the ID.
var event = CalendarApp.getDefaultCalendar().createEvent('Apollo 11 Landing',
    new Date('July 20, 1969 20:00:00 UTC'),
    new Date('July 20, 1969 21:00:00 UTC'),
    {location: 'The Moon'});
Logger.log('Event ID: ' + event.getId());

參數

名稱類型說明
titleString活動的名稱
startTimeDate事件開始的日期和時間
endTimeDate活動結束的日期和時間
optionsObject指定進階參數的 JavaScript 物件,如下所示

進階參數

名稱類型說明
descriptionString事件的說明
locationString活動地點
guestsString應新增為訪客的電子郵件地址清單,以半形逗號分隔
sendInvitesBoolean是否要傳送邀請電子郵件 (預設:false)

回攻員

CalendarEvent:已建立的活動

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createEventFromDescription(description)

根據任意形式的說明建立活動。

說明應採用與 UI 的「快速新增」功能相同的格式。

// Creates a new event and logs its ID.
var event = CalendarApp.getDefaultCalendar()
    .createEventFromDescription('Lunch with Mary, Friday at 1PM');
Logger.log('Event ID: ' + event.getId());

參數

名稱類型說明
descriptionString形式不拘的活動說明

回攻員

CalendarEvent:已建立的活動

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createEventSeries(title, startTime, endTime, recurrence)

建立新的活動系列。

// Creates an event series for a team meeting, taking place every Tuesday and Thursday in 2013.
var eventSeries = CalendarApp.getDefaultCalendar().createEventSeries('Team Meeting',
    new Date('January 1, 2013 03:00:00 PM EST'),
    new Date('January 1, 2013 04:00:00 PM EST'),
    CalendarApp.newRecurrence().addWeeklyRule()
        .onlyOnWeekdays([CalendarApp.Weekday.TUESDAY, CalendarApp.Weekday.THURSDAY])
        .until(new Date('January 1, 2014')));
Logger.log('Event Series ID: ' + eventSeries.getId());

參數

名稱類型說明
titleString系列活動的名稱
startTimeDate系列中第一個活動開始的日期和時間
endTimeDate系列中第一個活動結束的日期和時間
recurrenceEventRecurrence系列活動的重複設定

回攻員

CalendarEventSeries:已建立活動系列

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

createEventSeries(title, startTime, endTime, recurrence, options)

建立新的活動系列。

// Creates an event series for a team meeting, taking place every Tuesday and Thursday in 2013.
var eventSeries = CalendarApp.getDefaultCalendar().createEventSeries('Team Meeting',
    new Date('January 1, 2013 03:00:00 PM EST'),
    new Date('January 1, 2013 04:00:00 PM EST'),
    CalendarApp.newRecurrence().addWeeklyRule()
        .onlyOnWeekdays([CalendarApp.Weekday.TUESDAY, CalendarApp.Weekday.THURSDAY])
        .until(new Date('January 1, 2014')),
    {location: 'Conference Room'});
Logger.log('Event Series ID: ' + eventSeries.getId());

參數

名稱類型說明
titleString系列活動的名稱
startTimeDate系列中第一個活動開始的日期和時間
endTimeDate系列中第一個活動結束的日期和時間
recurrenceEventRecurrence系列活動的重複設定
optionsObject指定進階參數的 JavaScript 物件,如下所示

進階參數

名稱類型說明
descriptionString系列中事件的說明
locationString系列中事件的位置
guestsString應新增為系列活動的邀請對象的電子郵件地址清單 (以半形逗號分隔)
sendInvitesBoolean是否要傳送邀請電子郵件 (預設:false)

回攻員

CalendarEventSeries:已建立活動系列

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

getAllCalendars()

取得使用者擁有或訂閱的所有日曆。

// Determines how many calendars the user can access.
var calendars = CalendarApp.getAllCalendars();
Logger.log('This user owns or is subscribed to %s calendars.',
    calendars.length);

回攻員

Calendar[]:使用者可存取的所有日曆

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getAllOwnedCalendars()

取得使用者擁有的所有日曆。

// Determines how many calendars the user owns.
var calendars = CalendarApp.getAllOwnedCalendars();
Logger.log('This user owns %s calendars.', calendars.length);

回攻員

Calendar[]:使用者擁有的所有日曆

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getCalendarById(id)

取得具有指定 ID 的日曆。

// Gets the public calendar "US Holidays" by ID.
var calendar = CalendarApp.getCalendarById(
    'en.usa#holiday@group.v.calendar.google.com');
Logger.log('The calendar is named "%s".', calendar.getName());

參數

名稱類型說明
idString日曆 ID

回攻員

Calendar:具有指定 ID 的日曆;如果日曆不存在、使用者無法存取,或是使用者尚未訂閱日曆,則傳回 null

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getCalendarsByName(name)

取得具備使用者擁有或訂閱名稱的所有日曆。名稱不區分大小寫。

// Gets the public calendar named "US Holidays".
var calendars = CalendarApp.getCalendarsByName('US Holidays');
Logger.log('Found %s matching calendars.', calendars.length);

參數

名稱類型說明
nameString日曆名稱

回攻員

Calendar[]:使用者可存取的所有日曆,並使用這個名稱

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getColor()

取得日曆的顏色。

// Opens the calendar by its ID.
// TODO(developer): Replace the ID with your own.
const calendar = CalendarApp.getCalendarById('222larabrown@gmail.com');

// Gets the color of the calendar and logs it to the console.
// For the default calendar, you can use CalendarApp.getColor() instead.
const calendarColor = calendar.getColor();
console.log(calendarColor);

回攻員

String — 十六進位顏色字串 (「#rrggbb」)。

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getDefaultCalendar()

取得使用者的預設日曆。

// Determines the time zone of the user's default calendar.
var calendar = CalendarApp.getDefaultCalendar();
Logger.log('My default calendar is set to the time zone "%s".',
    calendar.getTimeZone());

回攻員

Calendar:使用者的預設日曆

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getDescription()

取得日曆的說明。

// Opens the calendar by its ID.
// TODO(developer): Replace the ID with your own.
const calendar = CalendarApp.getCalendarById('222larabrown@gmail.com');

// Sets the description of the calendar to 'Test description.'
calendar.setDescription('Test description');

// Gets the description of the calendar and logs it to the console.
// For the default calendar, you can use CalendarApp.getDescription() instead.
const description = calendar.getDescription();
console.log(description);

回攻員

String:這個日曆的說明。

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getEventById(iCalId)

取得具有指定 ID 的事件。如果系列屬於預設日曆以外的日曆,則必須從該日曆呼叫這個方法。呼叫 getEventById(iCalId) 只會傳回預設日曆中的活動。

多個活動系列中的活動可以有相同的 ID。在這種情況下,這個方法只會傳回該系列中的第一個事件。

// Opens the calendar by its ID.
// TODO(developer): Replace the ID with your own.
const calendar = CalendarApp.getCalendarById('222larabrown@gmail.com')

// Creates an event for the moon landing.
const event = calendar.createEvent('Apollo 11 Landing',
  new Date('July 20, 1969 20:05:00 UTC'),
  new Date('July 20, 1969 20:17:00 UTC'));

// Gets the calendar event ID and logs it to the console.
const iCalId = event.getId();
console.log(iCalId);

// Gets the event by its ID and logs the title of the event to the console.
// For the default calendar, you can use CalendarApp.getEventById(iCalId) instead.
const myEvent = calendar.getEventById(iCalId);
console.log(myEvent.getTitle());

參數

名稱類型說明
iCalIdString活動的 ID。

回攻員

CalendarEvent:具有指定 ID 的事件;如果事件不存在或使用者無法存取,則為 null

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getEventSeriesById(iCalId)

取得具有指定 ID 的事件系列。如果指定 ID 適用於單一 CalendarEvent,則 CalendarEventSeries 會傳回,其中包含系列中的單一事件。請注意,如果活動系列屬於預設日曆以外的日曆,則必須從該 CalendarApp 呼叫此方法;呼叫 getEventSeriesById(iCalId) 只會傳回預設日曆中的活動系列。

// Opens the calendar by its ID.
// TODO(developer): Replace the ID with your own.
const calendar = CalendarApp.getCalendarById('222larabrown@gmail.com');

// Creates an event series for a daily team meeting from 1 PM to 2 PM.
// The series adds the daily event from January 1, 2023 through December 31, 2023.
const eventSeries = calendar.createEventSeries('Team meeting',
  new Date('Jan 1, 2023 13:00:00'),
  new Date('Jan 1, 2023 14:00:00'),
  CalendarApp.newRecurrence().addDailyRule().until(new Date('Jan 1, 2024')));

// Gets the ID of the event series.
const iCalId = eventSeries.getId();

// Gets the event series by its ID and logs the series title to the console.
// For the default calendar, you can use CalendarApp.getEventSeriesById(iCalId) instead.
console.log(calendar.getEventSeriesById(iCalId).getTitle());

參數

名稱類型說明
iCalIdString事件系列的 ID。

回攻員

CalendarEventSeries:具有指定 ID 的系列;如果系列叢書不存在或使用者無法存取,則為 null

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getEvents(startTime, endTime)

取得在指定時間範圍內發生的所有事件。

這個方法會傳回在指定時間範圍內開始、在這段時間內結束或涵蓋特定時間範圍的事件。如果沒有指定時區,系統就會根據指令碼的時區解讀時間值,這可能會與日曆的時區不同。

// Determines how many events are happening in the next two hours.
var now = new Date();
var twoHoursFromNow = new Date(now.getTime() + (2 * 60 * 60 * 1000));
var events = CalendarApp.getDefaultCalendar().getEvents(now, twoHoursFromNow);
Logger.log('Number of events: ' + events.length);

參數

名稱類型說明
startTimeDate時間範圍的開始時間
endTimeDate時間範圍的結束時間,不含這兩個數字

回攻員

CalendarEvent[]:在指定時間範圍內發生的事件

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getEvents(startTime, endTime, options)

取得在指定時間範圍內發生且符合指定條件的所有事件。

這個方法會傳回在指定時間範圍內開始、在該時間範圍內結束或涵蓋特定時間範圍的事件。如果沒有指定時區,系統就會根據指令碼的時區解讀時間值,這可能會與日曆的時區不同。

請注意,套用 startmax 後,系統會開始篩選 authorsearchstatusFilters。也就是說,即使其他事件符合條件,傳回的事件數量可能少於 max

// Determines how many events are happening in the next two hours that contain the term
// "meeting".
var now = new Date();
var twoHoursFromNow = new Date(now.getTime() + (2 * 60 * 60 * 1000));
var events = CalendarApp.getDefaultCalendar().getEvents(now, twoHoursFromNow,
    {search: 'meeting'});
Logger.log('Number of events: ' + events.length);

參數

名稱類型說明
startTimeDate時間範圍的開始時間
endTimeDate時間範圍的結束時間,不含這兩個數字
optionsObject指定進階參數的 JavaScript 物件,如下所示

進階參數

名稱類型說明
startInteger要傳回的第一個事件索引
maxInteger要傳回的事件數量上限
authorString活動建立者用來篩選結果的電子郵件地址
searchString用於篩選結果的全文搜尋查詢
statusFilters[]GuestStatus用來篩選結果的狀態陣列

回攻員

CalendarEvent[]:在特定時間範圍內發生且符合條件的事件

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getEventsForDay(date)

取得在特定日期發生的所有事件。

這個方法會傳回在指定日期開始、在當天結束或涵蓋整天的事件。

請注意,系統只會使用 Date 物件的日期部分,忽略時間部分。 系統會根據日曆時區,將日期解讀為當天午夜至次日午夜。

// Determines how many events are happening today.
var today = new Date();
var events = CalendarApp.getDefaultCalendar().getEventsForDay(today);
Logger.log('Number of events: ' + events.length);

參數

名稱類型說明
dateDate擷取事件的日期 (僅使用日期;系統會忽略該時間)

回攻員

CalendarEvent[]:在指定日期發生的事件

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getEventsForDay(date, options)

取得在指定日期發生且符合特定條件的所有事件。

這個方法會傳回在指定日期開始、在當天結束或涵蓋整天的事件。

請注意,系統只會使用 Date 物件的日期部分,忽略時間部分。 系統會根據日曆時區,將日期解讀為當天午夜至次日午夜。

請注意,套用 startmax 後,系統會開始篩選 authorsearchstatusFilters。也就是說,即使其他事件符合條件,傳回的事件數量可能少於 max

// Determines how many events are happening today and contain the term "meeting".
var today = new Date();
var events = CalendarApp.getDefaultCalendar().getEventsForDay(today, {search: 'meeting'});
Logger.log('Number of events: ' + events.length);

參數

名稱類型說明
dateDate擷取事件的日期 (僅使用日期;系統會忽略該時間)
optionsObject進階篩選選項

進階參數

名稱類型說明
startInteger要傳回的第一個事件索引
maxInteger要傳回的事件數量上限
authorString活動建立者用來篩選結果的電子郵件地址
searchString用於篩選結果的全文搜尋查詢
statusFilters[]GuestStatus用來篩選結果的狀態陣列

回攻員

CalendarEvent[]:在指定日期發生且符合條件的事件

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getId()

取得日曆的 ID。使用者的預設日曆 ID 即為他們的電子郵件地址。

// Opens the calendar by its ID.
// To get the user's default calendar, use CalendarApp.getDefaultCalendar().
// TODO(developer): Replace the ID with your own.
const calendar = CalendarApp.getCalendarById('222larabrown@gmail.com');

// Gets the ID of the calendar and logs it to the console.
const calendarId = calendar.getId();
console.log(calendarId);

回攻員

String:日曆的 ID。

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getName()

取得日曆的名稱。

// Opens the calendar by its ID.
// TODO(developer): Replace the ID with your own.
const calendar = CalendarApp.getCalendarById('222larabrown@gmail.com');

// Gets the name of the calendar and logs it to the console.
// For the default calendar, you can use CalendarApp.getName() instead.
const calendarName = calendar.getName();
console.log(calendarName);

回攻員

String:這個日曆的名稱。

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getOwnedCalendarById(id)

取得具備指定 ID 的日曆 (如果使用者是該 ID 的擁有者)。

如要找出日曆 ID,請在 Google 日曆中按一下該日曆名稱旁邊的箭頭,然後選取「日曆設定」。ID 會顯示在接近設定頁面底部。

// Gets a (non-existent) private calendar by ID.
var calendar = CalendarApp.getOwnedCalendarById(
    '123456789@group.calendar.google.com');
Logger.log('The calendar is named "%s".', calendar.getName());

參數

名稱類型說明
idString日曆 ID

回攻員

Calendar:具有指定 ID 的日曆;如果日曆不存在或使用者並非日曆擁有者,則傳回 null

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getOwnedCalendarsByName(name)

取得所有含特定使用者名稱的日曆。名稱不區分大小寫。

// Gets a private calendar named "Travel Plans".
var calendars = CalendarApp.getOwnedCalendarsByName('Travel Plans');
Logger.log('Found %s matching calendars.', calendars.length);

參數

名稱類型說明
nameString日曆名稱

回攻員

Calendar[]:使用者擁有的所有日曆 (名稱皆相同)

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

getTimeZone()

取得日曆的時區。

// Opens the calendar by its ID.
// TODO(developer): Replace the ID with your own.
const calendar = CalendarApp.getCalendarById('222larabrown@gmail.com');

// Gets the time zone of the calendar and logs it to the console.
// For the default calendar, you can use CalendarApp.getTimeZone() instead.
const timeZone = calendar.getTimeZone();
console.log(timeZone);

回攻員

String:以「長」格式指定的時區,例如「America/New_York」,如 Joda.org 列出。

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

isHidden()

指定是否要在使用者介面中隱藏該日曆。

// Opens the calendar by its ID.
// TODO(developer): Replace the ID with your own.
const calendar = CalendarApp.getCalendarById('222larabrown@gmail.com');

// Determines whether the calendar is hidden in the user interface and logs it to the console.
// For the default calendar, you can use CalendarApp.isHidden() instead.
const isHidden = calendar.isHidden();
console.log(isHidden);

回攻員

Boolean:如果日曆不會顯示在使用者介面中,則為 true;如果不是,則為 false

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

isMyPrimaryCalendar()

判斷日曆是否為有效使用者的主要日曆。

// Opens the calendar by its ID.
// TODO(developer): Replace the ID with your own.
const calendar = CalendarApp.getCalendarById('222larabrown@gmail.com');

// Determines whether the calendar is the default calendar for
// the effective user and logs it to the console.
// For the default calendar, you can use CalendarApp.isMyPrimaryCalendar() instead.
const isMyPrimaryCalendar = calendar.isMyPrimaryCalendar();
console.log(isMyPrimaryCalendar);

回攻員

Boolean — 如果日曆是有效使用者的預設日曆,則為 true;如果不是,則為 false

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

isOwnedByMe()

決定日曆是否歸你所有。

// Gets a calendar by its ID. To get the user's default calendar, use
// CalendarApp.getDefault() instead.
// TODO(developer): Replace the ID with the calendar ID that you want to use.
const calendar = CalendarApp.getCalendarById('abc123456@group.calendar.google.com');

// Determines whether the calendar is owned by you and logs it.
console.log(calendar.isOwnedByMe());

回攻員

Boolean:如果日曆為您所有,則為 true;如果不是日曆擁有者,請設為 false

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

isSelected()

指定是否要在使用者介面中顯示日曆中的活動。

// Gets the user's default calendar. To get a different calendar, use getCalendarById()
// instead.
const calendar = CalendarApp.getDefaultCalendar();

// Determines whether the calendar's events are displayed in the user interface and logs it.
console.log(calendar.isSelected());

回攻員

Booleantrue如果日曆的活動顯示在使用者介面中;false

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

newRecurrence()

建立新的週期物件,用來建立事件重複規則。

// Creates an event series for a no-meetings day, taking place every Wednesday in 2013.
var recurrence = CalendarApp.newRecurrence().addWeeklyRule()
    .onlyOnWeekday(CalendarApp.Weekday.WEDNESDAY)
    .until(new Date('January 1, 2014'));
var eventSeries = CalendarApp.getDefaultCalendar().createAllDayEventSeries('No Meetings',
    new Date('January 2, 2013 03:00:00 PM EST'),
    recurrence);
Logger.log('Event Series ID: ' + eventSeries.getId());

回攻員

EventRecurrence:沒有設定任何規則的新週期物件 (做為每週週期)

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.readonly
  • https://www.google.com/calendar/feeds

setColor(color)

設定日曆顏色。

// Opens the calendar by its ID.
// TODO(developer): Replace the ID with your own.
const calendar = CalendarApp.getCalendarById('222larabrown@gmail.com');

// Sets the color of the calendar to pink using the Calendar Color enum.
// For the default calendar, you can use CalendarApp.setColor() instead.
calendar.setColor(CalendarApp.Color.PINK);

參數

名稱類型說明
colorStringCalendarApp.Color 或十六進位顏色字串 (「#rrggbb」)。

回攻員

Calendar - 這個日曆用於鏈結。

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

setDescription(description)

設定日曆的說明。

// Gets the user's default calendar. To get a different calendar, use getCalendarById()
// instead.
const calendar = CalendarApp.getDefaultCalendar();

// Sets the description of the calendar.
// TODO(developer): Update the string with the description that you want to use.
calendar.setDescription('Updated calendar description.')

參數

名稱類型說明
descriptionString這個日曆的說明

回攻員

Calendar — 這個用於鏈結的日曆

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

setHidden(hidden)

設定是否在使用者介面中顯示日曆。

參數

名稱類型說明
hiddenBooleantrue:可在使用者介面中隱藏日曆;false 表示顯示

回攻員

Calendar — 這個用於鏈結的日曆

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

setName(name)

設定日曆名稱。

// Gets the user's default calendar. To get a different calendar, use getCalendarById()
// instead.
const calendar = CalendarApp.getDefaultCalendar();

// Sets the name of the calendar.
// TODO(developer): Update the string with the name that you want to use.
calendar.setName('Example calendar name');

參數

名稱類型說明
nameString新名稱

回攻員

Calendar — 這個用於鏈結的日曆

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

setSelected(selected)

設定是否要在使用者介面中顯示日曆的活動。

// Gets the user's default calendar. To get a different calendar, use getCalendarById()
// instead.
const calendar = CalendarApp.getDefaultCalendar();

// Selects the calendar so that its events are displayed in the user interface. To
// unselect the calendar, set the parameter to false.
calendar.setSelected(true);

參數

名稱類型說明
selectedBooleantrue:在使用者介面中顯示日曆的活動,false 則可隱藏日曆

回攻員

Calendar — 這個用於鏈結的日曆

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

setTimeZone(timeZone)

設定日曆的時區。

// Gets the user's default calendar. To get a different calendar, use getCalendarById()
// instead.
const calendar = CalendarApp.getDefaultCalendar();

// Sets the time zone of the calendar to America/New York (US/Eastern) time.
calendar.setTimeZone('America/New_York');

參數

名稱類型說明
timeZoneString時區,以「長」格式指定 (例如「America/New_York」,如 Joda.org 列出)。

回攻員

Calendar - 這個日曆用於鏈結。

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

subscribeToCalendar(id)

讓使用者以指定 ID 訂閱日曆 (如果使用者允許訂閱)。

// Subscribe to the calendar "US Holidays".
var calendar = CalendarApp.subscribeToCalendar(
    'en.usa#holiday@group.v.calendar.google.com');
Logger.log('Subscribed to the calendar "%s".', calendar.getName());

參數

名稱類型說明
idString要訂閱的日曆 ID

回攻員

Calendar:新訂閱的日曆

擲回

Error:如果沒有此 ID 的日曆

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds

subscribeToCalendar(id, options)

讓使用者以指定 ID 訂閱日曆 (如果使用者允許訂閱)。

// Subscribe to the calendar "US Holidays", and set it to the color blue.
var calendar = CalendarApp.subscribeToCalendar(
    'en.usa#holiday@group.v.calendar.google.com',
    { color: CalendarApp.Color.BLUE });
Logger.log('Subscribed to the calendar "%s".', calendar.getName());

參數

名稱類型說明
idString要訂閱的日曆 ID。
optionsObject指定進階參數的 JavaScript 物件,如下所示。

進階參數

名稱類型說明
colorString十六進位顏色字串 ("#rrggbb") 或 CalendarApp.Colors 中的值。
hiddenBoolean是否在使用者介面中隱藏日曆 (預設:false)。
selectedBoolean日曆的事件是否會顯示在使用者介面中 (預設值:如果同時指定 color,則預設值為 true,否則為 false)。

回攻員

Calendar:新訂閱的日曆。

擲回

Error:如果沒有此 ID 的日曆

授權

使用這個方法的指令碼需要具備下列一或多個範圍的授權,或相關 REST API 的適當範圍:

  • https://www.googleapis.com/auth/calendar
  • https://www.google.com/calendar/feeds