How do I automatically insert today’s date when editing designated columns in Google Sheets with Apps Script?

I need an Apps Script that writes today’s date in the cell immediately to the right when a cell in columns C, G, or I on the ‘Log’ sheet is modified. Example:

function recordDate(e) {
  var sheet = e.source.getActiveSheet();
  if (sheet.getName() !== 'Log') return;
  var cellEdited = e.range;
  if ([3, 7, 9].includes(cellEdited.getColumn())) {
    var targetCell = cellEdited.offset(0, 1);
    if (!targetCell.getValue()) {
      targetCell.setValue(new Date());
    }
  }
}

hey, try using the onedit trigger to check if the target cell is empty before filling in a new date. i used something similar and found it works ok. maybe add a try/catch around the offset part incase of some non-editable cells

I implemented a solution that ensured proper error-handling and additional conditions to prevent unwanted overwriting in range targets. I modified my script to handle multi-cell edits by looping through each cell within the designated columns, which helped maintain your log’s integrity when large edits occur. I found that considering additional events such as range combinations was beneficial in avoiding accidental timestamp updates. This refined approach worked well in practice while keeping the logic straightforward and reliable.

I tackled this issue by creating a script that first verifies if the edited cell belongs to the ‘Log’ sheet and is within the specified columns. I made sure to check if the adjacent cell is blank before setting the new date, which helps prevent accidental updates. I also added a mechanism to handle multi-cell edits smoothly, ensuring that bulk modifications don’t lead to multiple overwrites. Error logging was also incorporated to keep track of any unexpected behavior during execution.