[英]Need help in Google App Script for Creating event in Google Calendar
我需要幫助創建一個谷歌應用程序腳本以在谷歌日歷中創建事件,遵循我正在使用的代碼,但給出了錯誤“無法讀取 null 的屬性'setColor'”用於顏色設置。
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Test jun")
var index = 2;
var lastRow = sheet.getLastRow();
for (; index <= lastRow; index++) {
var title = sheet.getRange(index, 1, 1, 1).getValue();
var startTime = sheet.getRange(index, 2, 1, 1).getValue();
var endTime = sheet.getRange(index, 3, 1, 1).getValue();
var description = sheet.getRange(index, 4, 1, 1).getValue();
var location = sheet.getRange(index, 5, 1, 1).getValue();
var guests = sheet.getRange(index, 6, 1, 1).getValue();
var eventColour = sheet.getRange(index, 7, 1, 1).getValue();
var sendInvites = true;
var calendar = CalendarApp.getCalendarById("rhse6nvlklu7n2pm86jaiuqsis@group.calendar.google.com").createEvent(title, startTime, endTime,
{ description: description, location: location, guests: guests, sendInvites: sendInvites }).getId();
if (eventColour === 'Condition1') CalendarApp.getEventById(calendar).setColor("10")
if (eventColour === "Condition2") CalendarApp.getEventById(calendar).setColor("11");
if (eventColour === "Condition3") CalendarApp.getEventById(calendar).setColor("12");
if (eventColour === "Condition4") CalendarApp.getEventById(calendar).setColor("13");
}// End of For Loop
}// End of Function
請幫忙。
當我看到你的var calendar = CalendarApp.getCalendarById("rhse6nvlklu7n2pm86jaiuqsis@group.calendar.google.com").createEvent(title, startTime, endTime, {description: description, location: location, guests: guests, sendInvites: sendInvites }).getId();
, 事件被創建到除默認日歷之外的日歷。
Class Class CalendarApp的getEventById(iCalId)
的官方文檔說如下。
獲取具有給定 ID 的事件。 如果系列屬於默認日歷以外的日歷,則必須從該
CalendarApp
調用此方法。 調用getEventById(iCalId)
僅返回默認日歷中的事件。
在這種情況下,需要從特定日歷中檢索事件。
setColor()
。 此外,在您的腳本中,使用了10, 11, 12, 13
的事件顏色 ID。 但是在當前階段,事件顏色是從1到11。請注意這一點。
在您的腳本中,您的每個 if 語句都是獨立的。 所以你可以使用else
。
當以上幾點反映到您的腳本時,它變成如下。
var calendar = CalendarApp.getCalendarById("rhse6nvlklu7n2pm86jaiuqsis@group.calendar.google.com").createEvent(title, startTime, endTime, {description: description, location: location, guests: guests, sendInvites: sendInvites }).getId(); if (eventColour === 'Condition1') CalendarApp.getEventById(calendar).setColor("10") if (eventColour === "Condition2") CalendarApp.getEventById(calendar).setColor("11"); if (eventColour === "Condition3") CalendarApp.getEventById(calendar).setColor("12"); if (eventColour === "Condition4") CalendarApp.getEventById(calendar).setColor("13");
至:
var event = CalendarApp.getCalendarById("rhse6nvlklu7n2pm86jaiuqsis@group.calendar.google.com").createEvent(title, startTime, endTime, { description: description, location: location, guests: guests, sendInvites: sendInvites }); if (eventColour === 'Condition1') { event.setColor("1"); } else if (eventColour === "Condition2") { event.setColor("2"); } else if (eventColour === "Condition3") { event.setColor("3"); } else if (eventColour === "Condition4") { event.setColor("4"); }
或者,您也可以使用 object 修改腳本,如下所示。
var obj = {"Condition1": "1", "Condition2": "2", "Condition3": "3", "Condition4": "4"}; CalendarApp.getCalendarById("rhse6nvlklu7n2pm86jaiuqsis@group.calendar.google.com").createEvent(title, startTime, endTime, { description: description, location: location, guests: guests, sendInvites: sendInvites }).setColor(obj[eventColour] || "11"); // In this case, when "eventColour" is not included in "obj", "11" of event color is used.
CalendarApp.getCalendarById("rhse6nvlklu7n2pm86jaiuqsis@group.calendar.google.com").getEventById({eventId}).setColor("1");
.CalendarApp.EventColor.BLUE
一樣使用它。 參考
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.