r/GoogleAppsScript • • 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 Upvotes

14 comments sorted by

View all comments

Show parent comment

1

u/EmyAle Sep 20 '25

Thank you a lot, I will try it!

2

u/mommasaidmommasaid Sep 20 '25

Per your other reply, modified code for a Creation timestamp rather than a Last Updated.

This now has one getValue() call in the current implementation. Idk how often that will happen, i.e. in your real life situation if the trigger cell gets edited multiple times or if it is perhaps just a user name dropdown that only happens once.

See what happens in your real life scenario before exploring faster / more complicated solutions.

Timestamp - Creation - Sample Sheet

// @OnlyCurrentDoc

//
// onEdit() reserved function, one instance per Spreadsheet.
// 
function onEdit(e) {

  // Call custom edit handlers until one returns true
  if (onEdit_Timestamp(e))
    return;

}


//
// Add or delete a creation timestamp upon detecting a trigger cell being edited.
// 
// Note: Multi-cell edits are not supported. If the top-left cell being edited is a trigger cell it will be updated but not the others.
//  
// Call from onEdit(), returns true if handled the event.
// 
function onEdit_Timestamp(e) {

  const FIRST_ROW = 6;

  const TRIGGER_COL = 6;
  const TIMESTAMP_COL = 11;

  // Exit if not editing a trigger cell
  if (!(e.range.columnStart === TRIGGER_COL && e.range.rowStart >= FIRST_ROW))
    return false;

  // Get the timestamp cell to update
  const stampCell = e.range.offset(0, TIMESTAMP_COL - TRIGGER_COL, 1, 1);

  // Clear the timestamp if trigger value is cleared, else create it if it doesnt' exist already
  if (e.value === undefined || e.value == "")
    stampCell.setValue(null);
  else {
    if (stampCell.getValue() == "")
      stampCell.setValue(new Date());
  }

  // Return true to indicate we handled the event
  return true;
}