Search code examples
google-sheetsgoogle-apps-script

Format(Color) Specific Text in sheet using G-App Script


In Sheet1 I have a Data like below

Sales count Increased to +12% in current Year
Sales Count Decreased to -12% in current Qtr
Florida Sales went Up Hawaii went Down

I want to change the color of Specific text in the Data existing in the Sheet1. The specific texts which I want to change the color are declared as array in my code. Array name is pos_text I am using the following solution from @doubleunary

And here is the code:

function changecolor() {  
  var range = SpreadsheetApp.getActiveSpreadsheet()
                            .getSheetByName("Sheet13").getRange("D2:H8");
  var c_values = range.getValues();
  var bold = SpreadsheetApp.newTextStyle().setBold(true).build();
  var count = 0;
  var pos_text =['Increased','up','high','more','positive'];      
  var colors;
  var srch_text='Increased';
  Logger.log(pos_text.length);
   
  for(i=0;i<pos_text.length;i++){
    const regex = new RegExp(pos_text[i].replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),'gi');
    const num_regex = new RegExp('[-]?[0-9]\&*','gi');        
    const format = SpreadsheetApp.newTextStyle().setBold(true).setForegroundColor('#55883B').build();
    const values = range.getDisplayValues();
    Logger.log(values);
    
    if(values!=null){
      let match;
      const formattedText = values.map(row => row.map(value => {    
      const richText = SpreadsheetApp.newRichTextValue().setText(value);
      while (match = regex.exec(value)) {
        Logger.log("While " + match);    
        richText.setTextStyle(match.index, match.index + match[0].length, format);
      }
      while (match = num_regex.exec(value)) {
        richText.setTextStyle(match.index, match.index + match[0].length, format);
      } 
      return richText.build();
    }));
      range.setRichTextValues(formattedText); 
  }
}
}

As per the solution given in this Bold specific text in Google Sheets Single text is passed, Instead I want to pass the Array List. To do so I tried the above code I pasted, the code I modified only changes the numbers not texts. Not sure where I am missing.


Solution

  • Although I'm not sure whether I could correctly understand your expected result when your showing script is modified, how about modifying it as follows?

    Modified script:

    Please set your range to rangeA1Notation as A1Notation.

    function formatPhraseInText() {
      const rangeA1Notation = "'Sheet13'!D2:H8";
      const pos_text = ['Increased', 'up', 'high', 'more', 'positive'];
    
      const range = SpreadsheetApp.getActiveSpreadsheet().getRange(rangeA1Notation);
      const format = SpreadsheetApp.newTextStyle().setBold(true).setForegroundColor('#55883B').build();
      const values = range.getDisplayValues();
      let match;
      const formattedText = values.map(row => row.map(value => {
        const richText = SpreadsheetApp.newRichTextValue().setText(value);
        pos_text.forEach(t => {
          const regex = new RegExp(t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi');
          while (match = regex.exec(value)) {
            richText.setTextStyle(match.index, match.index + match[0].length, format);
          }
        });
        return richText.build();
      }));
      range.setRichTextValues(formattedText);
    }
    

    Testing:

    When this script is run, the following result is obtained.

    From:

    enter image description here

    To:

    enter image description here