使用 Google Apps 脚本将图像作为 BlobSource 插入 Sheet

Inserting images into a Sheet as BlobSource using Google Apps Script

我在使用 google 脚本将图像从 google 驱动器插入 google sheet 时遇到了问题。当从 url 加载时,我可以让它完美地工作,但不能从 google 驱动器文件作为 blob 加载。该脚本验证大小为 88KB,但随后在插入函数为 运行.

时抛出一个错误,指出文件 >2MB

不过,88KB 的文件可以从 URL 加载。

Blob 会不会在一个 88KB 的文件周围添加了超过 2MB 的数据?可能我不太了解如何使用 blob?

function InsertImageTest() {

  // this is a file with a single sheet with a png image over the grid, starting at cell A1
  var ReportSpeadsheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/the-file-id/edit#gid=0")

  var ReportSheet = ReportSpeadsheet.getSheetByName("Sheet1");

  var File1 = DriveApp.getFileById("fileId1"); // this is a 763byte png image
  var File2 = DriveApp.getFileById("fileId2"); // this is a 88KB png image


  Logger.log("File1=" + File1.getName()); // returns correct name for file
  Logger.log("File2" + File2.getName()); // returns correct name for file
  Logger.log("File1=" + File1.getSize()); //File1=763
  Logger.log("File2=" + File2.getSize()); //File2=89678

  try { ReportSheet.insertImage(File1, 1, 1); } catch (err) {
    Logger.log("File1 failed  " + err); //Works
  } 

  try { ReportSheet.insertImage(File2, 1, 2); } catch (err) {
    Logger.log("File2 failed  " + err); //Exception: The blob was too large. The maximum blob size is 2MB. The maximum number of pixels is 1 million.
  } 
// Also tried File2.getAs('image/png') and File2.getBlob() as the blob argument, same error message.

// testing the 88KB file uploaded to a URL on my website.
  ReportSheet.insertImage("www.theURL.com/the88KBfile.png", 1, 3); // works fine


}

修改点:

  • 我认为在您的情况下,图像大小可能是您问题的原因,而不是文件大小。 insertImage()的图片尺寸限制可以在看到 目前阶段,insertImage()的最大图片尺寸似乎是1,048,576像素^2。
  • 为了避免你的问题,我想建议调整图片大小。

当你的脚本修改后,变成如下。

修改后的脚本:

此示例脚本使用驱动器 API 来检索图像大小。所以在你使用这个脚本之前,please enable Drive API at Advanced Google services。并且,请设置您的图像文件的文件ID。

function InsertImageTest() {
  var ReportSpeadsheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/the-file-id/edit#gid=0");
  var ReportSheet = ReportSpeadsheet.getSheetByName("Sheet1");

  // --- I modified below script.
  var fileIds = ["fileId1", "fileId2"];  // <--- Please set the file IDs of the image files.
  fileIds.forEach((id, i) => {
    var obj = Drive.Files.get(id);
    var image = obj.imageMediaMetadata && (obj.imageMediaMetadata.width * obj.imageMediaMetadata.height) > 1048576 ? obj.thumbnailLink.replace(/=s\d+/, "=s500") : DriveApp.getFileById(id).getBlob();
    ReportSheet.insertImage(image, 1, i + 1);
  });
}
  • 在此示例脚本中,当图像大小超过 1,048,576 像素^2 时,缩小图像并返回缩小后的图像 URL。当图片尺寸小于1,048,576像素^2时,返回图片的blob。
  • 调整后的图片宽度为 500 像素。当你想改变这个时,请修改=s500。但是,当图像尺寸超过 1,048,576 像素^2 时,就会出现错误。请注意这一点。

参考文献:

确定 url 方法有效后,我想出了这个解决方法来解决我自己的问题,但它不如 Tanaike 的解决方案那么简洁。

您可以通过 url 使用“https://docs.google.com/uc?id=" + fileId2

访问该文件

但是文件必须 'anyone with link' 可见,url 方法才能处理 Google Drive 文件,所以我临时更改了文件权限:

File.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
ReportSheet.insertImage("https://docs.google.com/uc?id=" + fileId2, 1, 4);
File.setSharing(DriveApp.Access.DOMAIN_WITH_LINK, DriveApp.Permission.VIEW);

我将采用 Tanaike 的解决方案。