At the Ignite Conference 2021, Microsoft announced new capabilities that bring a new JavaScript API to the excel spreadsheet app. Javascript API was first added in 2016 but now they have covered wide features that can be accessed and manipulated using these APIs.
This JavaScript API includes two JavaScript Object Models:
Excel JavaScript API
These are the application-specific API’s which provide strongly-typed objects that you can use to access worksheets, ranges, tables, charts, and more. These APIs are introduced in office 2016.
Common API
These APIs are used to access features such as UI, dialogs, and client settings that are common across multiple types of Office applications. These APIs are introduced in office 2013.
Excel Javascript APIs are available in both web and excel app.
Below are the areas where you can use new Excel JavaScript APIs:
- Charts
- Comments
- Conditional formatting
- Custom functions
- Data validation
- Events
- PivotTables
- Ranges and Cells
- RangeAreas (Multiple ranges)
- Shapes
- Tables
- Workbooks and Application-level APIs
- Worksheets
In this article, we will cover only charts example in JavaScript, as other features are not in our scope.
Creating a chart
The below example demonstrates chart creation using A1:B13 cells data available in a spreadsheet. It will create a line chart.
Excel.run(function (context) {
var sheet = context.workbook.worksheets.getItem("Sample");
var dataRange = sheet.getRange("A1:B13");
var chart = sheet.charts.add("Line", dataRange, "auto");
chart.title.text = "Sales Data";
chart.legend.position = "right"
chart.legend.format.fill.setSolidColor("white");
chart.dataLabels.format.font.size = 15;
chart.dataLabels.format.font.color = "black";
return context.sync();
}).catch(errorHandlerFunction);
It will produce:
Example Credits: Microsoft Docs
Microsoft has not yet declared the final release date of these APIs but to use this after release, you will need:
A valid Insiders: Beta build of Excel
Windows: greater than or equal to 16.0.14626.10000
Mac: greater than or equal to 16.55.21102600
The latest preview version of the js APIs
To check how previous versions of Javascript APIs work in excel, you can follow this great tutorial till the time this new feature gets released.