Google Apps分钟计时器未触发

时间:2016-12-07 20:17:30

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

我不是程序员,我是教师和教学技术专家。我正在努力弄清楚如何创建一些基本脚本来帮助我的学校使用Google Apps简化某些流程,而且我遇到了一个非常烦人且持久的问题。我的同事和我拼凑了一个脚本,该脚本通过Google表单提供的Google表格中的响应创建日历事件。我们在脚本上运行Copy Down附加组件,以便连接组成日历事件的description字段的某些字段,并且整个脚本在手动运行时运行良好。我们希望它每分钟运行一次,因此它会在复制后运行时捕获任何新的提交。问题是触发器不会运行,也不会向我们发送错误。没有什么可以调试的。脚本在下面,我想知道是否有人有任何想法来帮助我们。我们已经尝试将分钟计时器的脚本编写到脚本中,如另一篇文章中所示,但它只是每分钟都会重复触发,直到它出错。脚本如下,任何帮助将不胜感激。我无法正确使用此粘贴,抱歉。

    //insert your google calendar ID
var calendarId = "eths202.org_2d3933323533323433393439@resource.calendar.google.com";

//index (starting from 1) of each column in the sheet
var titleIndex = 15;
var descriptionIndex = 16;
var startDateIndex = 11;
var endDateIndex = 12;
var locationIndex = 10;
var googleCalendarIndex = 18;


/*
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();
 data.location = sheet.getRange(row,locationIndex,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);
 //end.setHours(23);
//end.setMinutes(59);
 //end.setSeconds(59);

 // 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, 
 location: sheetData.location,
 });
 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();

};

0 个答案:

没有答案