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設定日曆的時區,以「長」格式指定 (例如Joda.org 列出的「America/New_York」
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,請在 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:以「long」格式指定的時區 (例如 Joda.org 列出的「America/New_York」)。

授權

使用這個方法的指令碼必須取得下列一或多個範圍 (或相關的 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);

回攻員

Booleantrue 如果在使用者介面中隱藏日曆,則為 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);

回攻員

Booleantrue 表示該日曆是有效使用者的預設日曆;如果不是有效使用者的預設日曆,則為 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以「long」格式指定的時區 (例如 Joda.org 列出的「America/New_York」)。

回攻員

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是否要在使用者介面中顯示日曆活動 (預設值:true)

回攻員

Calendar:新訂閱的日曆

擲回

Error:如果沒有使用這個 ID 的日曆

授權

使用這個方法的指令碼必須取得下列一或多個範圍 (或相關的 REST API) 的適當範圍授權:

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