如何从 Google Apps Script Web App 返回图像? [英] How to return an image from a Google Apps Script Web App?

查看:21
本文介绍了如何从 Google Apps Script Web App 返回图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Google Apps 脚本制作一个实际返回图像文件而不仅仅是文本的 Web 应用程序.我试过就这么简单:

I am trying to make a Web App with Google Apps Script that actually returns an image file instead of just text. 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

脚本完成但没有返回任何内容.

The script completed but did not return anything.

我知道文件存在并且可以访问,因为我可以打印它的 MIME 类型,这是一个正确的 image/png.所以我在这里遗漏了一些明显的东西,但不是特别精通 Google Apps 脚本,更不用说任何类型的网络开发了,我不知道我错过了什么,正在寻找有关返回图像的介绍性材料来自 Google Apps Script 的结果也不是特别好,尤其是当我完全不确定从哪里开始时.

I know the file exists and is accessible, since I can print its MIME type, which is a proper 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 脚本从网络应用返回图像文件?

Is it even possible to return an image file from a web app via Google Apps Script?

推荐答案

doGet() 是保留的函数名.函数名称保留用于侦听对 Web 应用程序的已发布 URL 发出的 GET 请求.对 Web 应用程序的已发布 URL 发出的 GET 请求是事件".并且 doGet() 函数由事件触发.

doGet() is a reserved function name. The function name is reserved for listening to a GET request made to the published URL of the Web App. 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.

doGet() 函数可以返回两种不同类型的输出.

The doGet() function can return two different types of output.

  • HTML 输出对象 - HtmlService
  • 文本输出对象 - ContentService

doGet() 最常见的用途是将 HTML 内容返回给浏览器.但是使用 HtmlService 返回文件内容不起作用.

The most common use of doGet() is to return HTML content to the browser. But using HtmlService to return file content won't work.

doGet() 也可以与内容服务"一起使用.以各种形式返回文本内容.

doGet() can also be used together with "Content Service" to return text content in various forms.

文本内容可以以纯文本形式返回,也可以在返回之前设置文本内容的MIME Type.MIME 类型可以是:

The text content can be returned as plain text, or the MIME Type of the text content can be set before returning it. The MIME type can be:

  • 原子
  • CSV
  • ICAL
  • JAVASCRIPT
  • JSON
  • RSS
  • 文本
  • VCARD
  • XML

但是没有返回文件类型的 MIME 类型设置.

But there is no MIME Type setting to return file types.

如果文件可以转换为文本内容,返回,然后在浏览器中格式化,那么这是一种可能.如果要返回图像文件之类的内容,则需要将图像转换为 Blob,然后将 Blob 作为字符串获取,然后返回字符串.

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.

这篇关于如何从 Google Apps Script Web App 返回图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆