title: Google Sheets Script - for linking tables
body: <p>I have a Google Slide document which has both charts and tables linked to a Google sheet document.</p>
  
  <p>At times I need to create a copy of both and then need to link the Slide charts and tables to the newly copied Sheets.</p>
  
  <p>I have the following script which does this for the charts, but not for the tables. Would anyone be able to help me add the code to which will also adjust the tables? </p>
  
  <p>Very much appreciated</p>
  
  <pre><code>//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="1lH8hT4sfTCsHBfTKP_ldA7hL-3y8hgpneWCAxqJJpX8"; // enter your slide id here
    var spreadsheetId="1xk9__Ftpic2piUD4zZtYkcaUSE4NaerRH0yV-JuJgb0"; // 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 &lt; sheets2.length; i++) {
      var curSheet2=sheets2[i];
      var charts2 = curSheet2.getCharts();
          for (var j = 0; j &lt; 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 &lt; slides2.length; i++) {
      var curSlide2=slides2[i];
      var charts2 = curSlide2.getSheetsCharts();
          for (var j = 0; j &lt; 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 &lt; 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);
          }
  
    }
  
  }```
  </code></pre>