脚本编辑器,自动结束时间

时间:2018-07-09 12:56:20

标签: time editor google-form

全面披露,我绝不是任何类型的程序员。我的图书馆正在寻找一种更简便的方法,以便将多个位置添加到公共日历中。

最终,我偶然发现了this script,我能够适应我们的需求。但是,他们希望进行的一项更改是将结束时间默认设置为2小时后。例如,如果事件从1开始,则结束时间自动默认为3。

谁能告诉我我需要对脚本进行哪些更改才能实现? Here is the test form,我们用来输入日期。现在是手动输入结束时间,但我想必须将其删除,对吗?

在解决这个问题上的任何帮助将不胜感激。

谢谢!

//insert your google calendar ID
var calendarId = "ID-FOR-TEST-CALENDAR";

//index (starting from 1) of each column in the sheet
var titleIndex = 2;
var descriptionIndex = 3;
var startDateIndex = 4;
var endDateIndex = 5;
var googleCalendarIndex = 6;

/*
find the row where the Google Calendar Event ID is blank or null
The data of this row will be used to create a new calendar event
*/
function findRow(sheet) {
 var sheet = SpreadsheetApp.getActiveSheet();
 var dataRange = sheet.getDataRange();
 var values = dataRange.getValues();

 for (var i = 0; i < values.length; i++) {
 if(values[i][googleCalendarIndex-1]=="" || values[i][googleCalendarIndex-1]==null)
 newEvent(i+1);
 } 
 };


/* 
get the data of the new row by calling getSheetData() and 
create a new Calendar event by calling submitToGoogleCalendar()
*/

function newEvent(row){ 
 var sheet = SpreadsheetApp.getActiveSheet();
 var eventId = submitToGoogleCalendar(getSheetData(sheet,row),null)
 if(eventId!=null) 
 sheet.getRange(row,googleCalendarIndex,1,1).setValue(eventId);
};


/* 
Store the data of a row in an Array
*/

function getSheetData(sheet,row)
{
 var data = new Array();

 data.title=sheet.getRange(row,titleIndex,1,1).getValue();
 data.description=sheet.getRange(row,descriptionIndex,1,1).getValue();

 data.startDate = sheet.getRange(row,startDateIndex,1,1).getValue();
 data.endDate = sheet.getRange(row,endDateIndex,1,1).getValue();

 return data; 
};

/* 
if a cell is edited in the sheet, get all the data of the corresponding row and 
create a new calendar event (after deleting the old event) by calling submitToGoogleCalendar() 
*/

function dataChanged(event){

 var sheet = SpreadsheetApp.getActiveSheet();
 var row = event.range.getRow();

 var eventId = sheet.getRange(row,googleCalendarIndex,1,1).getValue();

 var eventId = submitToGoogleCalendar(getSheetData(sheet,row),eventId)

 if(eventId!=null)
 sheet.getRange(row,googleCalendarIndex,1,1).setValue(eventId);

};


/* 
This function creates an event in the Google Calendar and returns the calendar event ID 
which is stored in the last column of the sheet 
*/
function submitToGoogleCalendar(sheetData,eventId) {
 // some simple validations ;-)
 if(sheetData.title == "" || sheetData.startDate == "" || sheetData.startDate == null)
 return null;

 var cal = CalendarApp.getCalendarById(calendarId);
 var start = new Date(sheetData.startDate);
 var end = new Date(sheetData.endDate);

 // some simple date validations
 if(start > end)
 return null;

 var event = null;

 //if eventId is null (when called by newEvent()) create a new calendar event
 if(eventId==null)
 {
 event = cal.createEvent(sheetData.title, start, end, {
 description : sheetData.description, 
 });
 return event.getId(); 
 }
 /*
 else if the eventid is not null (when called by dataChanged()), delete the calendar event 
 and create a new event with the modified data by calling this function again
 */
 else
 {
 event = cal.getEventSeriesById(eventId);
 event.deleteEventSeries(); 
 return submitToGoogleCalendar(sheetData,null);
 }

 return event.getId();

};

1 个答案:

答案 0 :(得分:0)

我自己没有尝试过,相信您可以这样做:

function submitToGoogleCalendar(sheetData,eventId) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var start = new Date(sheetData.startDate);
  var end = new Date(sheetData.endDate);
  end.setHours(start.getHours() + 2);

...,其中最后一行是添加到原始脚本中的新行。

关于您的问题,是否应该删除结束时间,我想是的,应该删除结束时间。