[英]Using Google Sheets, how do I run a script every hour and populate each cell in a column?
I'm using google sheets and have never run a "cron job" here. 我正在使用Google表格,但从未在这里运行过“ cron作业”。 To give credit where it's due, I found this awesome code to help me access the speed insights api here: https://statsravingmad.com/measure/page-speed-insights/
为了给您应得的荣誉,我找到了这个很棒的代码,以帮助我在此处访问Speed Insights API: https : //statsravingmad.com/measure/page-speed-insights/
function speed(url,device,filter_third_party_resources,http_secure) {
url = url || 'www.statsravingmad.com';
strategy = 'desktop' || device;
filter_third_party_resources = 'true' || filter_third_party_resources;
http_secure = 'false' || http_secure ;
switch (http_secure) {
case 'false':
http_protocol = 'http://';
break;
case 'true':
http_protocol = 'https://';
break;
}
var key = 'api-key';
var api = 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=' + http_protocol + url
+ '&filter_third_party_resources=' + filter_third_party_resources +
'&strategy=' + strategy + '&key=' + key;
var response = UrlFetchApp.fetch(api, {muteHttpExceptions: true });
var result = JSON.parse(response.getContentText());
score = result.ruleGroups.SPEED.score;
return(score);
}
So I have this code in a function that is triggered every hour for my particular test sites in my google sheet. 因此,我将这个代码的功能设为每小时针对我的Google工作表中的特定测试网站触发一次。
But, the data is only filling one cell per site, the cell that the formula is assigned to. 但是,每个位置的数据仅填充一个单元格,即公式指定的单元格。
When using google sheets, how can I modify this in order to have it fill a new cell in a column every hour? 使用Google表格时,如何修改它以便使其每小时填充一列中的新单元格? Do I modify this code, do I have to set up another function, or is there an option to fill the cells down a column?
是否修改此代码,是否必须设置其他功能,或者是否可以选择将单元格填充到列中?
This function can be modified to write, say, to column C of Sheet1. 可以修改此函数以将其写入Sheet1的C列。 Here is how it would end, instead of
return(score)
(there is no need to return anything if the value is written to the spreadsheet directly; the function would not be invoked from the spreadsheet, but from a trigger ). 这是结束的方式,而不是
return(score)
(如果将值直接写入电子表格,则无需返回任何内容;该函数不会从电子表格中调用,而是从触发器调用)。
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var values = sheet.getRange("C:C").getValues(); // using column C
var lastRow = 1;
for (var i = 0; i < values.length; i++) {
if (values[i][0] != "") {
lastRow = i + 1;
}
}
sheet.getRange(lastRow + 1, 3).setValue(score); // column = 3 because C
Here the loop finds the last row in column C that has data, and the values of score
is placed under it. 在这里,循环在C列中找到了包含数据的最后一行,并将
score
的值置于其下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.