It's my first time with JS or Apps Script and I've written a working script that reads in rows of data, and depending on one of the values in each row, writes out new rows to a second sheet, like this:
(I've simplified the script and just included two cases to demonstrate the transformation—the "input" sheet has rows and each row has a value "band"; the function checks this band value and, depending on which band, writes a set of rows to output)
function convertToOutput() {
var sheet = ss.getSheetByName('Input');
var output = ss.getSheetByName('Output');
var last = sheet.getLastRow();
output.getRange('A2:P').clearContent();
output.getRange('A2:P').setNumberFormat('@STRING@');
for(var i=2;i<last+1;i++){
var courseCode = sheet.getRange(i,1).getValue();
var sectionID= sheet.getRange(i,2).getValue();
var band = sheet.getRange(i,3).getValue();
if( band == 'A')
{
// insert rows for band into output
var periodID = '1';
var cycleDay = "#10100";
output.appendRow([courseCode,sectionID,periodID,cycleDay]);
var periodID = '2'
var cycleDay = "#00001";
output.appendRow([courseCode,sectionID,periodID,cycleDay]);
var periodID = '7'
var cycleDay = "#01000";
output.appendRow([courseCode,sectionID,periodID,cycleDay]);
}
if( band == 'B')
{
// insert rows for band into output
var periodID = '2';
var cycleDay = "#10100";
output.appendRow([courseCode,sectionID,periodID,cycleDay]);
var periodID = '3'
var cycleDay = "#00001";
output.appendRow([courseCode,sectionID,periodID,cycleDay]);
var periodID = '7'
var cycleDay = "#00010"
output.appendRow([courseCode,sectionID,periodID,cycleDay]);
}
It works as intended but right now I have it calling google apps for all the read/writes; I've read this answer and these best practices, but I haven't found an example close enough to the kind of transformation I'm doing to be able to figure out how to apply these practices without help.
How can I refactor using batching or the cache service to reduce the calls my function is making?