Hello,
Is it possible to change in a cell a part of text to another colour?
Sincerely Peter
Hello,
Is it possible to change in a cell a part of text to another colour?
Sincerely Peter
Hi,
Yes, that's possible. You can either use RTFRuns, which is the "native" way in which rich strings are stored in Excel, or just use HTML. RTFRuns are an array of fonts, and the corresponding position in the string. Say, for character 0 to 5, use font 1, for character 6 to 7 use font 2, for characters 8 to end use font 3.
As always, the simplest way to find out this stuff is using APIMate. Just write text in different colors in Excel, open the file in APIMate, and check the result. I tried it here by writing
in Excel.
Then open the file in APIMate, and you get:
var
Runs: TArray<TRTFRun>;
fnt: TFlxFont;
begin
...
SetLength(Runs, 3);
Runs[0].FirstChar := 0;
fnt := xls.GetDefaultFont;
fnt.Color := TExcelColor.FromArgb($FF, $00, $00);
Runs[0].Font := fnt;
Runs[1].FirstChar := 5;
fnt := xls.GetDefaultFont;
Runs[1].Font := fnt;
Runs[2].FirstChar := 6;
fnt := xls.GetDefaultFont;
fnt.Color := TExcelColor.FromTheme(TThemeColor.Accent1);
Runs[2].Font := fnt;
//Set the cell values
xls.SetCellValue(1, 1, TRichString.Create('Hello world', Runs));
//We could also have used: xls.SetCellFromHtml(1, 1, '<font color = ''red''>Hello</font> <font color = ''#4472c4''>world</font>');