4

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?

Roberto B
  • 111
  • 2
  • 10
  • About ``The provided image is in an unsupported format.`` of your error message, can we ask you about what the format of image you want to use is? – Tanaike Sep 04 '17 at 01:30
  • I tried the ID's from GIF-, JPG-, PNG- and Google Drawing Files. – Roberto B Sep 04 '17 at 02:35
  • Don't worry. Those formats can be used at Google. – Tanaike Sep 04 '17 at 02:39
  • Same error message, when I used direct links like "var imageUrl = 'https://drive.google.com/uc?id=0B6cQGlkNU4tMU.....';" Only this imageUrl works: "https://lh6.googleusercontent.com/vuINetSxcBd8InRV.....=w1919-h986". But I want use DriveApp.getFileById like in the instruction. – Roberto B Sep 04 '17 at 02:39
  • Can I ask you about your current script? If you can do, please add it on your question. – Tanaike Sep 04 '17 at 02:46
  • Same code as in the example from Google Developer Site. I added it to my question. – Roberto B Sep 04 '17 at 02:54

1 Answers1

3

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.

Tanaike
  • 181,128
  • 11
  • 97
  • 165