简体   繁体   中英

Reducing Google Apps Script execution time with using an array?

I wrote a script to periodically copy data from one column depending on if each cell was determined to have current data (Designated as ALIVE in another column), and place that data in another column in a different sheet. The script doesn't exceed the execution time, however I was wondering if there was a way to make it faster by utilizing Arrays.

I appreciate the help, I'm new to Google Apps Script programming but plugging along. Many thanks in advance for the advice.

function copyFunctionDATA() {

  var defSheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("(DATA)")
  var defSheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("(DATAdead)")
  var numLastRow = 60

for (var x=11; x<=numLastRow; x++) {

  var srcRange = defSheet1.getRange(x,1);
  var srcRange2 = defSheet1.getRange(x,1);
  var value = srcRange.getValue();  
  var value2 = srcRange2.getValue();

if (value2.indexOf("ALIVE") !== -1) {
   defSheet2.getRange(x,1).setValue(value);
  }
 }}

Transposing in 2D array is very simple. The main difference is the way data is indexed : ranges count from 1 and arrays count from 0.

So to transpose your code you should get 2 arrays (one for each sheet) and iterate the corresponding cells, change the value depending on your condition and write back the array to the spreadsheet to update it.

Here is a rough transpose of your code with a couple of comments to explain : (some variables ought to be renamed for clarity)

function copyFunctionDATA() {

  var defSheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("(DATA)").getDataRange().getValues();// read the whole sheet in a 2D array
  var defSheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("(DATAdead)").getDataRange().getValues();// read the whole sheet in a 2D array
  var numLastRow = 59 ; // I suppose you intentionally limit to the 60 first rows ?

for (var x=10; x<=numLastRow; x++) {  // starting from row 11 >> becomes 10 


  var value = defSheet1[x][0]; 
  var value2 = defSheet1[x][0]; // you made a mistake in your code : you define 2 identical ranges !! change it to your need  : 0 is column A, 1 is B etc...

if (value2.indexOf("ALIVE") !== -1) {
   defSheet2[x][0] = defSheet1[x][0]; 
  }
 }
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("(DATAdead)").getRange(1,1,defSheet2.length,defSheet2[0].length).setValues(defSheet2);// write back defSheet2 array to sheet (DATAdead)
}

EDIT : if you want to overwrite only the first column in defSheet2 change simply the range definition for this sheet, for example like this :

  var defSheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("(DATAdead)").getRange('A1:A').getValues();// read the whole sheet in a 2D array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM