1

I'm new to google App Script. I made this script for copying a template slide show and after the copy is made i fill it with tekst of a google sheet.

When it's ready I want to make screenshots of the pages of the slides. I found this code on the internet that works fine when i use slideId that already exists. when I use it in my code when the copy is made i get this error

Exception: Attribute provided with no value: url

when I Log the response variable I get this error message Informatie { error: { code: 400, message: 'The specified object with pageObjectId SLIDES_API28088814_0 is not a page', status: 'INVALID_ARGUMENT' } } later when I use the slideId of the copy instead of the masterDeckID in generateScreenshots(masterDeckID); it works fine.

here is my code:

function createSlides() {
  var weekDays = ["Zondag","Maandag","Dinsdag","Woensdag","Donderdag","Vrijdag","Zaterdag"]
  var months = ["Januari","Februari","Maart","April","Mei","Juni","Jullie","Augustus","September","Oktober","November","December"]
  var date = new Date();
  let day = date.getDay();
  let dayNumber = date.getDate();
  let month = date.getMonth();
  let year = date.getFullYear();

  let templateId = "myslideID";
  var template = DriveApp.getFileById(templateId);
  var fileName = template.getName();
  var copy = template.makeCopy();
  copy.setName(dayNumber + months[month] + fileName);
  let copyId = copy.getId();

//open de copy from template point out templateslides 
  let masterDeckID = copyId;
  let deck = SlidesApp.openById(masterDeckID);
  let slides = deck.getSlides();
  let masterSlide = slides[0];
  let masterSlide2 = slides[1];
//open de content from sheet
  let dataRange = SpreadsheetApp.getActive().getDataRange();
  let sheetContents = dataRange.getValues();
  let header = sheetContents.shift();
  let updatedContents = [];

// inport google sheet text in  copy of template
  let slide1 = masterSlide.duplicate();
    slide1.replaceAllText("{{dayName}}", weekDays[day]);
    slide1.replaceAllText("{{dayNumber}}", dayNumber);
    slide1.replaceAllText("{{month}}",months[month] );
    slide1.replaceAllText("{{year}}", year);
    slide1.replaceAllText("{{website}}",sheetContents[0][3])
  let slide2 = masterSlide2.duplicate();
    slide2.replaceAllText("{{quote}}", sheetContents[0][4]);
//remove template slides
masterSlide.remove();
masterSlide2.remove();
// function for creating screenshots
generateScreenshots(masterDeckID);


}


function generateScreenshots(presentationId) {
  
  var presentation = SlidesApp.openById(presentationId);
  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,
  };

  // Log URL of the main thumbnail of the deck
  //Logger.log(Drive.Files.get(presentationId).thumbnailLink);

  // For storing the screenshot image URLs
  var screenshots = [];

  var slides = presentation.getSlides().forEach(function (slide, index) {
        var url = baseUrl.replace('{presentationId}', presentationId).replace('{pageObjectId}', slide.getObjectId());
        var response = JSON.parse(UrlFetchApp.fetch(url, parameters));
    

    // // Upload Googel Slide image to Google Drive
    var blob = UrlFetchApp.fetch(response.contentUrl).getBlob();
    DriveApp.createFile(blob).setName('Image ' + (index + 1) + '.png');

    screenshots.push(response.contentUrl);
  });

  return screenshots;
}

what is going wrong, what could be a solution? Any help would be appreciated.

Thanks,

Aaron

aaron
  • 13
  • 2

1 Answers1

0

In your showing script, how about the following modification? In this case, createSlides() is modified. I guessed that the reason for your current issue is due to that the updated Google Slides might not be reflected.

From:

masterSlide.remove();
masterSlide2.remove();
// function for creating screenshots
generateScreenshots(masterDeckID);

To:

masterSlide.remove();
masterSlide2.remove();

deck.saveAndClose(); // Added

// function for creating screenshots
generateScreenshots(masterDeckID);

Note:

The modified whole script of createSlides() is as follows.

function createSlides() {
  var weekDays = ["Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag"]
  var months = ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Jullie", "Augustus", "September", "Oktober", "November", "December"]
  var date = new Date();
  let day = date.getDay();
  let dayNumber = date.getDate();
  let month = date.getMonth();
  let year = date.getFullYear();

  let templateId = "myslideID";
  var template = DriveApp.getFileById(templateId);
  var fileName = template.getName();
  var copy = template.makeCopy();
  copy.setName(dayNumber + months[month] + fileName);
  let copyId = copy.getId();

  //open de copy from template point out templateslides 
  let masterDeckID = copyId;
  let deck = SlidesApp.openById(masterDeckID);
  let slides = deck.getSlides();
  let masterSlide = slides[0];
  let masterSlide2 = slides[1];
  //open de content from sheet
  let dataRange = SpreadsheetApp.getActive().getDataRange();
  let sheetContents = dataRange.getValues();
  let header = sheetContents.shift();
  let updatedContents = [];

  // inport google sheet text in  copy of template
  let slide1 = masterSlide.duplicate();
  slide1.replaceAllText("{{dayName}}", weekDays[day]);
  slide1.replaceAllText("{{dayNumber}}", dayNumber);
  slide1.replaceAllText("{{month}}", months[month]);
  slide1.replaceAllText("{{year}}", year);
  slide1.replaceAllText("{{website}}", sheetContents[0][3])
  let slide2 = masterSlide2.duplicate();
  slide2.replaceAllText("{{quote}}", sheetContents[0][4]);
  //remove template slides
  masterSlide.remove();
  masterSlide2.remove();

  deck.saveAndClose(); // Added

  // function for creating screenshots
  generateScreenshots(masterDeckID);
}

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • That does the trick. works like a charme. Didn't know de deck was still open. When I look at it now it seems logical. I opened it earlier so I have to close it to. – aaron Aug 13 '23 at 12:24
  • @aaron Thank you for your response. I'm glad your issue was resolved. Thank you, too. – Tanaike Aug 13 '23 at 12:33