Home > Back-end >  With Google script, Insert 4:3 image into Google Slide of 16:9 ratio, then export it as PNG file
With Google script, Insert 4:3 image into Google Slide of 16:9 ratio, then export it as PNG file

Time:02-03

With Google Apps script I'm trying to automate the process of inserting 4:3 image file (in Google Drive) into an empty slide of 16:9 ratio, aligning the image to the centre, then export the slide as PNG file.

I encountered the issue that the exported PNG file is still an empty slide and missing the inserted image.

Is there something wrong with my code?

Slides API enabled.

Please see below the code I have written so far:

const file = DriveApp.openById(#####);
const presentation1 = SlidesApp.openById(######);

var slide = presentation1.getSlides()[0];
var image = slide.insertImage(file);
image.alignOnPage(SlidesApp.AlignmentPosition.HORIZONTAL_CENTER);
        

var url = Slides.Presentations.Pages.getThumbnail(pptImageConverterId, slide.getObjectId(), {
          "thumbnailProperties.mimeType": "PNG"
        }).contentUrl;
var blob = UrlFetchApp.fetch(url).getAs('image/png');
folder.createFile(blob.setName("Slide1.png"));

CodePudding user response:

I believe your goal is as follows.

  • You want to remove your issue of I encountered the issue that the exported PNG file is still an empty slide and missing the inserted image.. This is your question.

Modification points:

  • In your script, pptImageConverterId and folder are not declared. pptImageConverterId is the same with ###### of SlidesApp.openById(######);?
  • DriveApp.openById(#####); is not correct. Is that DriveApp.getFileById("###");?
  • About aligning the image to the centre, in this case, I thought that you might have wanted SlidesApp.AlignmentPosition.CENTER instead of SlidesApp.AlignmentPosition.HORIZONTAL_CENTER.

But, you say I encountered the issue that the exported PNG file is still an empty slide and missing the inserted image.. So, I'm worried that you might have miscopied your script.

When I saw your script, if the above points are modified, I thought that the reason for your issue is due to that saveAndClose() is not used. When these are reflected in your script, it becomes as follows.

Modified script:

Please set pptImageConverterId, fileId, and folderId.

function myFunction() {
  const pptImageConverterId = "###"; // Please set your Google Slides ID.
  const fileId = "###"; // Please set the file ID of the image you want to use.
  const folderId = "###"; // Please set the folder ID you want to use.
  
  const file = DriveApp.getFileById(fileId);
  const presentation1 = SlidesApp.openById(pptImageConverterId);
  var slide = presentation1.getSlides()[0];
  var image = slide.insertImage(file);
  image.alignOnPage(SlidesApp.AlignmentPosition.CENTER);
  presentation1.saveAndClose();
  var url = Slides.Presentations.Pages.getThumbnail(pptImageConverterId, slide.getObjectId(), {
    "thumbnailProperties.mimeType": "PNG"
  }).contentUrl;
  var blob = UrlFetchApp.fetch(url).getBlob();
  DriveApp.getFolderById(folderId).createFile(blob.setName("Slide1.png"));
}
  • When this script is run, an image is put to the 1st slide of the Google Slides, and the slide is exported as a PNG file.

References:

  • Related