Код: Выделить всё
function updateChartLinks() {
// Replace with the ID of the presentation you want to update
var presentationId = "abcdefg";
// Replace with the ID of the new spreadsheet you want to link to
var spreadsheetId = "abcdefg";
var ss2 = SpreadsheetApp.openById(spreadsheetId);
var sheet2 = ss2.getSheets()[0];
var chart2 = sheet2.getCharts()[0];
Logger.log('spreadsheet2 name is ' + ss2.getName());
// get all charts from all sheets of the spreadsheet copy
var sheets2 = ss2.getSheets();
var allCharts = []; // keep track of all charts on all sheets of the spreadsheet
var allChartsIds = []; // keep track of the ids for all the charts
var chartsnum = 0;
for (var i = 0; i < sheets2.length; i++) {
var curSheet2 = sheets2[i];
if (curSheet2) { // Check if curSheet2 is defined
var charts2 = curSheet2.getCharts();
for (var j = 0; j < charts2.length; j++) {
var curChart2 = charts2[j];
allCharts[chartsnum] = curChart2;
allChartsIds[chartsnum] = curChart2.getChartId();
chartsnum++;
Logger.log('\n chart2 Id is ' + curChart2.getChartId());
}
}
}
//total number of charts in all sheets of the spreadsheet
var lengthAllCharts = allCharts.length;
var pres2 = SlidesApp.openById(presentationId);
Logger.log('pres2 name is ' + pres2.getName());
var slides2 = pres2.getSlides();
for (var i = 0; i < slides2.length; i++) {
var slide = slides2[i];
var shapes = slide.getShapes();
shapes.forEach(shape => {
if (shape.getShapeType() == SlidesApp.ShapeType.CHART) {
var chart = shape.asChart();
var chartData = chart.getChartData();
// Assuming the charts are in the same order as in the new spreadsheet
var newChart = allCharts.shift();
chartData.setDataSourceId(newChart.getChartId());
chart.setChartData(chartData);
// Force a refresh of the chart data
chart.refresh();
}
});
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... kbook-copy