【问题标题】:Need help in Google App Script for Creating event in Google Calendar在 Google 应用脚本中需要帮助以在 Google 日历中创建事件
【发布时间】:2021-03-27 05:08:35
【问题描述】:

我在创建 google 应用程序脚本以在 Google 日历中创建事件方面需要帮助,遵循我正在使用的代码,但颜色设置出现错误“无法读取属性 'setColor' of null”。

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

请帮忙。

【问题讨论】:

  • 你会用 Excel 做这个吗?如果不是,为什么要标记它??
  • 我的回答是否向您展示了您想要的结果?你能告诉我吗?这对我学习也很有用。如果这可行,与您有相同问题的其他人也可以将您的问题作为可以解决的问题。如果您对我的回答有疑问,我深表歉意。到时候,可以问一下你现在的情况吗?我想学习解决你的问题。

标签: javascript google-apps-script google-sheets google-calendar-api


【解决方案1】:

修改点:

  • 当我看到您的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");
}

或者,您也可以使用以下对象修改脚本。

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.
  • 如果要使用事件ID,可以使用CalendarApp.getCalendarById("rhse6nvlklu7n2pm86jaiuqsis@group.calendar.google.com").getEventById({eventId}).setColor("1");
  • 关于事件颜色,你也可以像CalendarApp.EventColor.BLUE一样使用。 Ref

参考资料:

【讨论】:

    猜你喜欢
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多