Google App Script Search and Update Single Cell Value Across Multiple Sheets If Found - Stack Overflow

PHOTO EMBED

Fri Sep 02 2022 08:42:13 GMT+0000 (Coordinated Universal Time)

Saved by @adegard

/**
 * 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;
}
content_copyCOPY

https://stackoverflow.com/questions/71555648/google-app-script-search-and-update-single-cell-value-across-multiple-sheets-if