Inserting image into Google Doc with apps script says invalid image data

旧时模样 提交于 2020-07-22 18:26:15

问题


I am trying to insert all images from a Google Drive folder into a Google document. But on insertImage I always get an "invalid image data" error.

function myFunction() {
  var folder = DriveApp.getFolderById(id);
  var files = folder.getFiles();
  while (files.hasNext()) {
     var file = files.next();
     var img = file.getBlob();
     var imgDoc = DocumentApp.getActiveDocument().getBody().insertImage(0, img);  
   }
}

There are numerous examples that explain how to do this, and all say to get the image blob and use that to insert. Is there something about this code that gives an incorrect image blob?


回答1:


How about this answer?

Hypothesis and Experiment:

From my experiment, in the Spreadsheet, when an image is inserted by insertImage() in Class Sheet, the limitation is due to the image area (pixels^2) rather than the file size of it. The maximum area of image which can be inserted is 1,048,576 pixels^2. Ref

From this situation, I thought that your issue is also related to the case of Spreadsheet. The limitation of insertImage() in Class Body might be due to the area of image. So I experimented as follows.

  • Image with the following sizes can be inserted.

    • 5000 pixels x 5000 pixels
    • 25000 pixels x 1000 pixels
    • 1000 pixels x 25000 pixels
  • Image with the following sizes can NOT be inserted.

    • 5001 pixels x 5001 pixels
    • 5000 pixels x 5001 pixels
    • 5001 pixels x 5000 pixels

As the result, it was found that the limitation of area is 25,000,000 pixels^2.

About your issue:

In your case, the image size of the shared image is 6240 pixels x 4160 pixels. This area is 25,958,400 pixels^2. This area is larger than the value (25,000,000 pixels^2) retrieved above experiment. By this, it is considered that the error occurs.

Workaround:

In order to avoid this limitation, how about this workaround? In this workaround, when the image size is more than 25,000,000 pixels^2, the image size is decreased using Drive API. The modified script is as follows.

Modified script:

function myFunction() {
  var folder = DriveApp.getFolderById(id);
  var files = folder.getFiles();
  while (files.hasNext()) {
     var file = files.next();
     var img = file.getBlob();

     // --- Following script was added.
     var url = "https://www.googleapis.com/drive/v3/files/" + file.getId() + "?fields=imageMediaMetadata(height%2Cwidth)%2CthumbnailLink&access_token=" + ScriptApp.getOAuthToken();
     var res = UrlFetchApp.fetch(url).getContentText();
     var obj = JSON.parse(res);
     if (obj.imageMediaMetadata && (obj.imageMediaMetadata.width * obj.imageMediaMetadata.height) > 25000000) {
       var width = 1000;
       var tlink = obj.thumbnailLink.replace(/=s\d+/, "=s" + width);
       img = UrlFetchApp.fetch(tlink).getBlob();
     }
     // ---

     var imgDoc = DocumentApp.getActiveDocument().getBody().insertImage(0, img);
   }
}
  • In this modification, as a sample, the image size is reduced with the width of 1000 pixels. If you want to modify this, please modify var width = 1000.
  • I think that in your script, Drive API has already been automatically enabled because the class DriveApp is used. But if the error related to Drive APi occurs, please check whether Drive API is enabled at API console.

References:

  • SpreadsheetApp.insertImage server error
  • insertImage()
  • Files: list of Drive API


来源:https://stackoverflow.com/questions/55466100/inserting-image-into-google-doc-with-apps-script-says-invalid-image-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!