2

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?

1 Answer 1

5
  • 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.