我认为 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);
});
});
}
});