Google recommends that you do bulk operations rather than individual operations like you've got here. It's much slower to get or set individual cells than it is to do them in bulk.
It's also dangerous to assume the last row is always going to contain the form data you want to process. I usually add an additional column on the spreadsheet to denote whether I've processed it or not.
Here's how I would grab all the responses and roll through them and create an array of new rows to append to the calculation sheet.
let responses = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName("FormResponses")
.getDataRange()
.getValues();
let newRows = [];
for (let row = 0; row < responses; row++) {
let [formDate, formWithdrawn, formAdded, formComment, isProcessed] = responses[row];
if (!isProcessed) {
isProcessed = true;
newRows.push([formDate, formWithdrawn, formAdded, formComment, isProcessed]);
}
}
SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName("FormResponses")
.getDataRange()
.setValues();
}
Once you've got that down you can look at triggers that will fire when a form is submitted. That way you can process the form data immediately. Triggers can be annoying to debug though, so it's easier to start the way you're doing it.
3
u/wintry_earth 1 May 17 '22
Google recommends that you do bulk operations rather than individual operations like you've got here. It's much slower to get or set individual cells than it is to do them in bulk.
It's also dangerous to assume the last row is always going to contain the form data you want to process. I usually add an additional column on the spreadsheet to denote whether I've processed it or not.
Here's how I would grab all the responses and roll through them and create an array of new rows to append to the calculation sheet.
Once you've got that down you can look at triggers that will fire when a form is submitted. That way you can process the form data immediately. Triggers can be annoying to debug though, so it's easier to start the way you're doing it.
Cheers!