Tuesday, May 21, 2024
130
rated 0 times [  134] [ 4]  / answers: 1 / hits: 24243  / 10 Years ago, tue, october 21, 2014, 12:00:00

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.


More From » google-apps-script

 Answers
25

from Cameron Roberts answer that works in almost every cases (if all cells are filled with strings only) and just for typing convenience, here is the same script with a small change in the map function : I added toString() to the return argument.


function testReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSheet()
replaceInSheet(sheet,'values','test');
}

function replaceInSheet(sheet, to_replace, replace_with) {
//get the current data range values as an array
var values = sheet.getDataRange().getValues();

//loop over the rows in the array
for(var row in values){

//use Array.map to execute a replace call on each of the cells in the row.
var replaced_values = values[row].map(function(original_value){
return original_value.toString().replace(to_replace,replace_with);
});

//replace the original row values with the replaced values
values[row] = replaced_values;
}

//write the updated values to the sheet
sheet.getDataRange().setValues(values);
}

[#69061] Friday, October 17, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yusuf

Total Points: 167
Total Questions: 97
Total Answers: 108

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
;