我一直在使用脚本从Google电子表格中提取数据,并使用该信息填充Google日历。它在很大程度上起作用,但是非常不稳定:有时它不起作用,并且大多数时候它会产生错误,即使它确实有效。
我得到的一个错误是"找不到方法createAllDayEvent(string,number,object)。 (第39行,文件"")",这当然没有多大意义,因为这是一种已知的有效方法。
以下是我的代码:我非常感谢能够帮助我调整它的人更加可靠:
谢谢, 斯特林
function SpreadsheetToCalendar()
{
// This function should be executed from the spreadsheet you want to export to the calendar
var mySpreadsheet = SpreadsheetApp.getActiveSheet();
// These 2 methods do the same thing. Using the 2nd one, as 1st is deprecated
// var myCalendar = CalendarApp.openByName("Autodesk Renewals");
var myCalendar = CalendarApp.getCalendarById("rfx.com_bp79fdqvbortgmv21bj8am4hjc@group.calendar.google.com");
// optional - delete existing events
var events = myCalendar.getEvents(new Date("January 1, 2013 PST"),
new Date("December 31, 2016 PST"));
for (var i = 0; i < events.length; i++)
{
events[i].deleteEvent();
Utilities.sleep(300); // pause in the loop for 300 milliseconds
}
var dataRange = mySpreadsheet.getRange("A2:H300");
var data = dataRange.getValues();
// process the data
for (i in data)
{
var row = data[i];
// assume that each row contains a date entry and a text entry
var theContract = row[0] ;
var theDate = row[2] ;
var theCustomer = row[3] ;
var theAssign = row[5] ;
var theStatus = row[6] ;
var theNotes = row[7] ;
//var theTitle = (theCustomer + " | " + theContract + " | " + theAssign + " | " + theStatus);
var theTitle = ("| " + theCustomer + " | " + theContract + " | " + theAssign + " | " + theStatus + " |");
//myCalendar.createAllDayEvent(theTitle, theDate);
// myCalendar.createAllDayEvent(theTitle, new Date(date));
myCalendar.createAllDayEvent(theTitle, theDate, {description:theNotes});
Utilities.sleep(300); // pause in the loop for 300 milliseconds
}
}
答案 0 :(得分:0)
关于createAllDayEvent的具体错误,请注意每个参数都有一个类型。您的代码未验证第[2]行中的单元格是否具有有效日期,而是传递了一个数字,这是不期望的。
你也在一个糟糕的数组上做一个for..in ..(google it .tldr你也会在“count”属性上循环。)