how to mimic search and replace in google apps script for a range

I wanted to automet some text replacement in a Google sheet.

I utilized the record a macro functionality while doing CTRL-H seacrch and replace, but nothing got recorded.

Then I tryed this code:

 spreadsheet.getRange('B:B').replace('oldText','newText');

but it does not work, range has no replace method

Should I iterate each cell?

Upvotes: 2

Views: 1713

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • You want to replace oldText to newText for the specific column (in this case, it's the column "B".)
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several answers.

Unfortunately, replace() cannot be used for the value of getRange(). So in this answer, I used TextFinder for achieving your goal.

Sample script:

var oldText = "oldText";
var newText = "newText";

var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("B1:B" + sheet.getLastRow()).createTextFinder(oldText).replaceAllWith(newText);
  • When you run this script, oldText in the column "B" of the active sheet is replaced to newText.

References:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 5

Related Questions