Sweco-MartijnDekker's avatar
Sweco-MartijnDekker
Copper Contributor
Apr 21, 2022
Status:
New

Excel Partial text font properties

I would like to be able to change the font color of a specific part of the text in a cell;
In VBA you would do something like this

 

Public Sub changeColor()

    Range("A1").Characters(Start:=7, Length:=5).Font.ColorIndex = 3

End Sub
  • Hi! Thank you for your patience. The requested rich text support feature has been implemented and will be included in the upcoming Excel requirement set 1.18 release, scheduled for February 2025. In the meantime, you can already experiment with these capabilities in the beta build.

    The following APIs will be available:

    - Excel.CellPropertiesInternal.TextRuns
    - Excel.CellPropertiesLoadOptionsInternal.TextRuns
    - Excel.RangeTextRun.Text
    - Excel.RangeTextRun.Font

    For more details, please refer to the official documentation. Below is a sample code snippet that demonstrates how you can use rich text formatting in cells:

    (async () => {
      await Excel.run(async (context) => {
        var sheet = context.workbook.worksheets.getItem("TestSheet");
        const cellSrc = sheet.getRange("A1");
        const cellSrcTextRun: Excel.SettableCellProperties = {
          textRuns: [
            { text: "Sample", font: { bold: true, color: "#00B0F0", size: 14, italic: true } },
            { text: "1", font: { subscript: true } },
            { text: "String" },
            { text: "2", font: { superscript: true, color: "black", tintAndShade: 0.5 } },
          ],
        };
    
        cellSrc.clear(Excel.ClearApplyTo.all);
        cellSrc.setCellProperties([[cellSrcTextRun]]);
        await context.sync();
    
        const textRunCellProperty = cellSrc.getCellProperties({ textRuns: true });
        await context.sync();
    
        const cellTextRuns = textRunCellProperty.value[0][0].textRuns;
        console.log(JSON.stringify(cellTextRuns, undefined, "  "));
      });
    })();

    If you have any further questions or feedback, feel free to let us know!