Google表格脚本-用于链接表格

时间:2020-03-24 16:52:22

标签: google-apps-script google-sheets

我有一个Google幻灯片文档,其中的图表和表格都链接到Google表格文档。

有时我需要创建两者的副本,然后需要将幻灯片图表和表格链接到新复制的表格。

我有以下脚本,该脚本针对图表执行此操作,但不针对表格执行此操作。有人能帮我添加代码吗?

非常感谢

//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 < 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);
        }

  }

}```

1 个答案:

答案 0 :(得分:0)

我了解您想将表格从工作表副本插入到幻灯片副本中,如果是这种情况,将无法完成,因为没有任何方法可以支持,您可以在另一张幻灯片中插入表格副本与insertTable一起使用,但不能从工作表中复制表格。您可以打开功能请求以在Google的Issue Tracker中实现该行为。