Google apps script 使用谷歌应用程序脚本从谷歌硬盘或URL将图像插入谷歌工作表

Google apps script 使用谷歌应用程序脚本从谷歌硬盘或URL将图像插入谷歌工作表,google-apps-script,Google Apps Script,我在下面使用这个脚本将URL中的图像插入到Google工作表中的指定单元格中,但是,它不适用于Google Drive中包含的图像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_

我在下面使用这个脚本将URL中的图像插入到Google工作表中的指定单元格中,但是,它不适用于Google Drive中包含的图像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);
    }
  }
//如果任何图形是URL,请将该URL插入简报选项卡中相应的单元格中。
var ss=SpreadsheetApp.getActiveSpreadsheet();
var source=ss.getSheetByName(“main_gen”).getRange(“ImageRange”).getValues();
var dstSheet=ss.getSheetByName(“简报”);
for(var num=0;num
用户在一个选项卡上按预定义的单元格数输入图像的URL。根据它们选择的顺序(可以有1到5个图像),图像将插入到另一个选项卡上的指定单元格中。我如何重写上面的代码来解释像这样()和这样()的URL

谢谢

  • 您希望使用
    image()
    的公式将谷歌硬盘中的图像文件放入电子表格。
    • 在您的例子中,URL类似于
      https://google.com/image.png
      https://drive.google.com/open?id=###
    • https://google.com/image.png
      是外部链接
    • https://drive.google.com/open?id=###
      是Google Drive中的链接
  • 您希望使用谷歌应用程序脚本实现这一点
如果我的理解是正确的,那么这个答案呢?请把这看作是几个可能的答案之一

问题和解决方法: 不幸的是,Google Drive中的图像文件无法使用
image()
直接放入电子表格。在这种情况下,图像文件需要公开共享。因此,在这个答案中,这些文件是公开共享的,并放在电子表格中

修改脚本: 修改脚本时,请按以下方式修改

发件人:
  • 在这种情况下,图像被放在单元格上。这不是放在牢房里的。因此,当您希望将图像大小与单元格大小匹配时,需要更改单元格大小。在上面的脚本中,
    dstSheet.setRowHeight(lastRow,imageHeight)
    dstSheet.setColumnWidth(1,cellWidth)
    更改单元格大小

我能理解
https://drive.google.com/open?id=###
。但是我无法理解
https://google.com/image.png
。我能问一下吗?例如,您能否提供
https://google.com/image.png
?@Tanaike它可以是在线的任何图像,例如:。感谢您的回复。在这种情况下,URL可用于
=IMAGE()
。在你的问题中,你想使用这样的URL和
https://drive.google.com/open?id=###
。我的理解正确吗?正确。我需要一个脚本,将为这些网址之一帐户。谢谢你的答复。我想我能正确理解你的问题。所以我提出了一个修改后的脚本作为答案。你能确认一下吗?如果这不是你想要的结果,我道歉。好的,我知道你在走什么路。当我运行此命令时,访问被拒绝。我正在访问的Google Drive文件夹中的图像不是我自己的,但我有查看权限。我假设我收到此错误是因为我不拥有该文件夹,因此无法设置图像的权限?@hunter21188谢谢您的回复。给您带来不便,我深表歉意。在这种情况下,把它复制到你的谷歌硬盘并公开分享怎么样?为此,我修改了示例脚本。你能确认一下吗?如果这不是你想要的方向,我再次道歉。感谢所有的帮助!不幸的是,这仍然给了我同样的错误。这是其中一个链接()的示例。我不确定这是否对您有帮助,或者我不明白为什么我可能仍然会遇到这些权限问题。我可以用我的谷歌帐号查看图片。@hunter21188谢谢你的回复。给您带来的不便,我深表歉意。关于
https://drive.google.com/file/d/12Au6IQE9l9X_hzM5n87Fs9gajJ_9VjWi/view
,这是与您的问题不同的模式。我没有注意到存在这样的其他模式。这是因为我的技术差。我为此道歉。我更新了我的答案。你能确认一下吗?如果这不能解决您的问题,您能否提供示例电子表格?通过这一点,我想确认一下。如果你能合作解决你的问题,我很高兴。找到了这个问题。我不得不用链接将其更改为
DOMAIN\u,因为这就是它的限制方式。用代码(例如)
=IMAGE(“https://drive.google.com/uc?export=download&id=12Au6IQE9l9X_hzM5n87Fs9gajJ_9VjWi“,1)
。但是,这些单元格是空白的。当我试图直接访问那个链接时,它说我没有权限。不幸的是,共享一个示例电子表格并没有什么帮助,因为我创建了一个,而且效果很好(尽管仍然没有在正确的单元格中插入图像)。这与域问题有关:/
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);
  }
}
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);
  }
}
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);