Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 谷歌应用程序脚本-使用谷歌驱动器ID将图像插入电子表格单元格_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 谷歌应用程序脚本-使用谷歌驱动器ID将图像插入电子表格单元格

Google apps script 谷歌应用程序脚本-使用谷歌驱动器ID将图像插入电子表格单元格,google-apps-script,google-sheets,Google Apps Script,Google Sheets,正如我从电子表格界面上看到的,你可以直接从谷歌硬盘上提供一个文件 选择将图像放置在单元格中或单元格上方。带有图像的单元格也不能有文本 但是在AppsScript中,我无法在单元格模式下执行“” 直接从Blob源代码添加图像(因此使用AppsScript代码可以下载Blob内容并将其提供给函数),但不适合我的情况 要以编程方式将图像直接插入单元格,我找到的唯一方法是使用=image(),但在这种情况下需要一个公共url 这就是我目前提出的解决方案: function getCdnImageUrl_

正如我从电子表格界面上看到的,你可以直接从谷歌硬盘上提供一个文件

选择将图像放置在单元格中或单元格上方。带有图像的单元格也不能有文本

但是在AppsScript中,我无法在单元格模式下执行“

直接从Blob源代码添加图像(因此使用AppsScript代码可以下载Blob内容并将其提供给函数),但不适合我的情况

要以编程方式将图像直接插入单元格,我找到的唯一方法是使用
=image()
,但在这种情况下需要一个公共url

这就是我目前提出的解决方案:

function getCdnImageUrl_(driveFile) {
  // https://stackoverflow.com/a/59537829
  let imageUrl = "https://docs.google.com/uc?id=" + driveFile.getId();
  console.info("Access to embeddable link %s", imageUrl);

  // Retrieve url of cdn disabling redirects
  let fetchResponse = UrlFetchApp.fetch(imageUrl, {
    followRedirects: false
  });

  // Access to 302 header "Location"
  let cdnUrl = fetchResponse.getAllHeaders()["Location"]
  console.info("CDN URL %s", cdnUrl);

  return cdnUrl;
}

function insertImageAsFormula_(range, driveFile) {
  // Retrieve url
  let url = getCdnImageUrl_(driveFile);

  // Set formula
  range.setFormula("=IMAGE(\"" + url + "\")");
}


function main() {
  let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = spreadsheet.getActiveSheet();
  let range = sheet.getRange("A1");

  let driveImageId = "my-drive-image-id";
  let driveFile = DriveApp.getFileById(driveImageId);

  // Set file as publicly available so CDN url will be callable for anonymous users
  driveFile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);

  // Retrieve url from CDN and set as formula
  insertImageAsFormula_(range, driveFile);

  // Force Spreadsheet to update view (download the image) otherwise when the file will be restored to "private" the link will provide HTTP 403
  SpreadsheetApp.flush();

  // Restore previous sharing access
  driveFile.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.VIEW);
}
一旦电子表格下载了图像,我发现如果使用的url返回经过身份验证的图像是没有问题的,因为图像已经下载并在视图中

您能否确认,目前没有直接的方法使用AppsScript在单元格中添加图像?我不太高兴我需要公开文件(即使时间很短),您对如何执行相同的操作以保持文件的私有性有什么建议吗


编辑 最后,我将文件从谷歌硬盘上传到谷歌云存储,以创建signedUrl

使用这种方法,驱动器文件是私有的,并且保持私有,生成的url在配置TTL后自动过期,并且url本身不可猜测,因为它包含签名

您能否确认,目前没有直接的方法使用AppsScript在单元格中添加图像

答复: 我完全可以证实这一点

更多信息: 由于在单元格中添加图像时没有使用
=IMAGE
公式,因此在撰写本文时,目前无法通过应用程序脚本或API实现这一点

然而,在2019年3月27日发布的一份关于谷歌问题追踪者的报告中,已经有这样的要求:

你可以打电话☆ 在功能请求左上角的问题编号旁边,它让谷歌知道更多的人希望实现该功能,因此更有可能看到该功能更快

我希望这对你有帮助

参考资料:
当使用
=IMAGE()
时,图像需要公开共享。这似乎是当前的规范。当访问令牌能够用作查询参数时,这就是解决方法。但现在,这是不能使用的。因此,作为其他解决方法,首先,图像以“单元内”模式作为一个strage手动放入电子表格。如果要将图像复制到所使用的电子表格中,该单元格将从存储电子表格复制到所使用的电子表格中。这样,你的目标就可以实现。但如果这不是你想要的方向,我道歉。