1Rosomak
1Rosomak

Reputation: 25

replace function not found in Google Spreadsheets using App Scripts

I have been searching for a way to replace text inside a string with another text. I searched at SO as well and found link to use .replace and .toString() functions (java ones) but they are NOT being recognized by Google Apps Script Editor.

s=s.toString().replace(A, b);

TypeError: Cannot call method "replace" of undefined. (line 15, file "Code")

Is there a way hot to do the required?

Thank you

Upvotes: 0

Views: 347

Answers (2)

TheMaster
TheMaster

Reputation: 50445

The error doesn't say there isn't a replace function; but that it cannot call replace method of undefined. It means s is undefined and not a string at the line where replace is called. You should be figuring out why s's value changes to undefined in the previous 14 lines.

Also if A and b are not variables, they should be quoted: ("A","b")

Upvotes: 0

Cooper
Cooper

Reputation: 64040

Replace returns a string. It doesn't change in place.

function stringystuff() {
  var s1="This is my country";
  var s2="your";
  var s3="my";
  s1=s1.replace(s3,s2);
  SpreadsheetApp.getUi().alert(s1);
}

From: MDN

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. If pattern is a string, only the first occurrence will be replaced.

Upvotes: 1

Related Questions