Google apps script 气体:从工作表中提取时,如何跳过已创建的日历事件
我正在尝试设置一个脚本,该脚本将从Google工作表中提取以创建Google日历事件,如果脚本再次运行,则跳过已经创建的事件。当前,它正在创建一个事件,并在列表中运行时覆盖它 '''Google apps script 气体:从工作表中提取时,如何跳过已创建的日历事件,google-apps-script,Google Apps Script,我正在尝试设置一个脚本,该脚本将从Google工作表中提取以创建Google日历事件,如果脚本再次运行,则跳过已经创建的事件。当前,它正在创建一个事件,并在列表中运行时覆盖它 ''' 函数CTOcalendar(){ //设置工作表和日历 var电子表格=SpreadsheetApp.getActive().getSheetByName('Sheet2'); 变量日历= CalendarApp.getCalendarById(“nsmvt.org_classroom3eda5899@group.
函数CTOcalendar(){
//设置工作表和日历
var电子表格=SpreadsheetApp.getActive().getSheetByName('Sheet2');
变量日历=
CalendarApp.getCalendarById(“nsmvt.org_classroom3eda5899@group.calendar.google.com");
/** ... **/
var CTO=spreadsheet.getRange(“A2:C100”).getValues();
对于(x=0;x
'''
感谢您的帮助。建议的解决方案
首先,在执行这些操作时,您应该跟踪单元格索引,以便在需要时可以直接应用更新
有多种解决方案允许您跳过事件创建(如果它已经存在);以下是一些:
- 创建事件后,您可以在其行的末尾放置一个复选框:
- 在其事件行中注册eventId,这对于将来更新它也很有用
event.getId()
返回值填充单元格
工具书类
当行设置为事件时,您可以选中工作表中的复选框。并筛选出已检查的。或者,您可以从创建的事件端获取eventID,然后在以后扩展脚本时,可以使用该ID更新事件。
function CTOcalendar() {
// Set up sheet and calendar
var spreadsheet = SpreadsheetApp.getActive().getSheetByName('Sheet2');
var calendar =
CalendarApp.getCalendarById("nsmvt.org_classroom3eda5899@group.calendar.google.com");
/** ... **/
var CTO = spreadsheet.getRange("A2:C100").getValues();
for (x=0; x < CTO.length; x++){
// retrieve one entry at a time
var entry = CTO[x];
// get start time from the 2nd column, column 1
var startTime = new Date(entry[1]);
// get end time from the 3rd column, column 2
var endTime = new Date(entry[2]);
// retrieve name from the 1st column, column 0
var name = entry[0];
/** ... **/
// ... within for loop
// if name is missing/empty skip
// this entry
if (name == ''){continue;}
// get events during the start and end times
// specified above
var conflicts = calendar.getEvents(startTime, endTime);
// if the length of conflicts is 0, create an event directly
if (conflicts.length == 0){
createAndShare(calendar, name, startTime, endTime);
}
// if the length of conflict is larger than 0
// iterate through the conflicting events
else {
for (c=0; c < conflicts.length; c++){
// load one conflict at a time
var conflict = conflicts[c]
// if title of conflict matches name
// skip this conflicting event
if (conflict.getTitle() == name){continue;}
// create new event
create(calendar, name, startTime, endTime);
}
}
}
}
function create(cal, title, start, end){
// create event with provided title, start
// start and end time
var event = cal.createEvent(title, start, end);
}
// Filter the values by their Checkbox state
var CTO = spreadsheet.getRange("A2:C100").getValues().filter(row => !row[checkbox_col_index]);
// create the event since the checkbox isn't checked.
// At the end check the checkbox setting the cell value to `true`