javascript 如何从网页调用 Google Apps 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14914955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Call Google Apps Script from Web Page
提问by carter
Have searched high and low for this. I have a web page of basic HTML/CSS/JS. I want users to be able to visit the page and upon opening page, a call is made to a google script I made which takes information from a spreadsheet and displays some of it on the page. I am hoping I don't have to do any fancy set up like in Google's tutorials because none of them were helpful to me.
对此进行了高低搜索。我有一个基本的 HTML/CSS/JS 网页。我希望用户能够访问该页面,并在打开页面时调用我制作的 google 脚本,该脚本从电子表格中获取信息并在页面上显示其中的一些信息。我希望我不必像 Google 的教程中那样做任何花哨的设置,因为它们都对我没有帮助。
My Webpage ----> Google Script ----> Google Spreadsheet
My Webpage <---- Google Script <---- Google Spreadsheet
我的网页 ----> Google Script ----> Google 电子表格
我的网页 <---- Google Script <---- Google 电子表格
Users should be able to select an item shown on the webpage (item populated from spreadsheet) and click a button which will allow users to enter a new page with a URL derived from the selected item.
用户应该能够选择网页上显示的项目(从电子表格填充的项目)并单击一个按钮,该按钮将允许用户使用从所选项目派生的 URL 进入新页面。
This is essentially a chat room program where the chat rooms are stored on a spreadsheet. I want users to be able to create a new chat room as well which should update the google spreadsheet.
这本质上是一个聊天室程序,其中聊天室存储在电子表格中。我希望用户也能够创建一个新的聊天室,它应该更新谷歌电子表格。
回答by Phil Bozak
Look into using the GET parameters. https://stackoverflow.com/a/14736926/2048063.
研究使用 GET 参数。https://stackoverflow.com/a/14736926/2048063。
Here's a previous question on the topic.
You can access the parameters passed by GET in your doGet(e)
function using e.parameter
. If you call http://script.google......./exec?method=doSomething
, then
您可以doGet(e)
使用e.parameter
. 如果你打电话http://script.google......./exec?method=doSomething
,那么
function doGet(e) {
Logger.log(e.parameter.method);
}
doSomething
will be written to the log, in this case.
doSomething
在这种情况下,将被写入日志。
Returning data from the script can be done using the ContentService, which allows you to serve JSON (I recommend). JSON is easiest (in my opinion) to make on the GAS end, as well as use on the client end.
从脚本返回数据可以使用ContentService来完成,它允许您提供 JSON(我推荐)。JSON 最容易(在我看来)在 GAS 端制作,也最容易在客户端使用。
The initial "populate list" call would look something like this. I will write it in jQuery because I feel that is very clean.
最初的“填充列表”调用看起来像这样。我会用 jQuery 写它,因为我觉得它很干净。
var SCRIPT_URL = "http://script.google.com/[....PUT YOUR SCRIPT URL HERE....]/exec";
$(document).ready(function() {
$.getJSON(SCRIPT_URL+"?callback=?",
{method:"populate_list"},
function (data) {
alert(JSON.stringify(data));
});
});
And the corresponding GAS that produces this.
以及产生此的相应 GAS。
function doGet(e) {
if (e.parameter.method=="populate_list") {
var v = {cat:true,dog:false,meow:[1,2,3,4,5,6,4]}; //could be any value that you want to return
return ContentService.createTextOutput(e.parameter.callback + "(" + JSON.stringify(v) + ")")
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
}
This method is called JSONP, and it is supported by jQuery. jQuery recognizes it when you put the ?callback=?
after your URL. It wraps your output in a callback function, which allows that function to be run on your site with the data as an argument. In this case, the callback function is the one defined in the line that reads function (data) {
.
这种方法称为 JSONP,jQuery 支持它。当您?callback=?
在 URL 之后放置 jQuery 时,jQuery 会识别它。它将您的输出包装在一个回调函数中,这允许该函数以数据作为参数在您的站点上运行。在这种情况下,回调函数是在读取 的行中定义的函数function (data) {
。