Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Apps Script create calendar events
<b>英文名:</b> <?= enTitle ?>
<br><b>中文名:</b> <?= zhTitle ?>
var CALENDAR_ID = 'xxxxxxxxxx@group.calendar.google.com'
/**
* Create Google Calendar events.
*/
function createCalendarEvents() {
var holidays = extractHolidays()
var template = HtmlService.createTemplateFromFile('Calendar Template');
holidays.forEach(function (holiday) {
var title = holiday.zhTitle
template.zhTitle = holiday.zhTitle
template.enTitle = holiday.enTitle
var html = template.evaluate().getContent()
CalendarApp.getCalendarById(CALENDAR_ID).createAllDayEvent(title, holiday.date, {
description: html
})
})
}
function extractHolidays() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Holidays')
var values = sheet.getRange(2, 1, sheet.getMaxRows() - 1, 3).getValues()
var results = []
for (var row in values) {
var rowValues = values[row]
// early termination if empty row is found
if (rowValues[0] === '' || rowValues[1] === '' || rowValues[2] === '') {
break
}
results.push({
zhTitle: rowValues[0],
enTitle: rowValues[1],
date: rowValues[2],
})
}
return results
}
function listAllCalendars() {
var calendars = CalendarApp.getAllCalendars();
calendars.forEach(function (c) {
Logger.log(c.getName() + " >>> " + c.getId())
})
}
中文標題 英文標題 日期
元旦 New Year's Day 1/1/2019
農曆年初一 Lunar New Year's Day 2/5/2019
農曆年初二 The second day of Lunar New Year 2/6/2019
農曆年初三 The third day of Lunar New Year 2/7/2019
清明節 Ching Ming Festival 4/5/2019
耶穌受難節 Good Friday 4/19/2019
耶穌受難節翌日 The day following Good Friday 4/20/2019
復活節星期一 Easter Monday 4/22/2019
勞動節 Labour Day 5/1/2019
佛誕翌日 The day following the Birthday of the Buddha 5/13/2019
端午節 Tuen Ng Festival 6/7/2019
香港特別行政區成立紀念日 HKSAR Establishment Day 7/1/2019
中秋節翌日 The day following the Chinese Mid-Autumn Festival 9/14/2019
國慶日 National Day 10/1/2019
重陽節 Chung Yeung Festival 10/7/2019
聖誕節 Christmas Day 12/25/2019
聖誕節後第一個周日 The first weekday after Christmas Day 12/26/2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.