简体   繁体   English

使用Google表格,我如何每小时运行一个脚本并填充列中的每个单元格?

[英]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.

相关问题 如何使用 Apps 脚本使用来自不同 Google 表格选项卡的 A 列数据填充 Google 表单问题? - How do I populate Google Form questions using column A data from different Google Sheets Tabs using Apps Script? 如何运行 Google 表格应用脚本,如果出现错误,则调用电子表格 function IMPORTXML() 来填充单元格? - How to run a Google Sheets App Script, and if there is an error, then call Spreadsheet function IMPORTXML() instead to populate the cell? 如何在使用 Google Apps 脚本填充某个单元格时自动填充当前时间和日期 - How do I auto populate the current time and date when a certain cell is populated using Google Apps Script 如何使用 Google Script 从 Google 表格中提取特定列? - How do I extract a specific Column from google sheets using Google Script? Google 表格:如何将图像标题自动填充到单元格列中(不是图像)? - Google Sheets: How to auto populate image titles into a cell column (Not Images)? 如何在多张工作表上运行 Google 脚本? - How do I run Google script on multiple sheets? 如何将值推送到列 Google Sheets App Script - How do I push values into a column Google Sheets App Script Google 表格脚本:如何仅在名称包含特定文本的表格上运行脚本? - Google Sheets script: how do I run a script only on the sheets whose names contain particular text? 如何在 Google 表格中的所有表格(选项卡)上运行 Google 表格应用脚本? - How do I run Google Sheets App Script on all sheets (tabs) in a Google Sheet? 如何在Google表格的列中的每第N行插入一个具有值的单元格 - How to insert a cell with value on every Nth row in a column in Google Sheets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM