Excel.ChartCollection class

A collection of all the chart objects on a worksheet.

[ API set: ExcelApi 1.1 ]

Extends
OfficeExtension.ClientObject

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(options)

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(propertyNamesAndPaths)

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

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

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

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

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));
    }
});

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

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

options
Excel.Interfaces.ChartCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions

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

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

[ 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();
});