brpl20
brpl20

Reputation: 13

Script function replace is not changing the cell

I created this function to change specific cell value, from "5.000,00 C" to "5.000,00":

But when I run the function the cell does not change.

I don't know what I'm doing wrong.

function myFunction() {
  var FILE = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/");
  var CONTENT = FILE.getSheetByName("r2--tests");
  var A1 = CONTENT.getRange("G3").getValue()
  var A1split = A1.split(" ");
  A1.replace(A1, `${A1split[0]}`);
};

Upvotes: 0

Views: 40

Answers (2)

Jotha
Jotha

Reputation: 428

You should assign the result of your replace to A1, and then write it back to the Spreadsheet:

function myFunction() {
  var FILE = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/");
  var CONTENT = FILE.getSheetByName("r2--tests");
  var A1 = CONTENT.getRange("G3").getValue()
  var A1split = A1.split(" ");
  A1 = A1.replace(A1, `${A1split[0]}`);
  CONTENT.getRange("G3").setValue(A1);
};

Upvotes: 1

MinusFour
MinusFour

Reputation: 14423

You'd need to update the cell:

CONTENT.getRange("G3").setValue(A1.replace(A1, `${A1split[0]}`));

Upvotes: 1

Related Questions