hun*_*188 3 google-apps-script
我使用下面的这个脚本将图像从 URL 插入到 Google 表格中的指定单元格中,但是,它不适用于 Google 云端硬盘中包含的图像 URL。
//If any graphic is a URL, insert that URL into the corresponding cell in the Briefing tab.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName("main_gen").getRange("ImageRange").getValues();
var dstSheet = ss.getSheetByName("Briefing");
for (var num = 0; num < source.length; num ++) {
if (/https?:\/\//.test(source[num][0])) { //Check to ensure a URL has been entered.
var graphicformula = '=IMAGE("' + source[num][0] + '",1)';
dstSheet.getRange(graphics_placements[num]).setFormula(graphicformula);
}
}
Run Code Online (Sandbox Code Playgroud)
用户在一个选项卡上的预定义数量的单元格中输入图像的 URL。根据他们选择的顺序(可以有 1 到 5 个图像),图像将插入到另一个选项卡上的指定单元格中。我如何重写上面的代码来解释这样的URL(https://google.com/image.png)和这样的URL(https://drive.google.com/open?id=12Au6IQE9l9X_hzM5n87Fs9gajJ)?
谢谢!
IMAGE()
。
https://google.com/image.png
和https://drive.google.com/open?id=###
。https://google.com/image.png
是外部链接。https://drive.google.com/open?id=###
是您的 Google 云端硬盘中的链接。如果我的理解是正确的,这个答案怎么样?请将此视为几个可能答案之一。
不幸的是,Google Drive 中的图像文件无法直接使用IMAGE()
. 在这种情况下,图像文件需要公开共享。因此,在这个答案中,文件被公开共享并放入电子表格中。
当您的脚本修改时,请修改如下。
从:for (var num = 0; num < source.length; num ++) {
if (/https?:\/\//.test(source[num][0])) { //Check to ensure a URL has been entered.
var graphicformula = '=IMAGE("' + source[num][0] + '",1)';
dstSheet.getRange(graphics_placements[num]).setFormula(graphicformula);
}
}
Run Code Online (Sandbox Code Playgroud)
到:
for (var num = 0; num < source.length; num ++) {
if (/https?:\/\//.test(source[num][0])) { //Check to ensure a URL has been entered.
var res = source[num][0].match(/drive\.google\.com\/open\?id=(\w.+)|drive\.google\.com\/file\/d\/(\w.+)\//);
if (res && res.length > 0) {
var id = res[1] || res[2];
var file = DriveApp.getFileById(id);
if (file.getOwner().getEmail() != Session.getActiveUser().getEmail()) {
file = file.makeCopy(DriveApp.getRootFolder());
}
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
source[num][0] = "https://drive.google.com/uc?export=download&id=" + id;
}
var graphicformula = '=IMAGE("' + source[num][0] + '",1)';
dstSheet.getRange(dstSheet.getLastRow() + 1, 1).setFormula(graphicformula);
}
}
Run Code Online (Sandbox Code Playgroud)
DriveApp.getRootFolder()
.https://drive.google.com/open?id=###
,请显示示例 URL。请按如下所示修改脚本中的 for 循环。
var scale = 0.5; // In this case, the imported image is reduces with 50 % from the original size.
var n = 0;
var cellWidth = 0;
for (var num = 0; num < source.length; num ++) {
if (/https?:\/\//.test(source[num][0])) { //Check to ensure a URL has been entered.
var res = source[num][0].match(/drive\.google\.com\/open\?id=(\w.+)|drive\.google\.com\/file\/d\/(\w.+)\//);
var blob = res && res.length > 0 ? DriveApp.getFileById(res[1] || res[2]).getBlob() : UrlFetchApp.fetch(source[num][0]).getBlob();
var lastRow = dstSheet.getLastRow() + 1 + n++;
var image = dstSheet.insertImage(blob, 1, lastRow);
var imageWidth = image.getWidth() * scale;
var imageHeight = image.getHeight() * scale;
cellWidth = cellWidth > imageWidth ? cellWidth : imageWidth;
image.setWidth(imageWidth);
image.setHeight(imageHeight);
dstSheet.setRowHeight(lastRow, imageHeight);
}
}
dstSheet.setColumnWidth(1, cellWidth);
Run Code Online (Sandbox Code Playgroud)
dstSheet.setRowHeight(lastRow, imageHeight)
更改dstSheet.setColumnWidth(1, cellWidth)
单元格大小。 归档时间: |
|
查看次数: |
10757 次 |
最近记录: |