Image from Google Drive to Google Slides via DriveApp.getFileById

2k views Asked by At

I want to import an image to Google Slides over Google Apps Script after the instruction Google Developers - How to...

function createImage(presentationId) {
// Add a new image to the presentation page. The image is assumed to exist in
// the user's Drive, and have 'imageFileId' as its file ID.
var requests = [];
var pageId = 'g1fe0c....';
var imageId = 'MyImage_01';
var imageUrl = DriveApp.getFileById('0B6cQESmLJfX...').getUrl();

var emu4M = {
  magnitude: 4000000,
  unit: 'EMU'
};
requests.push({
  createImage: {
    objectId: imageId,
    url: imageUrl,
    elementProperties: {
      pageObjectId: pageId,
      size: {
        height: emu4M,
        width: emu4M
      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: 100000,
        translateY: 100000,
        unit: 'EMU'
      }
    }
  }
});

// Execute the request.
var response = Slides.Presentations.batchUpdate({
  requests: requests
}, presentationId); 

}

But for every image-format I tried, there is an error message in Google Apps Script:

Invalid requests[0].createImage: The provided image is in an unsupported format.

Drive- and Slides API is activated in Google Advanced Services. The folder and the file has a public share.

Does anyone use the command DriveApp.getFileById() with subsequent export to Google Slides successfully?

1

There are 1 answers

0
Tanaike On BEST ANSWER

How about a following modification?

From :

var imageUrl = DriveApp.getFileById('0B6cQESmLJfX...').getUrl();

To :

var imageUrl = DriveApp.getFileById('0B6cQESmLJfX...').getDownloadUrl() + "&access_token=" + ScriptApp.getOAuthToken();

From this document, it seemed that the access token is required. https://developers.google.com/slides/how-tos/add-image

Updated: February 7, 2020

From January, 2020, the access token cannot be used with the query parameter like access_token=###. Ref So please use the access token to the request header instead of the query parameter.

In OP's case, I think that this answer can be used.