Excel.ChartCollection class

A collection of all the chart objects on a worksheet.

[ API set: ExcelApi 1.1 ]

Extends

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. Read-only.

[ API set: ExcelApi 1.1 ]

items

Gets the loaded child items in this collection.

Methods

add(type, sourceData, seriesBy)

Creates a new chart.

[ API set: ExcelApi 1.1 ]

add(typeString, sourceData, seriesBy)

Creates a new chart.

[ API set: ExcelApi 1.1 ]

getCount()

Returns the number of charts in the worksheet.

[ API set: ExcelApi 1.4 ]

getItem(name)

Gets a chart using its name. If there are multiple charts with the same name, the first one will be returned.

[ API set: ExcelApi 1.1 ]

getItemAt(index)

Gets a chart based on its position in the collection.

[ API set: ExcelApi 1.1 ]

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 does not exist, will return a null object.

[ API set: ExcelApi 1.4 ]

load(option)

Queues up a command to load the specified properties of the object. You must call "context.sync()" before reading the properties.

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(option)
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.

Events

onActivated

Occurs when a chart is activated.

[ API set: ExcelApi 1.8 ]

onAdded

Occurs when a new chart is added to the worksheet.

[ API set: ExcelApi 1.8 ]

onDeactivated

Occurs when a chart is deactivated.

[ API set: ExcelApi 1.8 ]

onDeleted

Occurs when a chart is deleted.

[ API set: ExcelApi 1.8 ]

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

RequestContext

count

Returns the number of charts in the worksheet. Read-only.

[ API set: ExcelApi 1.1 ]

readonly count: number;

Property Value

number

items

Gets the loaded child items in this collection.

readonly items: Excel.Chart[];

Property Value

Excel.Chart[]

Method Details

add(type, sourceData, seriesBy)

Creates a new chart.

[ API set: ExcelApi 1.1 ]

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
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

add(typeString, sourceData, seriesBy)

Creates a new chart.

[ API set: ExcelApi 1.1 ]

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, seriesBy?: "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
Range

The Range object corresponding to the source data.

seriesBy
"Auto" | "Columns" | "Rows"

Optional. Specifies the way columns or rows are used as data series on the chart. See Excel.ChartSeriesBy for details.

Returns

Examples

// Add a chart of chartType "ColumnClustered" on worksheet "Charts" 
// with sourceData from Range "A1:B4" and seriresBy is set to be "auto".
Excel.run(function (ctx) { 
    var rangeSelection = "A1:B4";
    var range = ctx.workbook.worksheets.getItem(sheetName)
        .getRange(rangeSelection);
    var chart = ctx.workbook.worksheets.getItem(sheetName)
        .charts.add("ColumnClustered", range, "auto");    return ctx.sync().then(function() {
            console.log("New Chart Added");
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

getCount()

Returns the number of charts in the worksheet.

[ API set: ExcelApi 1.4 ]

getCount(): OfficeExtension.ClientResult<number>;

Returns

OfficeExtension.ClientResult<number>

getItem(name)

Gets a chart using its name. If there are multiple charts with the same name, the first one will be returned.

[ API set: ExcelApi 1.1 ]

getItem(name: string): Excel.Chart;

Parameters

name
string

Name of the chart to be retrieved.

Returns

Examples

Excel.run(function (ctx) { 
    var chartname = 'Chart1';
    var chart = ctx.workbook.worksheets.getItem("Sheet1").charts.getItem(chartname);
    return ctx.sync().then(function() {
            console.log(chart.height);
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
Excel.run(function (ctx) { 
    var chartId = 'SampleChartId';
    var chart = ctx.workbook.worksheets.getItem("Sheet1").charts.getItem(chartId);
    return ctx.sync().then(function() {
            console.log(chart.height);
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
Excel.run(function (ctx) { 
    var lastPosition = ctx.workbook.worksheets.getItem("Sheet1").charts.count - 1;
    var chart = ctx.workbook.worksheets.getItem("Sheet1").charts.getItemAt(lastPosition);
    return ctx.sync().then(function() {
            console.log(chart.name);
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

getItemAt(index)

Gets a chart based on its position in the collection.

[ API set: ExcelApi 1.1 ]

getItemAt(index: number): Excel.Chart;

Parameters

index
number

Index value of the object to be retrieved. Zero-indexed.

Returns

Examples

Excel.run(function (ctx) { 
    var lastPosition = ctx.workbook.worksheets.getItem("Sheet1").charts.count - 1;
    var chart = ctx.workbook.worksheets.getItem("Sheet1").charts.getItemAt(lastPosition);
    return ctx.sync().then(function() {
            console.log(chart.name);
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

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 does not exist, will return a null object.

[ API set: ExcelApi 1.4 ]

getItemOrNullObject(name: string): Excel.Chart;

Parameters

name
string

Name of the chart to be retrieved.

Returns

load(option)

Queues up a command to load the specified properties of the object. You must call "context.sync()" before reading the properties.

load(option?: Excel.Interfaces.ChartCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.ChartCollection;

Parameters

option
Excel.Interfaces.ChartCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions

Returns

Remarks

In addition to this signature, this method has the following signatures:

load(option?: string | string[]): Excel.ChartCollection - Where option is a comma-delimited string or an array of strings that specify the properties to load.

load(option?: { select?: string; expand?: string; }): Excel.ChartCollection - Where option.select is a comma-delimited string that specifies the properties to load, and options.expand is a comma-delimited string that specifies the navigation properties to load.

load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.ChartCollection - Only available on collection types. It is similar to the preceding signature. Option.top specifies the maximum number of collection items that can be included in the result. Option.skip specifies the number of items that are to be skipped and not included in the result. If option.top is specified, the result set will start after skipping the specified number of items.

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

Excel.run(function (ctx) { 
    var charts = ctx.workbook.worksheets.getItem("Sheet1").charts;
    charts.load('items');
    return ctx.sync().then(function() {
        for (var i = 0; i < charts.items.length; i++)
        {
            console.log(charts.items[i].name);
        }
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
// Get the number of charts
Excel.run(function (ctx) { 
    var charts = ctx.workbook.worksheets.getItem("Sheet1").charts;
    charts.load('count');
    return ctx.sync().then(function() {
        console.log("charts: Count= " + charts.count);
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

load(option)

load(option?: OfficeExtension.LoadOption): Excel.ChartCollection;

Parameters

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.

[ API set: ExcelApi 1.8 ]

readonly onActivated: OfficeExtension.EventHandlers<Excel.ChartActivatedEventArgs>;

Returns

OfficeExtension.EventHandlers<Excel.ChartActivatedEventArgs>

Examples

Excel.run(function (context){
    context.workbook.worksheets.getActiveWorksheet()
        .charts.onActivated.add(function (event) {
        return Excel.run((context) => {
            console.log("The ID of the active chart is: " + event.chartId)
            return context.sync();
        });
    });
    return context.sync();
});

onAdded

Occurs when a new chart is added to the worksheet.

[ API set: ExcelApi 1.8 ]

readonly onAdded: OfficeExtension.EventHandlers<Excel.ChartAddedEventArgs>;

Returns

OfficeExtension.EventHandlers<Excel.ChartAddedEventArgs>

Examples

Excel.run(function (context){
    context.workbook.worksheets.getActiveWorksheet()
        .charts.onAdded.add(function (event) {
        return Excel.run((context) => {
            console.log("A chart has been added with ID: " + event.chartId);
            return context.sync();
        });
    });
    return context.sync();
});

onDeactivated

Occurs when a chart is deactivated.

[ API set: ExcelApi 1.8 ]

readonly onDeactivated: OfficeExtension.EventHandlers<Excel.ChartDeactivatedEventArgs>;

Returns

OfficeExtension.EventHandlers<Excel.ChartDeactivatedEventArgs>

Examples

Excel.run(function (context){
    context.workbook.worksheets.getActiveWorksheet()
        .charts.onDeactivated.add(function (event) {
        return Excel.run((context) => {
            console.log("The chart with this ID was deactivated: " + event.chartId);
            return context.sync();
        });
    });
    return context.sync();
});

onDeleted

Occurs when a chart is deleted.

[ API set: ExcelApi 1.8 ]

readonly onDeleted: OfficeExtension.EventHandlers<Excel.ChartDeletedEventArgs>;

Returns

OfficeExtension.EventHandlers<Excel.ChartDeletedEventArgs>

Examples

Excel.run(function (context){
    context.workbook.worksheets.getActiveWorksheet()
        .charts.onDeleted.add(function (event) {
        return Excel.run((context) => {
            console.log("The chart with this ID was deleted: " + event.chartId);
            return context.sync();
        });
    });
    return context.sync();
});