通过office.js插件更新powerpoint中图表背后的数据

问题描述 投票:0回答:1

我正在使用新的 Office.js API for PowerPoint。我已检查了文档,但找不到我要找的内容。

我想知道是否可以使用 Office.js 更新现有图表背后的数据

office365 powerpoint ms-office office-js office-addins
1个回答
0
投票

我认为 Office.js 可能不会提供一种直接的方法来更改现有图表的数据源,但是,您可以通过创建一个新图表并用更新/新数据替换现有图表来实现:-

Office.onReady(function (info) {
    if (info.host === Office.HostType.PowerPoint) {
        // Register event handlers and interact with PowerPoint here
        $(document).ready(function () {
            // Replace "YourChartTitle" with the title of the chart you want to update.
            var chartTitle = "YourChartTitle";
            var newChartData = [
                [1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]
            ]; // Replace with your new data

            PowerPoint.run(function (context) {
                // Get the current slide
                var currentSlide = context.presentation.slides.getActiveSlide();

                // Get the existing chart
                var existingChart = currentSlide.shapes.getByName(chartTitle);

                // Delete the existing chart
                existingChart.delete();

                // Insert a new chart with the updated data
                var newChart = currentSlide.shapes.addChart(Office.MailboxEnums.ChartType.ColumnClustered, newChartData, "A1:C3");

                return context.sync();
            })
            .catch(function (error) {
                console.log(error);
            });
        });
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.