r/GoogleAppsScript • u/swebberz • Aug 27 '25
Question Comparing 2 scripts to find out which would run faster
I have 2 scripts that are meant to do identical things.
I've rewritten it hoping to speed it up (it's not that slow but even a few seconds feels bad for a pretty small task) and the rewrite actually takes longer.
The 1st one runs in about 2 seconds usually and the 2nd one usually takes 3-4 seconds.
I am absolutely a novice at this, so if there is something else I could be changing to make this more efficient, let me know.
The process is,
Selecting a checkbox in Column D triggers the function.
Enters the current time in Column B
Sets the checkbox in Column D back to False.
Takes a value from Column H and adds it to a running total that is in Column E
function SetTimeOnEdit() {
var spreadsheet = SpreadsheetApp.getActive();
if (spreadsheet.getCurrentCell().getValue() == true &&
spreadsheet.getCurrentCell().getColumn() == 4 &&
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName() == 'Sheet1') {
spreadsheet.getCurrentCell().offset(0, -2).activate();
spreadsheet.getCurrentCell().setValue(new Date()).setNumberFormat("mmm d at h:mm AM/PM");
spreadsheet.getCurrentCell().offset(0, 2).activate();
spreadsheet.getCurrentCell().setValue('FALSE');
var currentCount = spreadsheet.getCurrentCell().offset(0,1).getValue()
var addCount = spreadsheet.getCurrentCell().offset(0,4).getValue()
spreadsheet.getCurrentCell().offset(0,1).setValue(currentCount + addCount) }
};
function SetTimeOnEdit(e) {
if (e.value !== 'TRUE'
|| e.range.columnStart !== 4
|| !(sheet = e.range.getSheet()).getName().match(/^(Sheet1)$/i)){
return;
}
sheet.getCurrentCell().offset(0, -2).setValue(new Date()).setNumberFormat("mmm d at h:mm AM/PM")
sheet.getCurrentCell().setValue('FALSE')
sheet.getCurrentCell().offset(0,1).setValue(sheet.getCurrentCell().offset(0,1).getValue()+sheet.getCurrentCell().offset(0, 4).getValue())
};
2
u/True_Teacher_9528 Aug 27 '25
Big thing you can do to speed it up is move your operations into memory, then use setvalues after doing what you want it to do. I’m not at my desk currently to type it out exactly but that’s the basic concept. I’ll be back at my desk in just a few minutes to add onto this comment
1
u/mommasaidmommasaid Aug 27 '25
The speed is fairly typical of script, but you can make it a bit faster...
I would recommend you use a checkbox with a custom "checked" value to trigger you script, that way you don't have to hardcode the sheet / column where the checkbox resides.
There are special range functions for checkboxes -- range.isChecked(), range.check(), range.uncheck() -- use those instead of get/set value because they won't affect non-checkbox cells, and will work with custom checkbox values.
From my experience -- the costly operation here is getValue() because it will cause your sheet to recalculate first. So get both the running total and add amount in one getValues() call. Multiple setValue() calls are ok because those seemed to be batched by sheets.
Idk if this causes any appreciable performance hit, but IMO better practice would be to pre-format the date column in your sheet as desired rather than setting it in script. There's no reason script should have to "know" what the format is.
Running Total Timestamp