创建日历事件到Google表格中标识的日历

发布于 2025-02-04 14:50:08 字数 2824 浏览 1 评论 0 原文

**我非常新,并且没有应用程序脚本。 / 我正在为高中的设施请求系统。教练,老师等可以为我们校园周围多个地点的请求/保留惯例或活动填写表格。我正在尝试从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

Here is the code I am using right now.  I am getting the following errors:

12:07:17 PM Error   
ReferenceError: Title is not defined
createEvent @ Code.gs:32
selectCalendarID    @ Code.gs:26



    var calendarId = " ";
var typeId = 4; //define column which data to be use to select calendar id (in your case, "location" column 4)
var title = 3
var startDtId = 7;
var endDtId = 9;
 
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 == "Aux Gym" || type == "OL 2") {
    calendarId = "[email protected]"; 
  } else if (type == "Main Soccer Field" || type == "BK 2") {
    calendarId = "[email protected]"; 
  } else if (type == "GT 1" || type == "GT 2") {
    calendarId = "[email protected]";
  }
  createEvent(calendarId,title,startDt,endDt);
}
 
 
function createEvent(calendarId, title, startDt, endDt) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var title = new Title(title);
  var start = new Date(startDtId);
  var end = new Date(endDtId);
  end.setDate(end.getDate() + 1);
  var type = type;
  var event = cal.createEvent(title,start, end, {
    }); 
}

**I am very new and inexperienced with Apps Script. /
I am working on a facilities request system for our high school. Coaches, teachers, etc, can fill out a form to request/reserve practices or events for multiple locations around our campus. I am trying to create Google Calendar events from Google Sheets where the calendar name is identified by the location chosen in the Google Form/Sheet (Column D). For example, the basketball coach would request the Main Gym for a date and time (duration) and a calendar event would be created on the Main Gym calendar. There are multiple calendars that are already created and owned by me.
I have watched several YouTube videos and tried some sample scripts but none help with exactly what I need. Any help would be appreciated.

I think I have several errors in the code. [Here is a copy of the spreadsheet.][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

Here is the code I am using right now.  I am getting the following errors:

12:07:17 PM Error   
ReferenceError: Title is not defined
createEvent @ Code.gs:32
selectCalendarID    @ Code.gs:26



    var calendarId = " ";
var typeId = 4; //define column which data to be use to select calendar id (in your case, "location" column 4)
var title = 3
var startDtId = 7;
var endDtId = 9;
 
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 == "Aux Gym" || type == "OL 2") {
    calendarId = "[email protected]"; 
  } else if (type == "Main Soccer Field" || type == "BK 2") {
    calendarId = "[email protected]"; 
  } else if (type == "GT 1" || type == "GT 2") {
    calendarId = "[email protected]";
  }
  createEvent(calendarId,title,startDt,endDt);
}
 
 
function createEvent(calendarId, title, startDt, endDt) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var title = new Title(title);
  var start = new Date(startDtId);
  var end = new Date(endDtId);
  end.setDate(end.getDate() + 1);
  var type = type;
  var event = cal.createEvent(title,start, end, {
    }); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你拒绝所有暧昧 2025-02-11 14:50:08

下面的代码应该工作,但我没有测试。在您的工作方法中,您将需要通过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])
  }
  });

  }

资源:

The code below should work but I didn't test it. Fo your approach to work, you will need to get the calendar by ID then you will be able to create the event under that calendar.

First, you will need to remove the header row:

  request.slide(1)

second, you can create variables that hold the calendar id so you can use them later in the loop:

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

Third, the switch statement to create the event under the designated calendar. You should use getCalendarById instead of getOwnedCalendarsByName since getCalendarById returns an instance of the calendar which you can use to create the event.

  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])
  }   

Here is the full code:

 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])
  }
  });

  }

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

琉璃梦幻 2025-02-11 14:50:08

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

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;

然后,只需插入其他陈述是否

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 = "[email protected]"; 
  } else if (type == "BK 1" || type == "BK 2") {
    calendarId = "[email protected]"; 
  } else if (type == "GT 1" || type == "GT 2") {
    calendarId = "[email protected]";
  }
  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
从这里有一个想法

如何更改Google嵌入事件颜色?
在这里

Just to share what im doing for selecting multiple calendar ids. After a lot of searching (Im not a programmer my self), I found out that using if can get the job done too.

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;

Then, just insert if else statement like this

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 = "[email protected]"; 
  } else if (type == "BK 1" || type == "BK 2") {
    calendarId = "[email protected]"; 
  } else if (type == "GT 1" || type == "GT 2") {
    calendarId = "[email protected]";
  }
  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, {
    }); 
}

Hope this help

P/S
Got an idea from here

How to change Google Calendar embed event color?
and here https://www.considerednormal.com/2014/04/adding-a-google-calendar-event-using-google-forms/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文