如何在谷歌应用程序脚本中使用<img rel="nofollow noreferrer" src>

Jon*_*ona 4 html google-apps-script

我想使用src属性在google apps脚本中显示图像.我把文件夹id和路径.但图像无法显示.我在这里分享了这个文件夹

谢谢您的帮助!

https://drive.google.com/drive/folders/0ByNw-B9nXMcbMkxHTGt2X2xUTzQ

function doGet() {
  var html = HtmlService.createTemplateFromFile('index').evaluate()
    .setTitle('picture').setSandboxMode(HtmlService.SandboxMode.NATIVE);
  return html;
}

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  <p> Dispaly picture </p>
    <p><img src="https://drive.google.com/drive/folders/0ByNw-B9nXMcbMkxHTGt2X2xUTzQ/200w.gif"> </p>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Jam*_*s D 10

img src=""要求你要显示的实际图像,不包含它的文件夹中的网址.要获得正确的链接,您需要在自己的窗口中打开图片或单击共享并以此方式获取链接.

您图片的正确链接是:

https://drive.google.com/file/d/0ByNw-B9nXMcbSTN2S2ZiYWprdzA/view

要在src中使用它,您需要将fileID添加到此格式URL的末尾:

https://drive.google.com/uc?export=download&id=

所以它变成:

https://drive.google.com/uc?export=download&id=0ByNw-B9nXMcbSTN2S2ZiYWprdzA

注意:驱动器文件必须与"有链接的任何人"共享,以便其他用户能够查看文件或在G Suite外部使用.


Fat*_*son 6

出于某种原因,James Donnellan 接受的答案对我不起作用。当我发布页面时,我可以看到图像,但其他人看不到。我检查了共享设置,并尝试使用“以我的身份执行”选项进行发布。也尝试了给定的链接,结果相同。

虽然上面提到的方法使用起来要简单得多,但我想分享一下对我有用的方法,以防其他人遇到同样的问题。我使用将图像转换为字节:

在应用脚本中:

function loadImageBytes(){
var id = "your_drive_file_id"
var bytes = DriveApp.getFileById(id).getBlob().getBytes();
return Utilities.base64Encode(bytes);
}
Run Code Online (Sandbox Code Playgroud)

在 JavaScript 中

google.script.run
.withSuccessHandler( function(bytes){ showImage(bytes) })
.loadImageBytes();

function showImage(bytes){
document.getElementById("ItemPreview").src = "data:image/png;base64," + bytes; 
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助任何人。