[Solved] Google Scripts / Sheets Add prefix to data once it has been entered into the cell


Use the following:

  • An onEdit trigger to check when someone has updated a cell
  • padStart() to add the leading zeros
  • replace() the first 4 zeros with “CSC1” (since only the first occurrence will be replaced if passing a string instead of regular expression)
  • setValue() to update the edited cell
function onEdit(e) {
  if (e.range.columnStart == 1) { // Column A
    const padded = e.value.padStart(15, 0);
    e.range.setValue(padded.replace('0000', 'CSC1'));
  }
}

8

solved Google Scripts / Sheets Add prefix to data once it has been entered into the cell