Jey Jay
Jey Jay

Reputation: 113

Google scripts use replaceText function

I have this function that returns all the sheets that don't contain *.

I would like to replace some text in the results.

The results show all my sheets without a * like "Client < 25", "Client < 50" etc

I want to get the result: "< 25" so remove "Client" from it.

function SheetNamesFiltered(avoid_memoization) {
  return SpreadsheetApp.getActiveSpreadsheet().getSheets()
  .map(sheet => [sheet.getName()])
  .filter(sheetName => !sheetName[0].match(/\*/));
}

I have tried the following but it doesn't work

    function SheetNamesFiltered(avoid_memoization) {
  return SpreadsheetApp.getActiveSpreadsheet().getSheets()
  .map(sheet => [sheet.getName()].replaceText("Client ", ""))
  .filter(sheetName => !sheetName[0].match(/\*/));
}

Thank you!

Upvotes: 1

Views: 65

Answers (1)

NightEye
NightEye

Reputation: 11194

Instead of doing replaceText during map, I separated them. First, getting the sheetName with no * and next is removing Client using replace in all results.

function SheetNamesFiltered(avoid_memoization) {
  var array = SpreadsheetApp.getActiveSpreadsheet().getSheets()
  .map(sheet => [sheet.getName()])
  .filter(sheetName => !sheetName[0].match(/\*/));
  // Separated replacement in every item in array result
  array.forEach(function(item){
    item[0] = item[0].replace("Client ", "");
  });
  Logger.log(array);
}

Output:

output

Sheets:

sheets

Upvotes: 2

Related Questions