我在电子表格中有一个google脚本,可以从日历中加载约会。它计算约会的时间。但是要更新日历项需要很长时间,我经常会遇到超时错误。从日历中获取约会不是问题。在脚本下提到的电子表格的每10个选项卡中的查询是否会降低速度?
function updateUren()
{
var cal = CalendarApp.openByName("Uren");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Uren");
//set first row
var details=[["title", "date", "start", "end", "duration"]]; // events[i].getDescription(),
var range=sheet.getRange(2,1,1,5);
range.setValues(details);
//gets the year from Boekjaar
var year = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Boekjaar").getRange(2,1,1,1).getValue();
var events = cal.getEvents(new Date("January 1, "+year), new Date("December 31, "+year));
if(events.length == 0)
{
Browser.msgBox('No events found for year: "' + year + '"');
}
for (var i=0;i<events.length;i++) {
//http://www.google.com/google-d-s/scripts/class_calendarevent.html
var startTime = events[i].getStartTime();
var endTime = events[i].getEndTime();
var durationHours = (endTime - startTime) / (1000*60*60);
var details=[[events[i].getTitle(), startTime, startTime, endTime, durationHours]]; // events[i].getDescription(),
var row=i+3;
range=sheet.getRange(row,1,1,5);
range.setValues(details);
}
var foo = "foo";
}
我有10个选项卡正在运行我在此处显示的查询。他们过滤特定时期的约会持续时间
=QUERY (Uren!A:E, "SELECT B, SUM(E) WHERE A CONTAINS 'SomeKeyWord' AND B>=DATETIME '2012-6-18 00:00:00'
AND B<DATETIME '2012-7-16 00:00:00' GROUP BY B PIVOT A'")
在调用updateUren()函数期间,它们是否昂贵且减慢了速度?
有人知道答案吗?
答案 0 :(得分:1)
我不确定访问数据的QUERY函数是否与它有关(它可能),但我的理解是最好不要在循环中进行获取/设置,因为这会大大减慢速度。相反,首先构建您的数组,然后使用一个setValues()调用来设置它。
function updateUren()
{
var cal = CalendarApp.openByName("Uren");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Uren");
//create first row of 2D array
var details=[["title", "date", "start", "end", "duration"]]; // events[i].getDescription(),
//gets the year from Boekjaar
var year = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Boekjaar").getRange(2,1,1,1).getValue();
var events = cal.getEvents(new Date("January 1, "+year), new Date("December 31, "+year));
if(events.length == 0)
{
Browser.msgBox('No events found for year: "' + year + '"');
}
for (var i=0;i<events.length;i++) {
//http://www.google.com/google-d-s/scripts/class_calendarevent.html
var startTime = events[i].getStartTime();
var endTime = events[i].getEndTime();
var durationHours = (endTime - startTime) / (1000*60*60);
details.push([events[i].getTitle(), startTime, startTime, endTime, durationHours]); // events[i].getDescription(),
}
sheet.getRange(2, 1, details.length, 5).setValues(details);
//var foo = "foo";
}
编辑:虽然您仍然在循环内调用Calendar服务。我应该补充说这是未经测试的,但有兴趣听听它是否有所作为。