使用应用脚本将图像插入Google文档时显示无效的图像数据 [英] Inserting image into Google Doc with apps script says invalid image data

查看:65
本文介绍了使用应用脚本将图像插入Google文档时显示无效的图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Google云端硬盘文件夹中的所有图像插入Google文档.但是在insertImage上,我总是收到无效的图像数据"错误.

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?

推荐答案

此答案如何?

根据我的实验,在电子表格中,当参考

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

从这种情况出发,我认为您的问题也与电子表格的情况有关.类主体中insertImage()的限制可能是由于图像的区域.所以我做了如下实验.

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像素x 5000像素
  • 25000像素x 1000像素
  • 1000像素x 25000像素

以下尺寸的图像无法插入.

Image with the following sizes can NOT be inserted.

  • 5001像素x 5001像素
  • 5000像素x 5001像素
  • 5001像素x 5000像素

结果,发现区域限制为25,000,000像素^ 2.

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

在您的情况下,共享图像的图像尺寸为6240像素x 4160像素.该区域是25,958,400像素^ 2.该区域大于以上实验中获得的值(25,000,000像素^ 2).这样,就可以认为是发生了错误.

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.

为了避免此限制,该解决方法如何?在这种解决方法中,当图像大小大于25,000,000像素^ 2时,将使用Drive API减小图像大小.修改后的脚本如下.

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.

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);
   }
}

  • 在本变型中,作为示例,图像尺寸随着1000像素的宽度而减小.如果要修改此内容,请修改var width = 1000.
  • 我认为在您的脚本中,由于使用了DriveApp类,因此已经自动启用了Drive API.但是,如果发生与Drive APi相关的错误,请检查是否在API控制台上启用了Drive API.
    • 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.
      • SpreadsheetApp.insertImage server error
      • insertImage()
      • Files: list of Drive API

      这篇关于使用应用脚本将图像插入Google文档时显示无效的图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆