从外部URL调用自定义GAS功能

时间:2022-08-15 16:55:57

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

我想调用我在Google Apps脚本中编写的自定义函数。当我执行getJSON时,我想它会自动运行我的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:

我的Google Apps脚本:

function getNumberOfFans(e){ 
   //code here
}

Do I have to add some kind of extra function parameter to my URL?

我是否必须在URL中添加某种额外的功能参数?

1 个解决方案

#1


  • In a "stand alone" Apps Script file add a doGet(e) function.
  • 在“独立”Apps脚本文件中添加doGet(e)功能。

  • Publish the Apps Script file as a Web App.
  • 将Apps脚本文件发布为Web App。

  • Get the published URL of the Web App.
  • 获取Web App的已发布URL。

  • Add a search string parameter to the end of the URL.
  • 将搜索字符串参数添加到URL的末尾。

You can add search string parameters to the URL of the published Wep App.

您可以将搜索字符串参数添加到已发布的Wep App的URL中。

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

在exec之后,搜索字符串位于URL的末尾。您必须在exec之后添加问号,然后添加name = value

Put the event argument (denoted by the letter "e") into the doGet(e) function, not the function you want used.

将事件参数(用字母“e”表示)放入doGet(e)函数中,而不是您想要使用的函数。

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.

以上代码用于GET请求。如果要使用POST请求,请不要在URL中使用搜索字符串。对于POST请求,您将在有效负载中发送信息。您仍将使用e.parameter访问发送的数据,但e.parameter中的任何内容都将是具有键/值对的对象。您需要知道在对象中发送的键(属性)名称是什么。

For an explanation on URL Parameters, see this documentation:

有关URL参数的说明,请参阅此文档:

URL Parameters

#1


  • In a "stand alone" Apps Script file add a doGet(e) function.
  • 在“独立”Apps脚本文件中添加doGet(e)功能。

  • Publish the Apps Script file as a Web App.
  • 将Apps脚本文件发布为Web App。

  • Get the published URL of the Web App.
  • 获取Web App的已发布URL。

  • Add a search string parameter to the end of the URL.
  • 将搜索字符串参数添加到URL的末尾。

You can add search string parameters to the URL of the published Wep App.

您可以将搜索字符串参数添加到已发布的Wep App的URL中。

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

在exec之后,搜索字符串位于URL的末尾。您必须在exec之后添加问号,然后添加name = value

Put the event argument (denoted by the letter "e") into the doGet(e) function, not the function you want used.

将事件参数(用字母“e”表示)放入doGet(e)函数中,而不是您想要使用的函数。

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.

以上代码用于GET请求。如果要使用POST请求,请不要在URL中使用搜索字符串。对于POST请求,您将在有效负载中发送信息。您仍将使用e.parameter访问发送的数据,但e.parameter中的任何内容都将是具有键/值对的对象。您需要知道在对象中发送的键(属性)名称是什么。

For an explanation on URL Parameters, see this documentation:

有关URL参数的说明,请参阅此文档:

URL Parameters