简体   繁体   English

如何从 Google Apps Script Web App 返回图像?

[英]How to return an image from a Google Apps Script Web App?

I am trying to make a Web App with Google Apps Script that actually returns an image file instead of just text.我正在尝试使用 Google Apps 脚本制作一个 Web 应用程序,该应用程序实际上返回一个图像文件,而不仅仅是文本。 I tried it as simple as that:我试过就这么简单:

function doGet() {
    var file = DriveApp.getFileById('[redacted]');
    return file;
}

When publishing it as a web app and executing it, it just says将其发布为 Web 应用程序并执行时,它只是说

The script completed but did not return anything.脚本完成但没有返回任何内容。

I know the file exists and is accessible, since I can print its MIME type, which is a proper image/png .我知道该文件存在并且可以访问,因为我可以打印它的 MIME 类型,这是一个正确的image/png So I'm missing something obvious here, but being not particularly well-versed in Google Apps Script, let alone web development of any kind, I don't know what that is I'm missing and searching for introductory material on returning an image from a Google Apps Script has not been particularly fruitful either, especially when I'm not realyl sure where to start at all.所以我在这里遗漏了一些明显的东西,但不是特别精通 Google Apps 脚本,更不用说任何类型的网络开发了,我不知道我错过了什么,正在寻找有关返回图像的介绍性材料来自 Google Apps Script 也不是特别有成效,尤其是当我完全不确定从哪里开始时。

Is it even possible to return an image file from a web app via Google Apps Script?甚至可以通过 Google Apps 脚本从网络应用程序返回图像文件吗?

doGet() is a reserved function name. doGet()是保留的函数名。 The function name is reserved for listening to a GET request made to the published URL of the Web App.函数名称保留用于侦听对 Web 应用程序的已发布 URL 发出的 GET 请求。 The GET request that is made to the published URL of the Web App is the "event" and the doGet() function is triggered by the event.对 Web App 的已发布 URL 发出的 GET 请求是“事件”,并且doGet()函数由该事件触发。

The doGet() function can return two different types of output. doGet()函数可以返回两种不同类型的输出。

  • HTML Output Object - HtmlService HTML 输出对象 - HtmlService
  • Text Output Object - ContentService文本输出对象 - ContentService

The most common use of doGet() is to return HTML content to the browser. doGet()最常见的用途是将 HTML 内容返回给浏览器。 But using HtmlService to return file content won't work.但是使用 HtmlService 返回文件内容将不起作用。

doGet() can also be used together with "Content Service" to return text content in various forms. doGet()还可以与“内容服务”一起使用,以各种形式返回文本内容。

The text content can be returned as plain text, or the MIME Type of the text content can be set before returning it.文本内容可以以纯文本形式返回,也可以在返回之前设置文本内容的MIME Type。 The MIME type can be: MIME 类型可以是:

  • ATOM原子能
  • CSV CSV
  • ICAL卡尔加里
  • JAVASCRIPT爪哇脚本
  • JSON JSON
  • RSS RSS
  • TEXT文本
  • VCARD电子卡
  • XML XML

But there is no MIME Type setting to return file types.但是没有返回文件类型的 MIME 类型设置。

If the file can be converted to text content, returned, and then formatted in the browser, then that is a possibility.如果文件可以转换为文本内容,返回,然后在浏览器中格式化,那么这是一种可能。 If you want to return something like an image file, then you would need to convert the image to a Blob, then get the blob as a string, and then return the string.如果要返回图像文件之类的内容,则需要将图像转换为 Blob,然后将 Blob 作为字符串获取,然后返回该字符串。

The function doGet must return an HtmlOutput object, not a file object.函数doGet必须返回一个HtmlOutput对象,而不是一个文件对象。 Such an object is created using HtmlService .这样的对象是使用HtmlService创建的。 Example:示例:

function doGet() {
  var html = '<img src = "https://googledrive.com/host/{file id}">'; 
  return HtmlService.createHtmlOutput(html);
}

This example will no longer work after August 31, 2016, when Google shuts down Drive Hosting .此示例在 2016 年 8 月 31 日之后将不再有效,届时 Google关闭了云端硬盘托管

Hence, the suggestion is to host static content elsewhere and use Apps Script web apps to generate HTML serving them:因此,建议在别处托管静态内容,并使用 Apps Script Web 应用程序生成 HTML 服务于它们:

function doGet() {
  var html = '<img src = "https://example.com/filename">'; 
  return HtmlService.createHtmlOutput(html);
}

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

相关问题 使用云端硬盘中的图片作为Google Apps脚本网络应用中的按钮 - Use an Image from Drive for a button in Google Apps Script web app Google Apps 脚本 Web 应用图像解决方法 - Google Apps Script Web App image workaround Web App HTML,JS + Google Apps脚本:在GAS中返回后如何返回我的应用程序? - Web App HTML, JS + Google Apps Script: How to go back to my app after return in GAS? 如何让用户从Google Apps脚本网络应用访问Google表格中的数据? - How to let users access data from a Google Sheet from a Google Apps Script web app? 如何通过 Google Apps Script Web App 将 jpg 或 png 图像转换为 Blob? - How do I convert jpg or png image to Blob via a Google Apps Script Web App? 如何将特定数据从谷歌应用程序脚本返回到 html 网页 - how to return specific data from a google apps script into html web page 从表单谷歌应用程序脚本Web应用程序动态列表 - google apps script web app dynamic list from form 从Google Apps脚本网络应用创建表单触发器 - Creating a form trigger from a google apps script web app 如何允许其他人访问使用Google Apps脚本制作的网络应用? - How to allow others to access a web app made from a Google Apps Script? 如何将数据从 Puppeteer 发布到 Google Apps 脚本 web 应用 API 端点 node.js 中? - How to POST data from Puppeteer to Google Apps Script web app API endpoint in node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM