我有以下腳本,用于將資料從谷歌表格發送到谷歌日歷,以便它在谷歌日歷中創建事件,但我現在需要根據用戶選擇對代碼進行顏色編碼。
在作業表(下面的螢屏截圖)中,“e”列將選擇顏色(或顏色代碼)。有沒有辦法添加到我當前的腳本中,以便根據“e”列中的選擇為每個事件添加顏色?

function create_Events() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Timesheet");
var vs = sh.getRange("A1:E" sh.getLastRow()).getValues();
var cal = CalendarApp.getCalendarById("John@email");
vs.forEach(([t, s, e, d]) => {
cal.createEvent(t, new Date(s), new Date(e), { description: d});
});
}
uj5u.com熱心網友回復:
修改點:
- 不幸的是,從您顯示的示例電子表格中,我認為十六進制顏色不能直接用于設定事件顏色。在這種情況下,請使用Enum EventColor。
- 為了從“E”列中檢索值,請修改
vs.forEach(([t, s, e, d]) => {為vs.forEach(([t, s, e, d, color]) => {。這樣,color可以用作“E”列中的值。
當這些點反映在你的腳本中時,下面的修改怎么樣?
修改后的腳本:
在使用此腳本之前,請將“E”列的值從十六進制值修改為Enum EventColor。就像PALE_BLUE, PALE_GREEN, MAUVE, 等等。這樣,使用檢索到的值 和setColor(CalendarApp.EventColor[color.toUpperCase()]),將事件顏色設定為創建的事件。
function create_Events() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Timesheet");
var vs = sh.getRange("A1:E" sh.getLastRow()).getValues();
var cal = CalendarApp.getCalendarById("[email protected]");
vs.forEach(([t, s, e, d, color]) => {
cal.createEvent(t, new Date(s), new Date(e), { description: d }).setColor(CalendarApp.EventColor[color.toUpperCase()]);
});
}
筆記:
- 當出現類似錯誤
Exception: Invalid argument: color時,請再次檢查“E”列的值。
參考:
- 設定顏色(顏色)
- 列舉事件顏色
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516879.html
