/**
* Search and replace a string with another string.
* @param {string} searchTerm term to search for in all sheets
* @param {string} replaceTerm term to replace all occurences of the search term with
* @returns number of occurrences replaced
*/
function searchAndReplaceAllSheets(searchTerm, replaceTerm) {
// get all sheets in your Spreadsheet
const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
let totalReplaced = 0;
// Loop over all sheets
sheets.forEach((sheet) => {
// create instance of TextFinder
const noReplacedElements = sheet
.createTextFinder(searchTerm) // create text finder
.matchCase(false) // case-insensitive
.replaceAllWith(replaceTerm); // replace-all values that contain the search term with the new value
totalReplaced += noReplacedElements;
});
Logger.log(
`Replaced ${totalReplaced} occurences of ${searchTerm} with ${replaceTerm} in ${sheets.length} sheets`
);
return totalReplaced;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter