Excel.ChartCollection class
A collection of all the chart objects on a worksheet.
- Extends
Remarks
Properties
context | The request context associated with the object. This connects the add-in's process to the Office host application's process. |
count | Returns the number of charts in the worksheet. |
items | Gets the loaded child items in this collection. |
Methods
add(type, source |
Creates a new chart. |
add(type |
Creates a new chart. |
get |
Returns the number of charts in the worksheet. |
get |
Gets a chart using its name. If there are multiple charts with the same name, the first one will be returned. |
get |
Gets a chart based on its position in the collection. |
get |
Gets a chart using its name. If there are multiple charts with the same name, the first one will be returned. If the chart doesn't exist, then this method returns an object with its |
load(options) | Queues up a command to load the specified properties of the object. You must call |
load(property |
Queues up a command to load the specified properties of the object. You must call |
load(property |
Queues up a command to load the specified properties of the object. You must call |
toJSON() | Overrides the JavaScript |
Events
on |
Occurs when a chart is activated. |
on |
Occurs when a new chart is added to the worksheet. |
on |
Occurs when a chart is deactivated. |
on |
Occurs when a chart is deleted. |
Property Details
context
The request context associated with the object. This connects the add-in's process to the Office host application's process.
context: RequestContext;
Property Value
count
Returns the number of charts in the worksheet.
readonly count: number;
Property Value
number
Remarks
items
Gets the loaded child items in this collection.
readonly items: Excel.Chart[];
Property Value
Method Details
add(type, sourceData, seriesBy)
Creates a new chart.
add(type: Excel.ChartType, sourceData: Range, seriesBy?: Excel.ChartSeriesBy): Excel.Chart;
Parameters
- type
- Excel.ChartType
Represents the type of a chart. See Excel.ChartType
for details.
- sourceData
- Excel.Range
The Range
object corresponding to the source data.
- seriesBy
- Excel.ChartSeriesBy
Optional. Specifies the way columns or rows are used as data series on the chart. See Excel.ChartSeriesBy
for details.
Returns
Remarks
Examples
// Add a chart of chartType "ColumnClustered" on worksheet "Charts"
// with sourceData from range "A1:B4" and seriesBy set to "auto".
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Sheet1");
const rangeSelection = "A1:B4";
const range = sheet.getRange(rangeSelection);
const chart = sheet.charts.add(
Excel.ChartType.columnClustered,
range,
Excel.ChartSeriesBy.auto);
await context.sync();
console.log("New Chart Added");
});
add(typeString, sourceData, seriesByString)
Creates a new chart.
add(typeString: "Invalid" | "ColumnClustered" | "ColumnStacked" | "ColumnStacked100" | "3DColumnClustered" | "3DColumnStacked" | "3DColumnStacked100" | "BarClustered" | "BarStacked" | "BarStacked100" | "3DBarClustered" | "3DBarStacked" | "3DBarStacked100" | "LineStacked" | "LineStacked100" | "LineMarkers" | "LineMarkersStacked" | "LineMarkersStacked100" | "PieOfPie" | "PieExploded" | "3DPieExploded" | "BarOfPie" | "XYScatterSmooth" | "XYScatterSmoothNoMarkers" | "XYScatterLines" | "XYScatterLinesNoMarkers" | "AreaStacked" | "AreaStacked100" | "3DAreaStacked" | "3DAreaStacked100" | "DoughnutExploded" | "RadarMarkers" | "RadarFilled" | "Surface" | "SurfaceWireframe" | "SurfaceTopView" | "SurfaceTopViewWireframe" | "Bubble" | "Bubble3DEffect" | "StockHLC" | "StockOHLC" | "StockVHLC" | "StockVOHLC" | "CylinderColClustered" | "CylinderColStacked" | "CylinderColStacked100" | "CylinderBarClustered" | "CylinderBarStacked" | "CylinderBarStacked100" | "CylinderCol" | "ConeColClustered" | "ConeColStacked" | "ConeColStacked100" | "ConeBarClustered" | "ConeBarStacked" | "ConeBarStacked100" | "ConeCol" | "PyramidColClustered" | "PyramidColStacked" | "PyramidColStacked100" | "PyramidBarClustered" | "PyramidBarStacked" | "PyramidBarStacked100" | "PyramidCol" | "3DColumn" | "Line" | "3DLine" | "3DPie" | "Pie" | "XYScatter" | "3DArea" | "Area" | "Doughnut" | "Radar" | "Histogram" | "Boxwhisker" | "Pareto" | "RegionMap" | "Treemap" | "Waterfall" | "Sunburst" | "Funnel", sourceData: Range, seriesByString?: "Auto" | "Columns" | "Rows"): Excel.Chart;
Parameters
- typeString
-
"Invalid" | "ColumnClustered" | "ColumnStacked" | "ColumnStacked100" | "3DColumnClustered" | "3DColumnStacked" | "3DColumnStacked100" | "BarClustered" | "BarStacked" | "BarStacked100" | "3DBarClustered" | "3DBarStacked" | "3DBarStacked100" | "LineStacked" | "LineStacked100" | "LineMarkers" | "LineMarkersStacked" | "LineMarkersStacked100" | "PieOfPie" | "PieExploded" | "3DPieExploded" | "BarOfPie" | "XYScatterSmooth" | "XYScatterSmoothNoMarkers" | "XYScatterLines" | "XYScatterLinesNoMarkers" | "AreaStacked" | "AreaStacked100" | "3DAreaStacked" | "3DAreaStacked100" | "DoughnutExploded" | "RadarMarkers" | "RadarFilled" | "Surface" | "SurfaceWireframe" | "SurfaceTopView" | "SurfaceTopViewWireframe" | "Bubble" | "Bubble3DEffect" | "StockHLC" | "StockOHLC" | "StockVHLC" | "StockVOHLC" | "CylinderColClustered" | "CylinderColStacked" | "CylinderColStacked100" | "CylinderBarClustered" | "CylinderBarStacked" | "CylinderBarStacked100" | "CylinderCol" | "ConeColClustered" | "ConeColStacked" | "ConeColStacked100" | "ConeBarClustered" | "ConeBarStacked" | "ConeBarStacked100" | "ConeCol" | "PyramidColClustered" | "PyramidColStacked" | "PyramidColStacked100" | "PyramidBarClustered" | "PyramidBarStacked" | "PyramidBarStacked100" | "PyramidCol" | "3DColumn" | "Line" | "3DLine" | "3DPie" | "Pie" | "XYScatter" | "3DArea" | "Area" | "Doughnut" | "Radar" | "Histogram" | "Boxwhisker" | "Pareto" | "RegionMap" | "Treemap" | "Waterfall" | "Sunburst" | "Funnel"
Represents the type of a chart. See Excel.ChartType
for details.
- sourceData
- Excel.Range
The Range
object corresponding to the source data.
- seriesByString
-
"Auto" | "Columns" | "Rows"
Optional. Specifies the way columns or rows are used as data series on the chart. See Excel.ChartSeriesBy
for details.
Returns
Remarks
getCount()
Returns the number of charts in the worksheet.
getCount(): OfficeExtension.ClientResult<number>;
Returns
OfficeExtension.ClientResult<number>
Remarks
getItem(name)
Gets a chart using its name. If there are multiple charts with the same name, the first one will be returned.
getItem(name: string): Excel.Chart;
Parameters
- name
-
string
Name of the chart to be retrieved.
Returns
Remarks
Examples
// Get the number of charts.
await Excel.run(async (context) => {
const charts = context.workbook.worksheets.getItem("Sheet1").charts;
charts.load('count');
await context.sync();
console.log("charts: Count= " + charts.count);
});
getItemAt(index)
Gets a chart based on its position in the collection.
getItemAt(index: number): Excel.Chart;
Parameters
- index
-
number
Index value of the object to be retrieved. Zero-indexed.
Returns
Remarks
Examples
await Excel.run(async (context) => {
const lastPosition = context.workbook.worksheets.getItem("Sheet1").charts.count - 1;
const chart = context.workbook.worksheets.getItem("Sheet1").charts.getItemAt(lastPosition);
await context.sync();
console.log(chart.name);
});
getItemOrNullObject(name)
Gets a chart using its name. If there are multiple charts with the same name, the first one will be returned. If the chart doesn't exist, then this method returns an object with its isNullObject
property set to true
. For further information, see *OrNullObject methods and properties.
getItemOrNullObject(name: string): Excel.Chart;
Parameters
- name
-
string
Name of the chart to be retrieved.
Returns
Remarks
load(options)
Queues up a command to load the specified properties of the object. You must call context.sync()
before reading the properties.
load(options?: Excel.Interfaces.ChartCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.ChartCollection;
Parameters
Provides options for which properties of the object to load.
Returns
load(propertyNames)
Queues up a command to load the specified properties of the object. You must call context.sync()
before reading the properties.
load(propertyNames?: string | string[]): Excel.ChartCollection;
Parameters
- propertyNames
-
string | string[]
A comma-delimited string or an array of strings that specify the properties to load.
Returns
Examples
await Excel.run(async (context) => {
const charts = context.workbook.worksheets.getItem("Sheet1").charts;
charts.load('items');
await context.sync();
for (let i = 0; i < charts.items.length; i++) {
console.log(charts.items[i].name);
}
});
load(propertyNamesAndPaths)
Queues up a command to load the specified properties of the object. You must call context.sync()
before reading the properties.
load(propertyNamesAndPaths?: OfficeExtension.LoadOption): Excel.ChartCollection;
Parameters
- propertyNamesAndPaths
- OfficeExtension.LoadOption
propertyNamesAndPaths.select
is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand
is a comma-delimited string that specifies the navigation properties to load.
Returns
toJSON()
Overrides the JavaScript toJSON()
method in order to provide more useful output when an API object is passed to JSON.stringify()
. (JSON.stringify
, in turn, calls the toJSON
method of the object that is passed to it.) Whereas the original Excel.ChartCollection
object is an API object, the toJSON
method returns a plain JavaScript object (typed as Excel.Interfaces.ChartCollectionData
) that contains an "items" array with shallow copies of any loaded properties from the collection's items.
toJSON(): Excel.Interfaces.ChartCollectionData;
Returns
Event Details
onActivated
Occurs when a chart is activated.
readonly onActivated: OfficeExtension.EventHandlers<Excel.ChartActivatedEventArgs>;
Event Type
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/30-events/events-chart-activated.yaml
await Excel.run(async (context) => {
const pieChart = context.workbook.worksheets.getActiveWorksheet().charts.getItem("Pie");
// Register the onActivated and onDeactivated event handlers.
pieChart.onActivated.add(chartActivated);
pieChart.onDeactivated.add(chartDeactivated);
await context.sync();
console.log("Added handlers for Chart onActivated and onDeactivated events.");
});
...
async function chartActivated(event) {
await Excel.run(async (context) => {
// Retrieve the worksheet.
const sheet = context.workbook.worksheets.getActiveWorksheet();
// Retrieve the activated chart by ID and load the name of the chart.
const activatedChart = sheet.charts.getItem(event.chartId);
activatedChart.load(["name"]);
await context.sync();
// Print out the activated chart's data.
console.log(`A chart was activated. ID: ${event.chartId}. Chart name: ${activatedChart.name}.`);
});
}
onAdded
Occurs when a new chart is added to the worksheet.
readonly onAdded: OfficeExtension.EventHandlers<Excel.ChartAddedEventArgs>;
Event Type
Remarks
Examples
await Excel.run(async (context) => {
context.workbook.worksheets.getActiveWorksheet()
.charts.onAdded.add(function (event) {
return Excel.run(async (context) => {
console.log("A chart has been added with ID: " + event.chartId);
await context.sync();
});
});
await context.sync();
});
onDeactivated
Occurs when a chart is deactivated.
readonly onDeactivated: OfficeExtension.EventHandlers<Excel.ChartDeactivatedEventArgs>;
Event Type
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/30-events/events-chart-activated.yaml
await Excel.run(async (context) => {
const pieChart = context.workbook.worksheets.getActiveWorksheet().charts.getItem("Pie");
// Register the onActivated and onDeactivated event handlers.
pieChart.onActivated.add(chartActivated);
pieChart.onDeactivated.add(chartDeactivated);
await context.sync();
console.log("Added handlers for Chart onActivated and onDeactivated events.");
});
...
async function chartDeactivated(event) {
await Excel.run(async (context) => {
// Callback function for when the chart is deactivated.
console.log("The pie chart is NOT active.");
});
}
onDeleted
Occurs when a chart is deleted.
readonly onDeleted: OfficeExtension.EventHandlers<Excel.ChartDeletedEventArgs>;
Event Type
Remarks
Examples
await Excel.run(async (context) => {
context.workbook.worksheets.getActiveWorksheet()
.charts.onDeleted.add(function (event) {
return Excel.run(async (context) => {
console.log("The chart with this ID was deleted: " + event.chartId);
await context.sync();
});
});
await context.sync();
});
Office Add-ins