Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsgoogle-sheets-apigoogle-slides-api

Is there a way (or workaround) to add tables from Google Sheets to a Google Slides presentation using app script?


I'm new to app script and coding in general.

I've written a script that uses an "on form submit" trigger to create a new folder in my drive, copy three (3) template files and drop them in there:

  • Two (2) of these are spreadsheets and the other is a Slides presentation.

I have tables in the spreadsheets that I want to embed and have update automatically into the presentation.

It looks like I can only embed charts from a spreadsheet into a presentation. Is this true?

If so, can I call on certain cells in the spreadsheet and get their values; then, place those in a pre-made table on the slides presentation?

Any other workarounds that I may be missing?

My group does this process any where between 8-30 times a day, so any way to automate this part of it would save a lot of time!

Thanks!


Solution

  • A Table from Spreadsheet to Slide

    function putTablesIntoSlides() {
      var pr=SlidesApp.openById('Presentation Id');
      var slide1_ObjectId=pr.getSlides()[1].getObjectId();//I was adding the table to slide[1];
      Logger.log(slide1_ObjectId);
      var ss=SpreadsheetApp.getActive();
      var sh=ss.getSheetByName('Sheet10');
      var rg=sh.getDataRange();
      var v=rg.getValues();
      //Create a table the size comes from Spreadsheet Data
      var request1={"createTable": {"elementProperties":{"pageObjectId": slide1_ObjectId},"rows": v.length,"columns": v[0].length}};
      var resp1=Slides.Presentations.batchUpdate({requests:request1}, pr.getId());
      //Setting up the data for adding text to the above table.
      var request2=[];
      for(var i=0;i<v.length;i++) {
        for(var j=0;j<v[i].length;j++) {
          request2.push({"insertText":{"objectId":resp1.replies[0].createTable.objectId,"cellLocation": {"rowIndex":i,"columnIndex":j},"text": v[i][j].toString()}});
        }
      }
      //Add test to all of the table cells
      var resp2=Slides.Presentations.batchUpdate({requests:request2}, pr.getId());
      Logger.log(JSON.stringify(resp1));   
    }
    

    This is my table data:

    HDR1 HDR6 HDR2 HDR5 HDR4 HDR7 HDR8 HDR3
    5 7 2 6 4 3 1 0
    7 4 8 2 3 1 7 8
    5 7 5 0 0 9 1 2
    2 4 9 9 7 3 7 2
    1 0 3 3 5 3 9 7
    3 7 8 3 2 4 1 8
    5 8 6 3 1 1 8 4
    3 1 7 7 1 5 7 5
    9 6 3 6 8 6 9 8
    5 4 9 6 2 3 3 4

    Google Slides API

    SlidesApp