如何从网页调用 Google Apps 脚本 [英] How to Call Google Apps Script from Web Page

查看:38
本文介绍了如何从网页调用 Google Apps 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为此搜索了高低.我有一个基本的 HTML/CSS/JS 网页.我希望用户能够访问该页面,并在打开页面时调用我制作的 google 脚本,该脚本从电子表格中获取信息并在页面上显示其中的一些信息.我希望我不必像 Google 的教程中那样做任何花哨的设置,因为它们都对我没有帮助.

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.

我的网页---->谷歌脚本---->谷歌电子表格
我的网页 <---- Google 脚本 <---- Google 电子表格

My Webpage ----> Google Script ----> Google Spreadsheet
My Webpage <---- Google Script <---- Google Spreadsheet

用户应该能够选择网页上显示的项目(从电子表格填充的项目),然后点击一个按钮,允许用户进入一个新页面,其中的 URL 来自所选项目.

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.

这本质上是一个聊天室程序,其中聊天室存储在电子表格中.我希望用户也能够创建一个新的聊天室来更新谷歌电子表格.

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.

推荐答案

研究使用 GET 参数.https://stackoverflow.com/a/14736926/2048063.

Look into using the GET parameters. https://stackoverflow.com/a/14736926/2048063.

这是关于该主题的上一个问题.

您可以使用 e.parameterdoGet(e) 函数中访问 GET 传递的参数.如果您调用 http://script.google......./exec?method=doSomething,则

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

function doGet(e) {
  Logger.log(e.parameter.method);
}

在这种情况下,

doSomething 将写入日志.

doSomething will be written to the log, in this case.

可以使用 ContentService 从脚本中返回数据,它允许您提供 JSON(我推荐).JSON 最容易(在我看来)在 GAS 端制作,也最容易在客户端使用.

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.

最初的填充列表"调用看起来像这样.我会用jQuery来写,因为我觉得很干净.

The initial "populate list" call would look something like this. I will write it in jQuery because I feel that is very clean.

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)); 
              });
});

以及产生它的相应 GAS.

And the corresponding GAS that produces this.

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);
  }
}

这个方法叫做JSONP,jQuery支持.当您将 ?callback=? 放在 URL 之后时,jQuery 会识别它.它将您的输出包装在一个回调函数中,这允许该函数以数据作为参数在您的站点上运行.在这种情况下,回调函数是在读取 function (data) {.

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) {.

这篇关于如何从网页调用 Google Apps 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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