Search code examples
javascriptexceloffice-jsexcel-addins

Is there a way to change the color of a line shape in an Office.js Excel add in?


I have tried the shape.fill.setSolidColor("color"); method as well as fill.setForegroundColor = 'color'; property. Neither of these seems to work and at times throw errors. Can a line shape be changed from its default blue color in excel add-ins? Much appreciated.


Solution

  • Welcome to Office JS world, Yes, the line shape color can be changed by Shape.lineFormat API. You may try the following sample code in the script lab

    async function addStraightLine() {
      await Excel.run(async (context) => {
        const shapes = context.workbook.worksheets.getItem("Shapes").shapes;
        const line = shapes.addLine(200, 50, 300, 150, Excel.ConnectorType.straight);
        line.lineFormat.color = "red";
        line.name = "StraightLine";
        await context.sync();
      });
    }
    

    Reference document can be found https://learn.microsoft.com/en-us/javascript/api/excel/excel.shapelineformat?view=excel-js-preview