Shadi Almosri
Shadi Almosri

Reputation: 11989

How to embed a slide in a Google Doc using App Script

I am currently creating a google doc that provides an overview of all the slides in a presentation using App Scripts. At the moment for each slide I create a thumbnail image and add that to the google doc.

What would be better is an embed of the slide in the doc (so that things such as animations etc show up in the Google doc) - this would be similar to hitting copy on a slide and then paste in Google docs.

Would anyone know how this can be achieved using App Scripts?

For reference this is the function I made for the thumbnails:

// Gets thumbnail URL
function getThumbnail(presentation, slideId) {
  var presentationId = presentation.getId()
  var baseUrl = "https://slides.googleapis.com/v1/presentations/{presentationId}/pages/{pageObjectId}/thumbnail?thumbnailProperties.thumbnailSize=SMALL"
  var url = baseUrl
      .replace("{presentationId}", presentationId)
      .replace("{pageObjectId}", slideId);

  var parameters = {
    method: "GET",
    headers: { Authorization: "Bearer " + ScriptToken },
    contentType: "application/json",
    muteHttpExceptions: true
  };

  var response = JSON.parse(UrlFetchApp.fetch(url, parameters));
  return response.contentUrl
}

// Update Thumbnails in Doc
function updateThumbnailCell(row, contentUrl) {
  var thumbnailCell = row.getCell(THUMBNAIL_DOC_COLUMN)
  thumbnailCell.clear()
  var imageBlob = UrlFetchApp.fetch(contentUrl).getBlob()
  thumbnailCell.insertImage(0, imageBlob).setWidth(200);
}

Cheers,

Shadi

Upvotes: 0

Views: 632

Answers (2)

Emilio Scalise
Emilio Scalise

Reputation: 1

After a lot of testing I've found a dirty hack to get what we want.

If you copy and paste all slides to a google doc, it creates a number of InlineImages equal to each slides, and those images are linked slides. There is no way to do this using appscript.

Perhaps if you use those InlineImages, you can move them around and copy them as you wish.

So the trick is:

  • Copy all slides to a blank google doc.
  • Wait a couple of minutes, or you won't be able to get those InlineImages in appscript (very strange thing, but it is what it is)
  • Run a script like the one below to make your table with thumbnails of the slides and the slide speaker notes, replace the two "" with the presentation ID and the destination document ID.

This code is based off https://gist.github.com/msbarry/9862e10cf4113d8e325ee14ccd8f62c6#file-slidestodoc-gs and heavily modified.

var INPUT_PRESO_ID = "<id>";
var OUTPUT_DOC_ID = "<id>";

function createNotesDoc() {
  var presentation = SlidesApp.openById(INPUT_PRESO_ID)
  var out = DocumentApp.openById(OUTPUT_DOC_ID)

  //out.getBody().clear();
  var body = out.getBody();
  var imgs = body.getImages();

  var slides = presentation.getSlides();



  if (imgs.length == 0) {
    Logger.log("Got no images, exiting");
    return;
  } else {
    Logger.log("Got " + imgs.length + " images and " + slides.length + " slides");
    if (imgs.length != slides.length) {
      Logger.log("I got a different number of images and slides, exiting.");
      return;
    }
  }

  
  body.appendParagraph(presentation.getName());

  out.setName(presentation.getName() + " - notes");
  table = body.appendTable();
  for (var i = 0; i < slides.length; i++) {
    Logger.log("Exporting page " + (i + 1) + "/" + slides.length);
    var slide = slides[i];
    var text = slide.getNotesPage().getSpeakerNotesShape().getText();
    var row = table.appendTableRow();

    var imageCell = row.appendTableCell();
    imageCell.removeChild(imageCell.getChild(0)); // remove empty first paragraph

    if (imgs[i] != null) {
      var thumbnail = imageCell.appendImage(imgs[i].copy());
      thumbnail.setWidth(160).setHeight(90);
      imgs[i].removeFromParent();
    } else {
      Logger.log("Linked slide " + i + " was null");
    }
    
    var noteCell = row.appendTableCell();
    noteCell.removeChild(noteCell.getChild(0)); // remove empty first paragraph
    table.setColumnWidth(0, 140);
    var noteParagraph = noteCell.appendParagraph("");
  
    noteParagraph.appendText(text.asRenderedString());

  }
}

Upvotes: 0

Cooper
Cooper

Reputation: 64110

Appending Google Slides to Document

function slides2Document() {
  var pres = SlidesApp.openById('slides id');
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var slides = pres.getSlides();
  for(var i = 0; i < slides.length; i++) {
    var slide = slides[i];
    var images = slide.getImages();
    for(var j = 0; j < images.length; j++) {
      var img = images[j];
      var imgblob = img.getBlob().getAs('image/gif');
      body.appendImage(imgblob);
    }
  }
}

Upvotes: 1

Related Questions