user7786890
user7786890

Reputation:

Can't change values of numeric cells via Google Script

Here's my script for some Google sheet:

function onEdit(e) {
  var value = e.range.getValue();
  if (~value.indexOf("*")) {
    e.range.setValue("with *");
  } else {
    e.range.setValue("without *");
  }
}

When I type some string to cell — script changes value to "with *" or "without *", but when I change value to "1", "1.5" or some another numeric value — cell don't changes.

How can I change numeric values to "without *"?

Upvotes: 1

Views: 38

Answers (1)

Tanaike
Tanaike

Reputation: 201398

I think that when the numeric characters like 1 and 1.5 are inputted, the value is retrieved as a number. So in order to work your script, please convert value to a string.

So can you try as follows?

From :

if (~value.indexOf("*")) {

To :

if (~value.toString().indexOf("*")) {

or

if (~String(value).indexOf("*")) {

If I misunderstand your question, I'm sorry.

Upvotes: 1

Related Questions