Refresh Google Sheets data on Google Slides via Apps Script

1.1k views Asked by At

We're currently trying to automate the 'updating' of data between Google Slides and Google Sheets. Currently within Google Slides, I have an imported linked table from Google Sheets. I'd hoped it was possible to trigger the updating of the data via Apps Script, but having difficulty in getting this to work. It appears to be possible via the API, but not necessarily through Apps Script.

Does anyone have a solution?

Thanks

1

There are 1 answers

0
Cooper On

I've just started to learn about that very thing.

I've been working with a slide presentation that has only images and text and here's a function I wrote yesterday to get me the objectId's of the text that i wanted to know how to update.

function logSlidesAndElements() {
  var presentationId='presentationID';
  var presentation=Slides.Presentations.get(presentationId);
  var slides=presentation.slides
  var s='';
  var br='<br />';
  s+=br + Utilities.formatString('The presentation contains %s slides:', slides.length);
  for (var i=0;i<slides.length;i++) 
  {
     s+=br + Utilities.formatString('slides[%s].objectId=%s',i,slides[i].objectId); 
     for(var j=0;j<slides[i].pageElements.length;j++)
     {
       s+=br + Utilities.formatString('slides[%s].pageElements[%s].objectId: %s',i,j,slides[i].pageElements[j].objectId);
       if(typeof(slides[i].pageElements[j].description)!='undefined')
       {
         s+=br + Utilities.formatString('slides[%s].pageElement[%s]=%s',i,j,(typeof(slides[i].pageElements[j].description)!='undefined')?slides[i].pageElements[j].description:'');
       }
       if(slides[i].pageElements[j].shape && slides[i].pageElements[j].shape.shapeType && slides[i].pageElements[j].shape.shapeType=='TEXT_BOX')
       {
          if(slides[i].pageElements[j].shape && slides[i].pageElements[j].shape.text && slides[i].pageElements[j].shape.text.textElements.length)
          {
            for(var k=0;k<slides[i].pageElements[j].shape.text.textElements.length;k++)
            {
              if(slides[i].pageElements[j].shape.text.textElements[k] && slides[i].pageElements[j].shape.text.textElements[k].textRun && slides[i].pageElements[j].shape.text.textElements[k].textRun.content)
              {
                s+=br + Utilities.formatString('slides[%s].pageElements[%s].shape.text.textElements[%s].textRun.content=%s',i,j,k,slides[i].pageElements[j].shape.text.textElements[k].textRun.content); 
                s+=br + Utilities.formatString('slides[%s].pageElements[%s].objectId: %s',i,j,slides[i].pageElements[j].objectId);
              }
            }
          }
        }
        if(slides[i].pageElements[j].image)
        {
          s+=br + Utilities.formatString('slides[%s].pageElements[%s].objectId: %s',i,j,slides[i].pageElements[j].objectId);
        }
      }
      s+=br;
   }
   var ui=HtmlService.createHtmlOutput(s).setWidth(1000).setHeight(450);
   SpreadsheetApp.getUi().showModelessDialog(ui, 'HillViewDental Presentation');
}

I played around a lot with the API explorer learning about how to setup the requests and just this morning began to have some success with it. I was able to delete all of the text of one slide and replace it with these two functions.

function insertText()
{
  Slides.Presentations.batchUpdate({"requests": [{"insertText": {"objectId": "g24899cf468_0_47","insertionIndex": 0,"text": "This is going to slide 3"}}]}, "1tdCmlhYt8gNgBBa5EvmSlg7yL5_V1irK8RdL2YQRLUE");
}

function deleteText()
{
  Slides.Presentations.batchUpdate({"requests": [{"deleteText": {"objectId": "g24899cf468_0_47","textRange": {"type": "ALL"}}}]}, "1tdCmlhYt8gNgBBa5EvmSlg7yL5_V1irK8RdL2YQRLUE");
}

This is what the output looks like of the logSlidesAndElements() function

enter image description here

I know it's not very refined but it's taken me quite a lot of research to get this far and now I'm starting to see some hope at the end of the tunnel. I'm guessing that there are others on this site that know more than I but I noticed your question had been here a while so I thought I'd share my progress with you as I know how frustrating it can be.