Skip to content

Instantly share code, notes, and snippets.

@msbarry
Last active February 7, 2024 11:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msbarry/9862e10cf4113d8e325ee14ccd8f62c6 to your computer and use it in GitHub Desktop.
Save msbarry/9862e10cf4113d8e325ee14ccd8f62c6 to your computer and use it in GitHub Desktop.
Google App Script to create a doc with speaker notes from slides
var INPUT_PRESO_ID = "<id>";
var OUTPUT_DOC_ID = "<id>";
function createNotesDoc() {
var presentation = Slides.Presentations.get(INPUT_PRESO_ID)
var out = DocumentApp.openById(OUTPUT_DOC_ID)
out.getBody().clear();
table = out.getBody().appendTable();
var links = [];
for (var i = 0; i < presentation.slides.length; i++) {
Logger.log("Exporting page " + (i + 1) + "/" + presentation.slides.length);
var slide = presentation.slides[i];
var text = slide.slideProperties.notesPage.pageElements[1].shape.text;
var row = table.appendTableRow();
var url = "https://docs.google.com/presentation/d/" + INPUT_PRESO_ID + "/export/png?id=" + INPUT_PRESO_ID + "&pageid=" + slide.objectId;
var imageCell = row.appendTableCell();
imageCell.removeChild(imageCell.getChild(0)); // remove empty first paragraph
imageCell.appendImage(UrlFetchApp.fetch(url).getBlob()).setWidth(160).setHeight(90);
var noteCell = row.appendTableCell();
noteCell.removeChild(noteCell.getChild(0)); // remove empty first paragraph
table.setColumnWidth(0, 140);
var noteParagraph = noteCell.appendParagraph("");
if (text) {
var textElements = text.textElements;
var fullContent = "";
for (var j = 0; j < textElements.length; j++) {
if (textElements[j].textRun) {
fullContent += textElements[j].textRun.content;
}
}
if (fullContent.trim()) {
noteParagraph.appendText(fullContent.trim());
}
}
}
}
@jenarehorka
Copy link

Hello,
thank you for sharing your code. I'm trying to use your script but getting following error message.

Exception: Invalid image data.

If I comment out the section about image all works fine. So maybe there is any new update in app script reg images? Or should I include any other service in appscript?

Thank for your help

Jan

@13stan13
Copy link

13stan13 commented Jan 1, 2024

I tried this but got an error Slides is not defined. How do I fix that?

@emisca
Copy link

emisca commented Jan 29, 2024

I fixed this code in a rough way till it works....

BUT first you have to enable API as following

  • open script editor
  • click on resources-> Advanced Google Services
  • Enable Google slide API and Drive API .
  • click on ok

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

var baseUrl =
    "https://slides.googleapis.com/v1/presentations/{presentationId}/pages/{pageObjectId}/thumbnail";
var parameters = {
    method: "GET",
    headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() },
    contentType: "application/json",
    muteHttpExceptions: true
  };

function createNotesDoc() {
  var presentation = SlidesApp.openById(INPUT_PRESO_ID)
  var out = DocumentApp.openById(OUTPUT_DOC_ID)
  out.getBody().clear();
  table = out.getBody().appendTable();
  var links = [];
  var slides = presentation.getSlides();
  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 url = baseUrl
      .replace("{presentationId}", INPUT_PRESO_ID)
      .replace("{pageObjectId}", slide.getObjectId());
    var imageCell = row.appendTableCell();
    imageCell.removeChild(imageCell.getChild(0)); // remove empty first paragraph

    var response = JSON.parse(UrlFetchApp.fetch(url, parameters));
    var blob = UrlFetchApp.fetch(response.contentUrl).getBlob();

    imageCell.appendImage(blob).setWidth(160).setHeight(90);
    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());

  }
}

@jenarehorka
Copy link

Thank you emisca this work very well!

@olivier-menicot
Copy link

Hi, I'm a newbie to App Script. How do I run this script ? I have opened App Script from Google Slide, then copy/paste the script and added the Services Drive and Slides as recommended above. However I'm not sure if I have to modify the script to point to my Google Slide presentation. Thanks.

@emisca
Copy link

emisca commented Jan 31, 2024

you need to provide the slides id and the document id in the 2 variables in the beginning:

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

This will select the presentation (slides) and the destination document where the content will be put. The doc has to be created before you run the script.

You can amend the script to have it create the output doc if you wish, but it will make the code more complex to handle permissions and where to put the doc.

@olivier-menicot
Copy link

olivier-menicot commented Jan 31, 2024

Thank you, it works great exactly as I expected !!

@emisca
Copy link

emisca commented Feb 7, 2024

see also https://stackoverflow.com/questions/59958862/how-to-embed-a-slide-in-a-google-doc-using-app-script/77949512#77949512 for a trick to put linked slide thumbnails instead of static images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment