I wanna find a specified text in spreadsheet and replace it with other word. I tried like this.
sheet = SpreadsheetApp.getActiveSheet()
sheet.replaceText ('ga: sessions','Sessions');
Then it says "Cannot find function replaceText in object Sheet."
from Cameron Roberts answer that works in almost every cases (if all cells are filled with strings only) and just for typing convenience (please accept his answer) here is the same script with a small change in the map function : I added toString() to the return argument.
Try something like this:
This is what worked for me, and it's simple.
The getActiveSheet() function returns a Sheet object whose functions are documented at the link. It would be awesome if there were an API like that, but currently you need to delete and insert (rows, columns, or the contents of a specific range) in order to be able to do that sort of replacement.
You can achieve this by reading in all values in the sheet (as an Array), Looping over the array, replacing the values, then writing the entire array back to the sheet.
There is a basic example below. You may need to modify this if your sheet contains formulas, or if you want to replace the text more than once in a given cell.
Note that any changes made to sheet between the time you read in and write out the data will be lost, and potentially could break this example.
Documentation:
Array.map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
String.replace: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Sheet.getDataRange: https://developers.google.com/apps-script/reference/spreadsheet/sheet#getDataRange()
range.GetValues: https://developers.google.com/apps-script/reference/spreadsheet/range#getValues()