如何从网页调用Google Apps脚本

时间:2020-12-06 23:19:39

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脚本,该脚本从电子表格中获取信息并在页面上显示一些信息。我希望我不必像谷歌的教程那样做任何花哨的设置,因为它们都没有对我有帮助。

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

我的网页----> Google脚本----> 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.

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

1 个解决方案

#1


15  

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

查看使用GET参数。 https://*.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

您可以使用e.parameter访问doGet(e)函数中GET传递的参数。如果你调用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 =?时,jQ​​uery会认出它?在您的URL之后。它将您的输出包装在回调函数中,该函数允许以您的数据作为参数在您的站点上运行该函数。在这种情况下,回调函数是在读取函数(数据){的行中定义的函数。

#1


15  

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

查看使用GET参数。 https://*.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

您可以使用e.parameter访问doGet(e)函数中GET传递的参数。如果你调用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 =?时,jQ​​uery会认出它?在您的URL之后。它将您的输出包装在回调函数中,该函数允许以您的数据作为参数在您的站点上运行该函数。在这种情况下,回调函数是在读取函数(数据){的行中定义的函数。