Skip to content Skip to sidebar Skip to footer

Google Scripts / Sheets Add Prefix To Data Once It Has Been Entered Into The Cell

Objectives Add a Prefix of CSC1[Leading Zeros] to a number such as 1291 or 12922 or 129223 this should apply to the whole column (Column F) Ensure total string length is = 15 usin

Solution 1:

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'));
  }
}

Post a Comment for "Google Scripts / Sheets Add Prefix To Data Once It Has Been Entered Into The Cell"