In Sheet1 I have a Data in range “D2:H8” 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 have Arraylist like below
p_text = [‘Increased’,’up’,’high’,’more’,’positive’]
n_text = [‘decreased’,’drop’,’down’,’low’,’negative’]
I want to change the color of text in these Arraylist and numbers in the range, if positive Green if Negative Red.
I am follwoing this 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 neg_text = ['decreased','drop','down','low','negative'];
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);
}
}
}
If I remove Loop and pass srch_text in const regx It works , in case of looping its not changing the colors. Not sure where its going wrong. Thanks in advance!!