Last active
March 28, 2024 05:31
-
-
Save htlin222/4eabe6165b838612da91baebef8ecb69 to your computer and use it in GitHub Desktop.
batch_add_event_from_gsheet_to_gcalendar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 當檔案打開時執行 | |
function onOpen() { | |
var ui = SpreadsheetApp.getUi(); // 獲取當前的用戶界面實例 | |
var menu = ui.createMenu('處理'); // 創建一個新菜單 | |
var calendarDict = getValuesAsDict(); // 從工作表獲取日曆ID和名稱對應關係 | |
var counter = 2; // 計數器,用於生成函式名稱 | |
// 遍歷所有日曆名稱和ID | |
for (var name in calendarDict) { | |
var id = calendarDict[name]; | |
// 創建一個特定的函式名稱,例如 getCalendarListB2、getCalendarListB3 等 | |
var functionName = 'getCalendarListB' + counter; | |
// 在菜單中新增一個項目,點擊後調用對應函式 | |
menu.addItem('新增到日曆 ' + name, functionName); | |
counter++; | |
} | |
// 把菜單加到用戶界面中 | |
menu.addToUi(); | |
} | |
// 將事件添加到指定的日曆 | |
function addEventsToCalendar(calendarId) { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // 獲取當前激活的工作表 | |
var calendarId = "[email protected]"; // 指定日曆ID,這裡應該用參數替代 | |
var sheetName = sheet.getName(); // 獲取工作表名稱 | |
// 檢查是否在 'calendar_list' 工作表上 | |
if(sheetName === "calendar_list") { | |
Browser.msgBox("Warning", "You are currently on the calendar_list sheet.", Browser.Buttons.OK); | |
return; // 如果是,則顯示警告並結束函數執行 | |
} | |
var calendar = CalendarApp.getCalendarById(calendarId); // 根據ID獲取日曆 | |
var rows = sheet.getDataRange().getValues(); // 獲取工作表上的所有數據 | |
// 遍歷每一行數據 | |
for (var i = 1; i < rows.length; i++) { | |
var row = rows[i]; | |
var added = row[0]; // 是否已添加標記 | |
var title = sheetName + " " + row[1]; // 事件標題 | |
// 檢查是否已添加和標題是否為空 | |
if (added.toString().toLowerCase() !== 'true' && row[1].trim() !== '') { | |
var content = row[2]; // 事件內容 | |
// 獲取時間和日期值 | |
var timeCellValue = sheet.getRange(i + 1, 4).getValue(); // 從第4列獲取時間 | |
var dateCellValue = sheet.getRange(i + 1, 5).getValue(); // 從第5列獲取日期 | |
var timeAsDate = new Date(timeCellValue); | |
var dateAsDate = new Date(dateCellValue); | |
// 格式化時間和日期 | |
var formattedTime = Utilities.formatDate(timeAsDate, Session.getScriptTimeZone(), 'HHmm').toString(); | |
var formattedDate = Utilities.formatDate(dateAsDate, Session.getScriptTimeZone(), 'YYYY-MM-dd').toString(); | |
var duration = row[5]; // 事件持續時間 | |
var timeString = formattedTime; | |
var dateString = formattedDate; | |
// 解析日期和時間,準備創建事件 | |
var hours = parseInt(timeString.substring(0, 2), 10); | |
var minutes = parseInt(timeString.substring(2, 4), 10); | |
var dateParts = dateString.split('-'); | |
var year = parseInt(dateParts[0], 10); | |
var month = parseInt(dateParts[1], 10) - 1; // 月份從0開始計數 | |
var day = parseInt(dateParts[2], 10); | |
var startTime = new Date(year, month, day, hours, minutes); | |
var endTime = new Date(startTime.getTime() + duration * 3600000); // 計算結束時間 | |
// 創建事件 | |
var event = calendar.createEvent(title, startTime, endTime, {description: content}); | |
// 將 'Added' 欄位設置為 'TRUE' | |
sheet.getRange(i + 1, 1).setValue('TRUE'); | |
} | |
} | |
} | |
// 從 'calendar_list' 工作表獲取日曆名稱和ID,並返回一個字典 | |
function getValuesAsDict() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("calendar_list"); // 獲取指定的工作表 | |
if (!sheet) { | |
Logger.log("找不到名為 'calendar_list' 的工作表。"); | |
return; | |
} | |
var rangeA = sheet.getRange("A2:A" + sheet.getLastRow()); // 獲取A列的數據範圍 | |
var valuesA = rangeA.getValues(); // 獲取A列的所有值 | |
var dict = {}; // 創建空字典 | |
for (var i = 0; i < valuesA.length; i++) { | |
var name = valuesA[i][0]; | |
if (name === "" || name === undefined) break; | |
var id = sheet.getRange("B" + (i + 2)).getValue(); | |
dict[name] = id; // 將名稱和ID添加到字典 | |
} | |
return dict; // 返回字典 | |
} | |
// 下面幾個函數是為不同的日曆ID調用 addEventsToCalendar 函數 | |
// 每個函數對應不同的日曆ID,從 'calendar_list' 工作表獲取對應的值 | |
function getCalendarListB2() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("calendar_list"); | |
var value = sheet.getRange("B2").getValue(); | |
if (value === "") { | |
return getCalendarListB2(); // 如果B2為空,則重新調用自己 | |
} | |
addEventsToCalendar(value); // 調用添加事件函數 | |
} | |
function getCalendarListB3() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("calendar_list"); | |
var value = sheet.getRange("B3").getValue(); | |
if (value === "") { | |
return getCalendarListB3(); // 如果B3為空,則重新調用自己 | |
} | |
addEventsToCalendar(value); | |
} | |
function getCalendarListB4() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("calendar_list"); | |
var value = sheet.getRange("B4").getValue(); | |
if (value === "") { | |
return getCalendarListB4(); // 如果B4為空,則重新調用自己 | |
} | |
addEventsToCalendar(value); | |
} | |
function getCalendarListB5() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("calendar_list"); | |
var value = sheet.getRange("B5").getValue(); | |
if (value === "") { | |
return getCalendarListB5(); // 如果B5為空,則重新調用自己 | |
} | |
addEventsToCalendar(value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
說明
安裝
calendar_list
裡貼上你的google calendar id 詳見 How to find your Google Calendar ID特色
🥟 我的標題