首页 > 解决方案 > 通过 URL 插入图像在 Google Apps 脚本中不起作用

问题描述

我创建的 Google 表单中的一个响应是上传一张图片,该图片与链接电子表格中的 URL 一起保存在我的 Google Drive 中。在 Google 幻灯片中,我创建了一个 Apps 脚本,并成功地为每一行/条目以及该行中的文本、电子表格创建了幻灯片,但在尝试使用 URL 插入图像时遇到了困难。在运行时,我抛出异常“无法检索 URL 处的图像......请确保图像有效且可公开访问。” 当我手动尝试通过菜单栏通过 URL 插入图像时,我也会遇到同样的错误。我驱动器中的所有文件夹和文件都不受限制,因此互联网上的任何人都可以通过此链接查看图像。我已经在我的浏览器和其他浏览器中进行了测试,它成功打开了图像,但在幻灯片中没有。

这是我从函数中使用的一些代码,但不包括对电子表格的引用。也尝试在 URL 末尾添加“/edit”、“/view”,但结果相同。记录器确实显示正在从工作表中获取 URL。

function CreateSlides() {
  var deck = SlidesApp.getActivePresentation();
  var a = 2;  // data begins in second row in spreadsheet
  while (sheet.getRange(a,2,1,1).getValue() != "") {
  //Insert new slide if not first slide
    if (a != 2) {
      slide = deck.insertSlide(a-2,SlidesApp.PredefinedLayout.BLANK);  //add new slide
    } else {
      slide = deck.getSlides()[0]; //get first slide
    }
  // Insert image of painting
  Logger.log(sheet.getRange(paintingURLCol + a).getValue());
  var picture = slide.insertImage(sheet.getRange(paintingURLCol + a).getValue() + '/view');
  // Edit picture size and position
  // Increment counter to next row in sheet
  a++;
  }
}

标签: google-apps-scriptgoogle-sheetsgoogle-formsgoogle-slidesinsert-image

解决方案


Google 表单按上传问题类型记录的 URL 不能与诸如此类的方法一起使用,insertImage(URL)因为这些 URL 不受支持。相反,您应该使用:

https://drive.google.com/uc?id=DRIVE_FILE_ID

上述情况是因为记录到电子表格的 URL 指向嵌入图像的页面,而不是实际的图像文件。

有关的


推荐阅读