The following isn't an answer to your question per se, but it is an exploration of the syntax surrounding TextStyle and RichTextValue. It may assist in understanding why var x = A1.getRuns();
doesn't work, and how you might build code to analyse cells. Note the programmatic creation of RichText is another thing altogether.
Spreadsheet Screenshot
function wa13593205() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheetname = "Sheet1";
var sheet = ss.getSheetByName(sheetname);
var range = sheet.getRange("G3");
var ar = sheet.setActiveRange(range);
//var ar = ss.getActiveRange();
var gr = ar.getRichTextValue().getRuns();
//Logger.log(gr); //DEBUG
var richText = ss.getCurrentCell().getRichTextValue();
var thistext = richText.getText();
Logger.log("DEBUG: Cell content = "+thistext);
var thisstyle = richText.getTextStyle();
//Logger.log("DEBUG: this style = "+thisstyle)
var runs = richText.getRuns();
//Logger.log("runs length="+runs.length)
for (var i=0; i<runs.length; i++) {
// get the style of this section of text, between startIndex and endIndex
var run = runs[i];
var runtext = run.getText();
var color = run.getTextStyle().getForegroundColor();
var font = run.getTextStyle().getFontFamily();
var fontsize = run.getTextStyle().getFontSize();
var startIndex = run.getStartIndex();
var endIndex = run.getEndIndex();
var styleBold = run.getTextStyle().isBold();
var styleItalic = run.getTextStyle().isItalic();
var stylesThru = run.getTextStyle().isStrikethrough();
var styleUline = run.getTextStyle().isUnderline();
Logger.log("i="+i+", Start Idx:"+startIndex+", End Idx:"+endIndex+", Text:"+runtext+", Bold?: "+styleBold+", Color:"+color+", Font:"+font+", Size:"+fontsize+", Italic?:: "+styleItalic+", Strikethru?:"+stylesThru+", Underline?:"+styleUline);
}
}
Logger - Results
DEBUG: Cell content = black red blue black
i=0, Start Idx:0, End Idx:5, Text:black, Bold?: false, Color:#000000, Font:Calibri, Size:14, Italic?:: true, Strikethru?:false, Underline?:false
i=1, Start Idx:5, End Idx:6, Text: , Bold?: true, Color:#000000, Font:Open Sans, Size:10, Italic?:: false, Strikethru?:false, Underline?:false
i=2, Start Idx:6, End Idx:9, Text:red, Bold?: true, Color:#ff0000, Font:Comic Sans MS, Size:18, Italic?:: false, Strikethru?:false, Underline?:false
i=3, Start Idx:9, End Idx:10, Text: , Bold?: true, Color:#000000, Font:Open Sans, Size:10, Italic?:: false, Strikethru?:false, Underline?:false
i=4, Start Idx:10, End Idx:14, Text:blue, Bold?: false, Color:#4a86e8, Font:Roboto, Size:14, Italic?:: false, Strikethru?:true, Underline?:false
i=5, Start Idx:14, End Idx:15, Text: , Bold?: true, Color:#000000, Font:Open Sans, Size:10, Italic?:: false, Strikethru?:false, Underline?:false
i=6, Start Idx:15, End Idx:20, Text:black, Bold?: false, Color:#b6d7a8, Font:Trebuchet MS, Size:18, Italic?:: true, Strikethru?:false, Underline?:false
a function that can be called like a normal function from a cell, like "=removeRed(A1)"
is a "custom function". To learn about custom functions please read developers.google.com/apps-script/guides/sheets/functions.[code]function myFunction(A1) { var x = A1.getRuns(); return x }
doesn't work. it is saying that getRuns isnt a function I can use. why is that? is it because the value of A1 isn't a string? I can't seem to get the rich text functions to work on cell values.getRuns()
is pretty new (almost 12 months) and there is precious little documentation and examples available. This external post Script to change the color of individual text in a cell, particularly the code included in the "UPDATE" at the bottom of the page, is handy.