neal aise
neal aise

Reputation: 895

Trigger onEdit() to update the same cell value in Google Sheets

When I add a value to a cell it should get converted (in place) to a formatted value (a hyperlink in my case). This should not trigger another onEdit().

So say I enter "foo" the text automatically becomes http://bar.com/zee/foo

How do we do this using onEdit? Is there a better way to achieve the same result?

Upvotes: 1

Views: 1649

Answers (1)

user3717023
user3717023

Reputation:

A script modifying cell content does not trigger the "edit" event. So you can achieve your goal without worrying about creating an infinite loop: modify the active cell using the new value in this cell.

function onEdit(e) {
  if (e.value.oldValue === undefined) {
    e.range.setValue('http://example.com/' + e.value);
  }
}

The reason for conditional statement is to avoid putting content in the cell when the edit amounts to clearing out the cell. Such edits are detected as follows: when the cell content is deleted, e.value becomes an object in which the property oldValue represents the old value in the cell.

Upvotes: 2

Related Questions