简体   繁体   English

使用 Google Apps Scripts 将 Google Drive 图像插入到模板化的 html 中

[英]Inserting Google Drive image into templated html using Google Apps Scripts

I am using templated html and HtmlService in Google Apps Scripts to generate pdf reports.我在 Google Apps Scripts 中使用模板化的 html 和 HtmlService 来生成 pdf 报告。 I am trying to insert an image from my Google Drive into the html template.我正在尝试将我的 Google Drive 中的图像插入到 html 模板中。

The templated html looks like this:模板化的 html 如下所示:

<img src= <?= DriveApp.getFileById("1234567890-abcdefghijklmnopqrstuvwxyz").getAs("image/jpeg");?> >

and the output html looks like this:输出 html 如下所示:

<img src="Blob">

If I remove the .getAs() function then the output is:如果我删除.getAs()函数,则输出为:

<img src="image name.jpg"> 

Both snippets of code only result in a hollow, square box only a few pixels wide when the html is evaluated.在评估 html 时,这两个代码片段只会产生一个只有几个像素宽的空心方形框。 Is there any way that the image can be inserted into the html instead of a Blob or File objects?有什么方法可以将图像插入 html 而不是BlobFile对象吗?

It's because the IMG tag cannot use a blob as a source, use这是因为 IMG 标签不能使用 blob 作为源,使用

<img src= <?= DriveApp.getFileById(id).getDownloadUrl();?> >

Chek the documentation getDowloadUrl() 查看文档getDowloadUrl()

Regarding to put an image into HTML in google apps script you should:关于在谷歌应用程序脚本中将图像放入 HTML,您应该:

  1. Upload to google drive上传到谷歌驱动器
  2. Get the ID of the image获取图片的ID
  3. Then put in in the format below然后按照下面的格式输入
img src="https://drive.google.com/uc?export=view&id=1dPKfyZlZFZzauqA8DcaB3h6oHX7sPqs8" width="150px" height="200px" border="5"

1dPKfyZlZFZzauqA8DcaB3h6oHX7sPqs8 is the ID of the picture. 1dPKfyZlZFZzauqA8DcaB3h6oHX7sPqs8是图片的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM