我一直在使用脚本从Google电子表格中提取数据,并使用该信息填充Google日历。它在大多数情况下工作,但非常不稳定:有时它不工作,大多数时候即使它工作也会产生错误。
一个错误我得到很多是"无法找到方法createAllDayEvent(字符串,数字,对象)。(第39行,文件")",这当然没有多大意义,因为这是一个已知的有效方法。
下面是我的代码:我将非常感谢任何人可以帮助我调整它,使其更可靠:
谢谢,英镑兑美元gbp =
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
}
}
关于createAllDayEvent的特定错误,请注意每个参数都有一个类型。您的代码没有验证行[2]中的单元格是否具有有效的日期,而是传递了一个数字,这是不期望的。
你也在为……做……在数组是坏的(谷歌它。TLDR你也将循环"count"属性)