Office JS - Excel,添加透视图

bsxbgnwa  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(140)

如何使用office.js添加数据透视图?
我可以看到图表的 pivotOptions 属性(https://learn.microsoft.com/en-us/javascript/api/excel/excel. chart?view=excel-js-preview#excel-excel-chart-pivotoptions-member),但我找不到一个清晰的数据透视图创建的例子,类似于其他文档页面(即. https://learn.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-pivottables
这可能吗?你能示范一下吗?

a11xaf1n

a11xaf1n1#

试试看:

Excel.run(function (context) {
    // Get the active worksheet
    var sheet = context.workbook.worksheets.getActiveWorksheet();
    // Define the data range for the PivotTable
    var dataRange = sheet.getRange("A1:C10");
    // Create a PivotTable based on the data range
    var pivotTable = sheet.pivotTables.add("A15", dataRange, true);
    // Define the data range for the PivotChart
    var chartRange = sheet.getRange("E1:F10");
    // Create a PivotChart based on the PivotTable and data range
    var pivotChart = sheet.charts.add("A30", Excel.ChartType.columnClustered, pivotTable);
    pivotChart.setSourceData(chartRange);
    // Synchronize the changes with the workbook
    return context.sync();
}).catch(function (error) {
    console.log(error);
});

相关问题