Sharing a very useful solution from Bannager Bong in this thread of the Google Docs editor's help forum . Small changes have been made to the function so that it takes arguments to search for, replaces the values, and then adds a range argument so that the function can focus on a specific area. Despite this, this method is very slow (my sheets have 5 thousand lines).
function Cleanup12m() { var spreadsheet = SpreadsheetApp.getActive(); //fandr(",", ""); //fandr("\"",""); fandr(" ","",spreadsheet.getRange('BA:BA')); //uses specific range }; function fandr(find, repl) { var r=SpreadsheetApp.getActiveSheet().getDataRange(); var rws=r.getNumRows(); var cls=r.getNumColumns(); var i,j,a,find,repl; //find="abc"; //repl="xyz"; for (i=1;i<=rws;i++) { for (j=1;j<=cls;j++) { a=r.getCell(i, j).getValue(); if (r.getCell(i,j).getFormula()) {continue;} //if (a==find) { r.getCell(i, j).setValue(repl);} try { a=a.replace(find,repl); r.getCell(i, j).setValue(a); } catch (err) {continue;} } } }; //Revised to apply to a selected range function fandr(find, repl, range) { var r= range;//SpreadsheetApp.getActiveSheet().getDataRange(); var rws=r.getNumRows(); var cls=r.getNumColumns(); var i,j,a,find,repl; //find="abc"; //repl="xyz"; for (i=1;i<=rws;i++) { for (j=1;j<=cls;j++) { a=r.getCell(i, j).getValue(); if (r.getCell(i,j).getFormula()) {continue;} //if (a==find) { r.getCell(i, j).setValue(repl);} try { a=a.replace(find,repl); r.getCell(i, j).setValue(a); } catch (err) {continue;} } } };
Tommie C.
source share