I wish to alter the colour of a specific section of text within a cell range. I wish to use it as a function which I have named =CTEXT. I wish to make the function visible and working =CTEXT(range, text, colour)
function CTEXT(range, text, color) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getRange(range);
var richText = cell.getRichTextValue();
var newText = richText.getText();
var startIndex = newText.indexOf(text);
if (startIndex !== -1) {
var endIndex = startIndex + text.length;
var textStyle = SpreadsheetApp.newTextStyle()
.setForegroundColor(color)
.build();
var newRichText = richText.copy().setText(newText)
.setTextStyle(startIndex, endIndex, textStyle)
.build();
cell.setRichTextValue(newRichText);
I have tried this, however, I am quite new to apps script and do not know how this works to its full extent.
New contributor
Tristan GARNER is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.