user2843472
user2843472

Reputation: 3

Google Spreadsheet; script to automatically change specific text on cell entry

I am wrestling with something which I think must be quite simple.

I want to enter a timecode into a cell in this format: "01.00.00.00" and have the script automatically change the contents of the cell to: "01:00:00:00"

I tried using replacetext, I also tried modifying a script I found online...

function onEdit(e) {
 if (e.value == ".") e.range.setValue(":")
 }

...but that only changes the cell if it ONLY contains one single "." character, and I think it also replaces the whole cell with ":" so it would never be able to change all my "." and leave the numbers themselves alone.

I'm not a programmer and hope that this'll be quite straightforward for someone to help with.

Many thanks for reading my question.

Upvotes: 0

Views: 3205

Answers (1)

wchiquito
wchiquito

Reputation: 16551

One option is:

function onEdit(e) {
  var range = e.range, column = 3; // Column where you will apply the change
  if (range.getColumn() === column)
    range.setValue(e.value.replace(/\./g, ':'));
}

Upvotes: 1

Related Questions