Excel.ChartCollection class

工作表中的所有 chart 对象的集合。A collection of all the chart objects on a worksheet.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

Extends

属性

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。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 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

items

获取此集合中已加载的子项。Gets the loaded child items in this collection.

方法

add(type, sourceData, seriesBy)

创建新图表。Creates a new chart.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

add(typeString, sourceData, seriesBy)

创建新图表。Creates a new chart.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

getCount()

返回工作表中的图表数。Returns the number of charts in the worksheet.

[API 集: ExcelApi 1.4][ 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 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

getItemAt(index)

根据其在集合中的位置获取图表。Gets a chart based on its position in the collection.

[API 集: ExcelApi 1.1][ 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. 如果图表不存在, 将返回一个 null 对象。If the chart does not exist, will return a null object.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

load(option)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

load(propertyNames)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

load(option)
toJSON()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始Excel.ChartCollection对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.ChartCollectionData, 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), 其中包含集合的项中所有加载属性的浅表副本的 "items" 数组。) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

事件

onActivated

在激活图表时发生。Occurs when a chart is activated.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

onAdded

将新图表添加到工作表时发生此事件。Occurs when a new chart is added to the worksheet.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

onDeactivated

当停用图表时发生此事件。Occurs when a chart is deactivated.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

onDeleted

在删除图表时发生。Occurs when a chart is deleted.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

属性详细信息

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。This connects the add-in's process to the Office host application's process.

context: RequestContext;

属性值

RequestContext

count

返回工作表中的图表数。Returns the number of charts in the worksheet. 只读。Read-only.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

readonly count: number;

属性值

number

items

获取此集合中已加载的子项。Gets the loaded child items in this collection.

readonly items: Excel.Chart[];

属性值

Excel.Chart[]

方法详细信息

add(type, sourceData, seriesBy)

创建新图表。Creates a new chart.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

add(type: Excel.ChartType, sourceData: Range, seriesBy?: Excel.ChartSeriesBy): Excel.Chart;

参数

type
Excel.ChartType

表示图表的类型。Represents the type of a chart. 有关详细信息, 请参阅 ChartType。See Excel.ChartType for details.

sourceData
Range

对应于源数据的 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. 有关详细信息, 请参阅 ChartSeriesBy。See Excel.ChartSeriesBy for details.

返回

add(typeString, sourceData, seriesBy)

创建新图表。Creates a new chart.

[API 集: ExcelApi 1.1][ 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;

参数

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. 有关详细信息, 请参阅 ChartType。See Excel.ChartType for details.

sourceData
Range

对应于源数据的 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. 有关详细信息, 请参阅 ChartSeriesBy。See Excel.ChartSeriesBy for details.

返回

示例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 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

getCount(): OfficeExtension.ClientResult<number>;

返回

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 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

getItem(name: string): Excel.Chart;

参数

name
string

要检索的图表的名称。Name of the chart to be retrieved.

返回

示例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 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

getItemAt(index: number): Excel.Chart;

参数

index
number

要检索的对象的索引值。Index value of the object to be retrieved. 从零开始编制索引。Zero-indexed.

返回

示例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. 如果图表不存在, 将返回一个 null 对象。If the chart does not exist, will return a null object.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

getItemOrNullObject(name: string): Excel.Chart;

参数

name
string

要检索的图表的名称。Name of the chart to be retrieved.

返回

load(option)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

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

参数

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

返回

注解

除了此签名之外, 此方法还具有以下签名:In addition to this signature, this method has the following signatures:

load(option?: string | string[]): Excel.ChartCollection-Where 选项是一个逗号分隔的字符串或一个字符串数组, 用于指定要加载的属性。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 选项。选择是一个用逗号分隔的字符串, 用于指定要加载的属性和选项。 expand 是一个用逗号分隔的字符串, 用于指定要加载的导航属性。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-仅适用于集合类型。load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.ChartCollection - Only available on collection types. 它类似于前面的签名。It is similar to the preceding signature. top 指定结果中可包含的集合项的最大数量。Option.top specifies the maximum number of collection items that can be included in the result. 选项。 skip 指定要跳过且不包含在结果中的项目数。Option.skip specifies the number of items that are to be skipped and not included in the result. 如果指定了 top 选项, 则在跳过指定数目的项目后将启动结果集。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. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

load(propertyNames?: string | string[]): Excel.ChartCollection;

参数

propertyNames
string | string[]

一个以逗号分隔的字符串或字符串数组, 用于指定要加载的属性。A comma-delimited string or an array of strings that specify the properties to load.

返回

示例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;

参数

返回

toJSON()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始Excel.ChartCollection对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.ChartCollectionData, 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), 其中包含集合的项中所有加载属性的浅表副本的 "items" 数组。) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

toJSON(): Excel.Interfaces.ChartCollectionData;

返回

事件详细信息

onActivated

在激活图表时发生。Occurs when a chart is activated.

[API 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

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

返回

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 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

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

返回

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 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

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

返回

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 集: ExcelApi 1.8][ API set: ExcelApi 1.8 ]

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

返回

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