简体   繁体   English

在 Google 应用脚本中需要帮助以在 Google 日历中创建事件

[英]Need help in Google App Script for Creating event in Google Calendar

I need help in Creating a google apps script to create events in Google Calendar, following the code I'm using but gives the error "Cannot read property 'setColor' of null" for color settings.我需要帮助创建一个谷歌应用程序脚本以在谷歌日历中创建事件,遵循我正在使用的代码,但给出了错误“无法读取 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

please help.请帮忙。

Modification points:修改点:

  • When I saw your script of var calendar = CalendarApp.getCalendarById("rhse6nvlklu7n2pm86jaiuqsis@group.calendar.google.com").createEvent(title, startTime, endTime, {description: description, location: location, guests: guests, sendInvites: sendInvites }).getId();当我看到你的var calendar = CalendarApp.getCalendarById("rhse6nvlklu7n2pm86jaiuqsis@group.calendar.google.com").createEvent(title, startTime, endTime, {description: description, location: location, guests: guests, sendInvites: sendInvites }).getId(); , the event is created to the calendar except for the default calendar. , 事件被创建到除默认日历之外的日历。

  • The official document of getEventById(iCalId) of Class Class CalendarApp says as follows. Class Class CalendarApp的getEventById(iCalId)的官方文档说如下。

    Gets the event with the given ID.获取具有给定 ID 的事件。 If the series belongs to a calendar other than the default calendar, this method must be called from that CalendarApp .如果系列属于默认日历以外的日历,则必须从该CalendarApp调用此方法。 Calling getEventById(iCalId) only returns an event in the default calendar.调用getEventById(iCalId)仅返回默认日历中的事件。

    • I think that this is the reason of your issue.我认为这是您的问题的原因。
  • In this case, it is required to retrieve the event from the specific calendar.在这种情况下,需要从特定日历中检索事件。

    • In your script, I think that the event object of the created event can be directly used for setColor() .在您的脚本中,我认为创建事件的事件 object 可以直接用于setColor()
  • And also, in your script, the event color IDs of 10, 11, 12, 13 are used.此外,在您的脚本中,使用了10, 11, 12, 13的事件颜色 ID。 But in the current stage, the event color is from 1 to 11. Please be careful this.但是在当前阶段,事件颜色是从1到11。请注意这一点。

  • In your script, your each if statement is independence.在您的脚本中,您的每个 if 语句都是独立的。 So you can use else .所以你可以使用else

When above points are reflected to your script, it becomes as follows.当以上几点反映到您的脚本时,它变成如下。

Modified script:修改后的脚本:

From: 从:
 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");
To: 至:
 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"); }

Or, you can also modify your script using an object as follows.或者,您也可以使用 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.
  • If you want to use the event ID, you can use CalendarApp.getCalendarById("rhse6nvlklu7n2pm86jaiuqsis@group.calendar.google.com").getEventById({eventId}).setColor("1");如果要使用事件 ID,可以使用CalendarApp.getCalendarById("rhse6nvlklu7n2pm86jaiuqsis@group.calendar.google.com").getEventById({eventId}).setColor("1"); . .
  • About the event color, you can also use it like CalendarApp.EventColor.BLUE .关于事件颜色,您也可以像CalendarApp.EventColor.BLUE一样使用它。 Ref 参考

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Apps 脚本和 Google 表格,创建日历事件 - Apps Script and Google Sheets, Creating calendar event 需要帮助来创建Google表格脚本或公式 - Need help creating a google sheets script or formula Google App 脚本 - 添加或更新日历事件 - Google App Script - Add or Update Calendar Event 使用 Google 表格脚本创建 Google 日历事件有错误 - Creating a Google Calendar Event with Google Sheets script has Errors 使用 App 脚本从 Google 表格添加或更新 Google 日历事件 - Add or Update Google Calendar Event from Google Sheet with App Script Google Apps脚本-createEvent函数未创建日历事件 - Google Apps Script - createEvent function not creating calendar event (Google脚本)在创建Google表单时需要帮助以弄清楚如何减少对Google Script服务的调用 - (Google Script) Need Help Figuring Out How To Reduce Calls to Google Script Services in Creating Google Forms 谷歌应用脚​​本从 2 个可能的日历中删除事件 - Google app script delete event from 2 possible calendar 在谷歌表中需要帮助 - 脚本 - Need help in google sheet - script SyntaxError: missing ) 在我创建从 GoogleAppsScript 到 Google 日历的事件时,在 google 脚本中的参数列表之后 - SyntaxError: missing ) after argument list in google script while i was creating event from GoogleAppsScript to Google Calendar
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM