- 您想将 Google 云端硬盘中的图片放到您的 Google 电子表格中。
- 您希望使用 Google Apps 脚本实现此目的。
如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。
模式一:
在此模式中,图像使用=IMAGE("URL") 放置。
使用=IMAGE("URL") 时,需要公开分享图片。所以请以On - Anyone with the link公开分享图片。
另外,请将端点修改如下。
发件人:
https://drive.google.com/open?id=###
到:
https://drive.google.com/uc?export=download&id=###
- 在这种情况下,您可以在图片公开分享后将图片添加到
=IMAGE("https://drive.google.com/uc?export=download&id=###")。
模式 2:
如果您不想公开分享图片,这种模式怎么样?在此模式中,图像以 blob 形式放置,不公开共享。
这里,请检查以下示例脚本。
示例脚本:
var fileId = "###"; // Please set the file ID of the image.
var sheet = SpreadsheetApp.getActiveSheet();
var blobSource = DriveApp.getFileById(fileId).getBlob();
var image = sheet.insertImage(blobSource, 1, 1);
image.setWidth(100).setHeight(100);
sheet.setColumnWidth(1, 100).setRowHeight(1, 100);
- 当您运行脚本时,图像会被放到单元格“A1”中。并且图像大小被调整为 100 x 100 像素。然后,根据图像大小更改行和列大小。
- 这是一个简单的示例脚本。所以请根据您的实际情况进行修改。
参考资料:
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
补充:
从你的回复中发现图片尺寸超过限制尺寸(1,048,576 pixels^2)Ref你现在的原因是这个。
在这种情况下,为了放置图像,需要调整图像大小。以下示例脚本通过调整图像大小来放置图像。为此,我使用了 Google Apps 脚本库。所以please install it to the script editor。
示例脚本:
var fileId = "###"; // Please set the file ID of the image.
var sheet = SpreadsheetApp.getActiveSheet();
var blobSource = DriveApp.getFileById(fileId).getBlob();
var obj = ImgApp.getSize(blobSource);
var height = obj.height;
var width = obj.width;
if (height * width > 1048576) {
var r = ImgApp.doResize(fileId, 512);
blobSource = r.blob;
}
var image = sheet.insertImage(blobSource, 1, 1);
image.setWidth(100).setHeight(100);
sheet.setColumnWidth(1, 100).setRowHeight(1, 100);
- 在此示例脚本中,当图像大小超过
1,048,576 pixels^2 时,图像会调整大小并放入电子表格中。
- 这是一个简单的示例脚本。所以请根据您的实际情况进行修改。
参考资料: