简体   繁体   中英

JS/GAS: For loop to speed up copy from spreadsheet cell, replace document placeholder with value?

I'm using Google App Script. I have a spreadsheet with questions on it for energy auditors about buildings they visit. The auditor is asked to put their answers to the spreadsheet's questions in certain cells. Then they can use a script I wrote to generate a more formal looking Google Document report. The report is generated via these steps: Each cell the auditor inputs an answer into is a defined range. For instance, let's say Cell B10 is defined as "buildingAddress" in spreadsheet. The auditor is asked to put the building address in that cell - let's say he inputs "55 Sample Drive, Portland". When the auditor clicks to generate a Document report, the script runs these lines:

var buildingAddress = sheet.getRangeByName('buildingAddress').getValue(); copyBody.replaceText("<buildingAddress>", buildingAddress);

The place holder in my (Document file) report template is <buildingAddress> . So the code finds this in the report template and replaces it with "55 Sample Drive, Portland" - the value the auditor entered into the spreadsheet cell.

Unfortunately, there are A LOT of such cell values I need to pull from the spreadsheet and push to a placeholder in the report document. They all fit the structure of this:

var buildingAddress = sheet.getRangeByName('buildingAddress').getValue(); 
  copyBody.replaceText("<buildingAddress>", buildingAddress);

So, I'm wondering, can I achieve the same result but use a lot less code by using an array and for loop??? Let's say the array looks like this:

var array = ["buildingAddress", "buildingOwner", "auditorName"];

How do I set up a for loop???

Thank you!!!!!

a loop will not give you gains its exactly the same except cleaner code.

debug it and see where the slow parts are (see execution transcript or log at key steps).

For example if its slow to get a range by name, and all those named ranges are contiguous, instead make a single named range for those cells. get the range (will return array) and get the values from there. this makes a single "get range" call instead of the N you have now.

from your "how to write a loop" question, seems you are just beginning programming. Id suggest a tutorial and more practicing as stackoverflow assumes you know those basics.

Thanks! If anyone's interested, here's what worked for me. I made an array of string objects. Each string was same text as a defined range in my spreadsheet. Then I used this for loop:

for(var i = 0; i < simpleCopyReplaceArray.length; i++){
var definedRangeCellName = simpleCopyReplaceArray[i];
var cellValue = ss.getRangeByName(definedRangeCellName).getValue();
var placeHolder = "<" + definedRangeCellName + ">";

if( cellValue != ""){
  copyBody.replaceText(placeHolder, cellValue);
}else{
  copyBody.replaceText(placeHolder, "");}

}

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