【问题标题】:Create Calendar Events to Calendars identified in Google Sheets为 Google 表格中标识的日历创建日历事件
【发布时间】:2022-06-20 15:12:17
【问题描述】:

**我是新手,对 Apps 脚本缺乏经验。 我正在为我们的高中开发一个设施请求系统。教练、教师等可以填写表格来请求/预订我们校园内多个地点的练习或活动。我正在尝试从 Google 表格创建 Google 日历事件,其中日历名称由 Google 表单/表格(D 列)中选择的位置标识。例如,篮球教练会向主健身房请求日期和时间(持续时间),并且将在主健身房日历上创建日历事件。我已经创建并拥有了多个日历。 我已经观看了几个 YouTube 视频并尝试了一些示例脚本,但对我所需要的没有任何帮助。任何帮助将不胜感激。

我认为我的代码中有几个错误。 [这是电子表格的副本。][1]

        function createCalendarEvent() {
  let spreadsheet=SpreadsheetApp.getActiveSheet();
  let request=sheet.getDataRange().getValues();
  request.splice(0,1);
  let getOwnedCalendarsByName=Spreadsheet.getDataRange(4);
  let athleticsCalendar=CalendarApp.getOwnedCalendarsByName()
  
  

 request.forEach(function(entry){
 
 athleticsCalendar.creat(entry[2],entry[6],entry[8]);
  });

}


  [1]: https://docs.google.com/spreadsheets/d/1zdevdFr8R0xmQNoO3wveLgty189PrFVTlTe-HpJUfGE/edit?usp=sharing

【问题讨论】:

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


    【解决方案1】:

    下面的代码应该可以工作,但我没有测试它。对于您的工作方式,您需要通过 ID 获取日历,然后您才能在该日历下创建活动。

    首先,您需要删除标题行:

      request.slide(1)
    

    其次,您可以创建保存日历 ID 的变量,以便稍后在循环中使用它们:

      let aux_gym_calendar_id = ""
      let main_soccer_field_calendar_id = ""
      ...
    

    三、switch语句在指定日历下创建事件。您应该使用getCalendarById 而不是getOwnedCalendarsByName,因为getCalendarById 返回一个日历实例,您可以使用它来创建事件。

      switch (calendarsName) {
        case "Aux Gym":
         cal = CalendarApp.getCalendarById(aux_gym_calendar_id)
         cal.createEvent(entry[2], entry[6], entry[8])
         break;
        case "Main Soccer Field":
         cal = CalendarApp.getCalendarById(main_soccer_field_id)
         cal.createEvent(entry[2], entry[6], entry[8])
      }   
    

    这里是完整的代码:

     function createCalendarEvent() {
      let sheet = SpreadsheetApp.getActiveSheet();
      let request = sheet.getDataRange().getValues()
    
      //remove header row
      request = request.slice(1)
    
      let cal = ""
     //Declare and init calendar id variables
      let aux_gym_calendar_id = ""
      let main_soccer_field_id = ""
    
      request.forEach(function (entry) {
       //get the calendar name
       let calendarsName = entry[3];
    
      //create event on selected calendar
      switch (calendarsName) {
        case "Aux Gym":
         cal = CalendarApp.getCalendarById(aux_gym_calendar_id)
         cal.createEvent(entry[2], entry[6], entry[8])
         break;
        case "Main Soccer Field":
         cal = CalendarApp.getCalendarById(main_soccer_field_id)
         cal.createEvent(entry[2], entry[6], entry[8])
      }
      });
    
      }
    

    资源:https://developers.google.com/apps-script/reference/calendar/calendar-app#getOwnedCalendarById(String)

    【讨论】:

    • 感谢两位的回复。我运行了你的代码并得到了这个错误:9:00:49 AM Error TypeError: Cannot read property 'createEvent' of null (anonymous) @ Code.gs:21 createCalendarEvent @ Code.gs:13
    【解决方案2】:

    只是为了分享我为选择多个日历 ID 所做的事情。经过大量搜索(我自己不是程序员),我发现使用 if 也可以完成工作。

    var calendarId = " ";
    var typeId = 2; //define column which data to be use to select calendar id (in your case, "location" column 4)
    var startDtId = 4;
    var endDtId = 5;
    

    然后,像这样插入 if else 语句

    function selectCalendarID() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var rows = sheet.getDataRange();
      var numRows = rows.getNumRows();
      var values = rows.getValues();
      var lr = rows.getLastRow();
    
      var startDt = sheet.getRange(lr, startDtId, 1, 1).getValue();
      var endDt = sheet.getRange(lr, endDtId, 1, 1).getValue();
      var type = sheet.getRange(lr, typeId, 1, 1).getValue();
        //select calendar id based on schedule type
        if(type == "OL 1" || type == "OL 2") {
        calendarId = "calId1@group.calendar.google.com"; 
      } else if (type == "BK 1" || type == "BK 2") {
        calendarId = "calId2@group.calendar.google.com"; 
      } else if (type == "GT 1" || type == "GT 2") {
        calendarId = "calId3@group.calendar.google.com";
      }
      createAllDayEvent(calendarId,startDt,endDt);
    }
    }
    
    function createAllDayEvent(calendarId, startDt, endDt) {
      var cal = CalendarApp.getCalendarById(calendarId);
      var start = new Date(startDt);
      var end = new Date(endDt);
      end.setDate(end.getDate() + 1);
      var type = type;
      var event = cal.createAllDayEvent(start, end, {
        }); 
    }
    

    希望有帮助

    P/S 从这里得到一个想法 How to change Google Calendar embed event color? 在这里https://www.considerednormal.com/2014/04/adding-a-google-calendar-event-using-google-forms/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 2023-01-19
      相关资源
      最近更新 更多