'Duplicate a Google Slide connected to a Google Sheet to a new instance
I have a Google Slide where all its charts are generated via a linked Google Sheet.
At the moment, when I duplicate "Google Slide A" that is connected to "Google Sheet A", the duplicated "Copy of Google Slide A" remains connected to "Google Sheet A", instead of being connected to "Copy of Google Sheet A" - Which is how I want it to be.
I found this line of code from Google Script that solves the problem but can only duplicate the google sheet charts, not the tables.
I want to add the same function for the tables.
Could anyone help me add to this code the same possibility for tables ? (I have 0 coding knowledge)
Kindly find the code below:
//this updates all charts embedded from sheets on all slides of the specified slides id to a specified spreadsheet id
//Intended for use immediately after duplicating both the slide/presentation and the spreadsheet
//For now you must manually enter / cut and paste the ID of both files directly below in the two Id variables inside the quotes
function myFunction() {
//sheet and slide id's - the charts in the slide id listed here will be linked to the the sheet id listed here:
var slideId="435345345345345345345345345345345345"; // enter your slide id here
var spreadsheetId="23423423423423423423423423423434234234"; // enter your sheet id here
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];
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(slideId);
Logger.log('pres2 name is ' + pres2.getName());
var slides2=pres2.getSlides();
for (var i = 0; i < slides2.length; i++) {
var curSlide2=slides2[i];
var charts2 = curSlide2.getSheetsCharts();
for (var j = 0; j < charts2.length; j++) {
var curChart2=charts2[j];
var chartHeight=curChart2.getHeight();
var chartWidth=curChart2.getWidth();
var chartLeft=curChart2.getLeft();
var chartTop=curChart2.getTop();
for (var k = 0; k < lengthAllCharts; k++) {
if (curChart2.getChartId()==allChartsIds[k]){
var chart2=allCharts[k];
break;
}
}
Logger.log('\n chart2 ObjId is ' + curChart2.getObjectId());
Logger.log('\n chart2 ChartId is ' + curChart2.getChartId());
Logger.log('\n chart2 Chart data ' + curChart2.getSpreadsheetId());
curChart2.remove();
curSlide2.insertSheetsChart(chart2, chartLeft, chartTop, chartWidth, chartHeight);
}
}
}
Thank you very much for your help.
Kikuchi
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
