研究使用 GET 参数。 https://stackoverflow.com/a/14736926/2048063.
Here's a previous question on the topic.
您可以使用e.parameter 在您的doGet(e) 函数中访问通过GET 传递的参数。如果你打电话给http://script.google......./exec?method=doSomething,那么
function doGet(e) {
Logger.log(e.parameter.method);
}
doSomething 将被写入日志,在这种情况下。
可以使用ContentService 从脚本返回数据,它允许您提供 JSON(我推荐)。 JSON 最容易(在我看来)在 GAS 端制作,以及在客户端使用。
最初的“填充列表”调用看起来像这样。我会用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));
});
});
以及产生这个的相应 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);
}
}
这种方法称为JSONP,jQuery支持。当您将?callback=? 放在您的 URL 之后时,jQuery 会识别它。它将您的输出包装在一个回调函数中,该函数允许该函数以数据作为参数在您的站点上运行。在这种情况下,回调函数是在读取function (data) { 的行中定义的函数。