从外部URL调用自定义的GAS功能 [英] Call a custom GAS function from external URL
问题描述
我想调用我在Google Apps脚本中编写的自定义函数。当我执行getJSON时,我想它会自动运行我的doGet(e)。
我的Javascript:
$ .getJSON(https:// script.google.com/macros/s/[ID]/exec,function(data){
// code here
});
是否有可能的方式调用我的自定义函数之一例如
我的Google Apps脚本:
function getNumberOfFans(e){
// code这里
}
我是否需要为我的网址添加一些额外的功能参数?
- 在独立Apps脚本文件中添加
(e)
函数 - 发布Apps脚本文件作为Web应用程序。
- 获取发布的网址Web应用程序。
- 将搜索字符串参数添加到URL的末尾。
您可以将搜索字符串参数添加到发布的Wep App的URL中。
以下是一个示例:
https://script.google.com/macros/s/ [ID] / exec?searchStringName = functionOne
搜索字符串位于URL末尾,位于 exec
之后。您必须在 exec
然后 name = value
$ b $之后添加问号b
将事件参数(用字母e表示)放入 doGet(e)
函数中,而不是要使用的函数。
函数doGet(e){
var passedString,whatToReturn;
passedString = e.parameter.searchStringName;
if(passedString ==='functionOne'){
whatToReturn = functionOne(); //运行函数One
};
返回ContentService.createTextOutput(whatToReturn);
};
函数functionOne(){
var something;
//。 。 。 。码;
something = code here;
返回一些东西;
};
以上代码是针对GET请求的。如果您想使用POST请求,请不要在URL中使用搜索字符串。对于POST请求,您将在有效负载中发送信息。您仍然可以使用 e.parameter
来访问发送的数据,但是 e.parameter
中的任何内容都将成为具有键/值对的对象。您需要知道在对象中发送的密钥(属性)名称。
有关URL参数的说明,请参阅此文档:
I want to call a custom function I wrote within my Google Apps Script. When I execute a getJSON I suppose it'll automatically run my doGet(e).
My Javascript:
$.getJSON(https://script.google.com/macros/s/[ID]/exec, function(data){
//code here
});
Is there a possible way to call one of my custom functions for example
My Google Apps Script:
function getNumberOfFans(e){
//code here
}
Do I have to add some kind of extra function parameter to my URL?
- In a "stand alone" Apps Script file add a
doGet(e)
function. - Publish the Apps Script file as a Web App.
- Get the published URL of the Web App.
- Add a search string parameter to the end of the URL.
You can add search string parameters to the URL of the published Wep App.
Here is an example:
https://script.google.com/macros/s/[ID]/exec?searchStringName=functionOne
The search string is at the end of the URL, after exec
. You must add a question mark after exec
and then name=value
Put the event argument (denoted by the letter "e") into the doGet(e)
function, not the function you want used.
function doGet(e) {
var passedString,whatToReturn;
passedString = e.parameter.searchStringName;
if (passedString === 'functionOne') {
whatToReturn = functionOne(); //Run function One
};
return ContentService.createTextOutput(whatToReturn);
};
function functionOne() {
var something;
//. . . . Code;
something = code here;
return something;
};
The above code is for a GET request. If you want to use a POST request, don't use a search string in the URL. For a POST request, you will send information in the payload. You'll still use e.parameter
to access the data sent, but whatever is in e.parameter
will be an object with key/value pairs. You'll need to know what the key (property) name is that was sent in the object.
For an explanation on URL Parameters, see this documentation:
这篇关于从外部URL调用自定义的GAS功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!