Been trying to automate the Find and Replace in Google Sheets but did not work

84 views Asked by At

My sheet is a query-sheet from database. Some of them contain html hex-code color which I need to manually use edit>Find and Replace every time it is refreshed.

I am very new to Google App Script and been trying to use the following code:

function Clearcode() {
  var lookupone = new RegExp(/{color:#.{7}/);
  var rep = "";
  var spreadSheet = SpreadsheetApp.getActive();
  var querySheet = spreadSheet.getSheetByName("QUERY");
  var lastRow = querySheet.getLastRow();
  var lastColumn = querySheet.getLastColumn();
  var data = querySheet.getRange(2, 1, lastRow-1, lastColumn).getValues();
  var textfinder = querySheet.createTextFinder(lookupone);
  var found = textfinder.replaceAllWith(rep);
  return (found);
}

Yet, when I run this function in the sheet it did not work. Any thought?

P.S. I planned to eliminated "[color]" part of the hex-code as well by create the similar function.

P.S.2 I have attached a snapshot of a table as you requested. The red line is just for confidentiality of the data. Below the line is just a normal text.

A snapshot of the sheet

1

There are 1 answers

0
TheMaster On BEST ANSWER

Pay attention to types!

querySheet.createTextFinder("\\{color:#?.{0,6}\\}")//only 6 characters
  .useRegularExpressions(true)
  .replaceAllWith("")