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

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

问题描述

搜索高和低这一点。我有一个基本的HTML / CSS / JS网页。我希望用户能够访问该页面,并在打开的页面中,调用了一个谷歌的脚本我做这需要从为preadsheet信息,并显示它的一些在页面上。我希望我没有做任何花哨的设置像谷歌的教程,因为他们都不是对我很有帮助。

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.

我的网页---->谷歌脚本---->谷歌小号preadsheet
我的网页< ----谷歌脚本< ----谷歌小号preadsheet

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

用户应当能够选择在网页上(人口从s preadsheet项)所示的项目,然后单击一个按钮,这将允许用户进入一个新的页面与所选择的项目得出的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.

这基本上是那里的聊天室被存储在为preadsheet一个聊天室程序。我希望用户能够创建一个新的聊天室,以及应该更新谷歌小号preadsheet。

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参数。 <一href="http://stackoverflow.com/a/14736926/2048063">http://stackoverflow.com/a/14736926/2048063.

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

这里的话题一个previous的问题。

Here's a previous question on the topic.

您可以访问得到你的的doGet(五)通过参数使用功能 e.parameter 。如果你调用的http://script.google......./exec方法= 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是最简单的(在我看来),使上气端,以及在客户端使用。

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

和产生此相应的气体。

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 + "(" + Utilities.jsonStringify(v) + ")")
        .setMimeType(ContentService.MimeType.JSON);
  }
}

该方法被称为JSONP,它支持jQuery的。 jQuery的认识它,当你把?回调=?您的网址后。它包装在一个回调函数,这使得该功能可以在您的网站的数据作为参数运行的输出。在这种情况下,回调函数是在读功能(数据){

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

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

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