简体   繁体   English

Google Script/sheets - 如何使用 ...new Set() 使脚本在循环期间运行得更快

[英]Google Script/sheets - How to make a script run faster during a loop using …new Set()

Based on the great support by @ziganotschka on this question , we have the following code.基于@ziganotschka对此问题的大力支持,我们有以下代码。 For each of reference, here's a link to the demo sheet posted on the original question.对于每个参考,这里是原始问题上发布的演示表的链接

function change_row_color() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  var range = spreadsheet.getDataRange();
  var rangeData = range.getValues();
  var lastRow = spreadsheet.getLastRow();
  var previousclient = rangeData[0][4];
  console.log(previousclient);
  var colors = [[0,0,0,0,0]];
  for ( j = 1 ; j < lastRow; j++){
    var currentclient = rangeData[j][4];
    console.log(previousclient," ",currentclient);
    if (previousclient != currentclient) {
      colors.push(["#cc4125","#cc4125","#cc4125","#cc4125","#cc4125"]);
      previousclient = currentclient;  
    } else{
      colors.push([0,0,0,0,0]);
    }
  }
range.setBackgrounds(colors);
}

The script is running great, but it's running rather slow.该脚本运行良好,但运行速度相当慢。 My test array has 350 rows and it takes about 20s to run.我的测试数组有 350 行,运行大约需要 20 秒。 My actual array has 10K+ rows, and growing.我的实际数组有 10K+ 行,并且还在增长。 Once I run this code on the real array, I'd like to drop it to below 5s.在真实数组上运行此代码后,我想将其降至 5 秒以下。 ziganotschka suggested to use...new Set(). ziganotschka 建议使用...new Set()。 I'd love you input on this.我希望你能对此提出意见。

Using Conditional formatting使用条件格式

To apply a given style for all the row where the value in the E column is the same as the previous row.为 E 列中的值与前一行相同的所有行应用给定样式。

In your sheet:在您的工作表中:

  • Format > Conditional formatting格式 > 条件格式
  • Apply to range A2:Z适用于范围A2:Z
  • Format rule Custom formula is格式规则Custom formula is
  • Value of formula =$E2=$E1 // change the value of E is you need to make the test on an other row.公式的值=$E2=$E1 // 改变 E 的值是你需要在另一行上进行测试。

Choose your wanted formatting style.选择您想要的格式样式。

May not be any faster but you can give it a try.可能不会更快,但您可以尝试一下。

function change_row_color() {
  var ss=SpreadsheetApp.getActive()
  var sh=ss.getSheetByName('Sheet1');
  var rg=sh.getDataRange();
  var vs=rg.getValues();
  var colors=rg.getBackgrounds();
  colors.map(function(r){return r.fill('#000000')});//this is not necessary if the backgrounds always start as black.
  var pc=vs[0][4];
  vs.forEach(function(r,i){
    if(i>0 && r[4]!=pc) {
        colors[i].fill("#cc4125");
        pc=r[4];
    }
  });
rg.setBackgrounds(colors);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google 表格脚本,自定义 function 运行时错误(30 秒)。 如何让function跑得更快? - Google Sheets Script, Custom function runtime error (30seconds). How to make function run faster? 我怎样才能使这个谷歌表脚本代码更短/更快? - How can I make this google sheets script code shorter/faster? 如何让这个脚本运行得更快? - How to make this script run faster? 如何在Google表格脚本中更快地突出显示重复项? - How to highlight duplicates faster in google sheets script? 使用 Google Apps 脚本,如何替换 Google 表格模板中的文本以制作新表格? - Using Google Apps Script, how can I replace text in a Google Sheets template to make a new Sheet? 如何在多个工作表上运行脚本,Google Sheets - How to run a script on multiple sheets, Google Sheets 如何使循环在 Google 表格的 App Script 中自动更新范围 - How to make the loop update the range automatically in App Script for Google Sheets 需要帮助,请在脚本编辑器中创建For循环,以使脚本在Google Spreadsheet中的所有工作表上运行代码 - Need help creating For loop in Script editor to make Script run code on all sheets in a Google Spreadsheet 使用 For 循环运行 Apps 脚本 - Google 表格 / Apps 脚本 - Use A For Loop to Run Apps Script - Google Sheets / Apps Script 在谷歌表格脚本的 For 循环中使用 getTime - Using getTime in a For loop in google sheets script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM