循环播放会降低我的谷歌应用程序速度。我怎样才能加快速度?

时间:2016-09-16 02:25:46

标签: performance optimization google-apps-script google-sheets

我有40个Google表格文件,我需要更改标题颜色。它们在第1列和第13列的行中有不同的行。我需要找到它们的位置,并改变它们的颜色。

这很有效,但很慢。无论如何我能更有效地写这个吗?我觉得循环直到57的行(我知道这是标题的最后一行)正在减慢我的过程,但我想不出更好的方法。

    function colorchange() {
    // The following defines the variable SpreadsheetIDs, which is a list of all the ID's that are going to be changed
      var spreadsheetIDs = ["1TZK5s3KvAFn9AMgWjIyPFAkjvhwL-3sGy5ieipQV-R8", //template
                    "1NSctLmGT0eWzvSj7P_NOUmgR79nIIokEEiaTJo6_Bb8", //1
                    "1plfjNQx4aFFnwcqMfv_wohbiYnlC_NVjW-etBM-xm5c", //2
                    "1i7myzXNFiFM6ZtkG590Hg4oEr-cAV8gsWxlV7rjL2b8", //3
                    "1-3r7D6nsVLT1FEZhGpGP9eF9G9HUZmuEe22aDajNBA0", //4
                    "1LBob3F3D1E__K6CJyBBIssho_cLkxm9uD00DP03QQz8", //5
                    "1Bot9TAbAr-g_Dgbqi4WJvXfE-Gt9t2ErxJk3Y-pvWZs", //6
                    "18kOx3-Q0DQhAA7jm19UZQhBUYShYVORWlQIgU_iicq4", //7
                    "1aHZ3FHUW6jt5HpuYb0fpBvfgTqSazXzZwJbvQEa8Qcs", //8
                    "1WkDHWL6sIkSu9qt_nizzXm2er-hxzuXEf8GsCTq3fug", //9
                    "1ZLDjW3VTMaumQIbihvyELyCuBghcn_GrQmuzCPnno9Y", //10
                   //ECT until 40ish spreadsheetID's
                   ];
      var i=0,color="",sheet,thisID="",e=1,c=1;

    // This calls the Browser input box and defines it the color Variable
        color = Browser.inputBox("Enter the color");

      for (i=0;i<spreadsheetIDs.length;++i) {
        thisID=spreadsheetIDs[i];
    //This opens the first ID, and opens up the Sheet1 sheet. ALL of the sheets in the territory MUST have this name.
        sheet=SpreadsheetApp.openById(thisID).getSheetByName('Sheet1');

    //This loops through the column 1 and 13 and checks for the correct font size all the way up to row 59.
    //When it finds the correct fontsize, it sets the fontcolor to the color variable value.
    for (e=1;e<58;++e) {
      //Row A
         c=1
          if(sheet.getRange(e,c).getFontSize() == '24') {
              sheet.getRange(e,c).setFontColor(color);}
      //Row M
         c=13
          if(sheet.getRange(e,c).getFontSize() == '24') {
              sheet.getRange(e,c).setFontColor(color);}
          }
    //Set Background for A1 and Color for D1
    sheet.getRange(1,1).setBackground(color);
    sheet.getRange(1,4).setFontColor(color);
        }
      }

2 个答案:

答案 0 :(得分:1)

我认为这Best Practices for App Script可以帮到你。

使用批处理操作

  

您可以编写脚本以最大限度地利用内置功能   通过最小化读写次数来缓存。交替   读写命令很慢。要加速脚本,请读取所有数据   使用一个命令进入数组,对数据执行任何操作   数组,并用一个命令写出数据。

     

这是一个例子 - 你不应该遵循或使用的例子。该   图库中的电子表格分形艺术脚本(仅适用于   旧版Google表格)使用以下代码来设置   100 x 100电子表格网格中每个单元格的背景颜色:

// DO NOT USE THIS CODE. It is an example of SLOW, INEFFICIENT code.
// FOR DEMONSTRATION ONLY
var cell = sheet.getRange('a1');
for (var y = 0; y < 100; y++) {
  xcoord = xmin;
  for (var x = 0; x < 100; x++) {
    var c = getColor_(xcoord, ycoord);
    cell.offset(y, x).setBackgroundColor(c);
    xcoord += xincrement;
  }
  ycoord -= yincrement;
  SpreadsheetApp.flush();
}
  

脚本效率低下:它遍历100行和100列,   连续写入10,000个单元格。 Google Apps脚本   回写缓存有帮助,因为它强制使用flush at进行回写   每一行的结尾。由于缓存,只有100   调用电子表格。

     

但是通过批量调用可以使代码更有效率。   这是一个重写,其中单元格范围被读入一个名为的数组   颜色,颜色分配操作是对数据执行的   数组,并将数组中的值写出到电子表格中:

// OKAY TO USE THIS EXAMPLE or code based on it.
var cell = sheet.getRange('a1');
var colors = new Array(100);
for (var y = 0; y < 100; y++) {
  xcoord = xmin;
  colors[y] = new Array(100);
  for (var x = 0; x < 100; x++) {
    colors[y][x] = getColor_(xcoord, ycoord);
    xcoord += xincrement;
  }
  ycoord -= yincrement;
}
sheet.getRange(1, 1, 100, 100).setBackgroundColors(colors);

答案 1 :(得分:0)

这确实是最慢的方法!

您需要做的是将所有值读入数组,更改值,然后一次性写回。

因此,检查Range类中的getValues和getFontColors以及setValues和setFontColors之类的调用,这样您只能对Range对象进行两次调用,并大大减少执行时间。