r/GoogleAppsScript • u/EmyAle • Sep 19 '25
Question How to make my script faster?
Hello, would anyone be able to help me with this script, so it would run faster? I might have around 30 users writing into my sheet at the same time and I want to know the timestamp when they write new info in specific column. The code works, but it feels quite slow:
function onEdit(e) { addTimestamp(e); }
function addTimestamp(e){ var targetColumn = 6; var tab = e.source.getActiveSheet().getName(); var startRow = 6; var row = e.range.getRow(); var col = e.range.getColumn();
if(col === targetColumn && row >= startRow && tab === tab && e.source.getActiveSheet().getRange(row,11).getValue() == ""){ e.source.getActiveSheet().getRange(row,11).setValue(new Date()); }
if(col === targetColumn && row > 5 && tab === tab && e.source.getActiveSheet().getRange(row,6).getValue() == "" && e.source.getActiveSheet().getRange(row,11).getValue() != ""){ e.source.getActiveSheet().getRange(row,11).clearContent(); } }
3
u/WicketTheQuerent Sep 19 '25
You could reduce the calls to Class SpreadsheetApp methods, but this might not improve performance much, as onEdit is known to be slow and have limitations that make it infeasible for some use cases.
var row = e.range.getRow(); var col = e.range.getColumn();as the row number and the column number are already included in the event object, as follows:e.range.rowStartande.range.columnStart.e.source.getActiveSheet(). For this, use var sheet =e.source.getActiveSheet(), then usesheetinstead ofe.source.getActiveSheet().2
u/mommasaidmommasaid Sep 19 '25 edited Sep 19 '25
Your formula after reformatting:
We can see that getting the tab name is wasted, because your tab comparison is doing nothing. And you are twice checking for the same starting row/column in slightly different ways.
Most importantly, you are doing
getValue()multiple times, and that's a costly operation (afaik it recalculates your sheet each time to ensure the values are up to date).You don't need
e.source.getActiveSheet().getRange(row, 6).getValue()at all because that's getting the currently edited cell's value which can be found ine.valueWhen clearing the timestamp, you don't need to
getValue()to see if it's empty before clearing it -- just clear it whether it needs it or not.Finally, as written, you do not update the timestamp every time an edit occurs, which to me doesn't sound like a timestamp, but instead a creation date.
Idk which is your desired behavior. A "last edited" timestamp is actually faster because you don't have to check if one exists already, you just set a new one every time. So that's what I did in the revised code. Let me know if you instead want a "creation time".
2
u/mommasaidmommasaid Sep 19 '25 edited Sep 20 '25
Timestamp - Last Edited - Sample Sheet
Try this out on the sample sheet to verify functionality, but copy it into your actual sheet as well to see what performance is like there.
Be sure to get rid of your existing onEdit() so there isn't a name conflict.
The revised code has zero
getValue()calls and should be a fair bit faster but YMMV on traffic / server load / mystery. You should still expect ~1 second to update, that's just the nature of apps script.If it is too slow for how fast the edits are coming in and your script is getting overrun and missing updates, there are other tricks to explore depending on your workflow and how "mission critical" an exact timestamp is.
(The code could/should be updated to handle multi-cell edits as well but see where this is at first as far as performance.)
EDIT: Removed sample code from this post, see below for Creation Timestamp code.
1
u/True_Teacher_9528 Sep 20 '25
You could possibly do it in a timed batch, depending on how often you want the data updated. You could use properties service to store everything in the scripts memory to then update all at once once an hour or whatever would be needed.
2
u/mommasaidmommasaid Sep 21 '25
Formula-based timestamps -- using self-referencing formulas with Iterative Calculations enabled -- are another option to consider and AFAIK are the highest performing solution available.
That has some other caveats that go with it, but if those were an issue perhaps a hybrid formula/script result could address those.
A Creation/Deletion timestamp like you want does not require any helper column like some other timestamps might using formulas...
Creation/Deletion Timestamp (ItCalc)