google-apps-scriptgoogle-slides

Problem interpreting how to copy / paste tables within a presentation using apps script


Apps script documentation appears to suggest it is possible to copy a table from one slide to another - but I can't seem to get it to work. This is the script I've used - any thoughbts on what I'm doing wrong? Here's a link to an example of it in "use".

 * @OnlyCurrentDoc
 */
/*
Instructions copied from https://developers.google.com/apps-script/reference/slides/slide#insertShape(ShapeType)
// Copy a table between presentations.
var otherPresentationSlide = SlidesApp.openById('presentationId').getSlides()[0];
var currentPresentationSlide = SlidesApp.getActivePresentation().getSlides()[0];
var table = otherPresentationSlide.getTables[0];
currentPresentationSlide.insertTable(table); // Also available for Layout, Master, and Page.*/

function test(){
var slide2 = SlidesApp.getActivePresentation().getSlides()[1];
var table = SlidesApp.getActivePresentation.getPageElementById('g2c6eee07fad_0_0').asTable();
slide2.insertTable(table); 

Solution

  • In your showing script, how about the following modification?

    From:

    var table = SlidesApp.getActivePresentation.getPageElementById('g2c6eee07fad_0_0').asTable();
    

    To:

    var table = SlidesApp.getActivePresentation().getPageElementById('g2c6eee07fad_0_0').asTable();
    

    SlidesApp.getActivePresentation is modified to SlidesApp.getActivePresentation().

    But, in this case, I think that SlidesApp.getActivePresentation() can be declared as a variable as follows.

    function test() {
      var slide = SlidesApp.getActivePresentation();
      var slide2 = slide.getSlides()[1];
      var table = slide.getPageElementById('g2c6eee07fad_0_0').asTable();
      slide2.insertTable(table);
    }
    

    Or, if you want to copy a table in the 1st page to 2nd page, the following modification might be able to be used.

    function test() {
      var slide = SlidesApp.getActivePresentation();
      var slides = slide.getSlides();
      var srcTable = slides[0].getTables()[0];
      var dst = slides[1];
      dst.insertTable(srcTable);
    }