从外部 URL 调用自定义 GAS 函数 [英] Call a custom GAS function from external URL

查看:18
本文介绍了从外部 URL 调用自定义 GAS 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用我在 Google Apps 脚本中编写的自定义函数.当我执行 getJSON 时,我想它会自动运行我的 doGet(e).

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

我的 JavaScript:

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

我的 Google Apps 脚本:

My Google Apps Script:

function getNumberOfFans(e){ 
   //code here
}

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

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

推荐答案

  • 在独立"或绑定的 Apps 脚本文件中添加 doGet(e) 函数.
  • 将 Apps 脚本文件发布为 Web 应用程序.
  • 获取已发布的 Web 应用 URL.
  • 在 URL 末尾添加搜索字符串参数.
  • 您可以将搜索字符串参数添加到已发布的 Wep 应用程序的 URL.

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

    这是一个例子:

    https://script.google.com/macros/s/[ID]/exec?searchStringName=functionOne
    

    搜索字符串位于 URL 的末尾,exec 之后.您必须在 execname=value

    The search string is at the end of the URL, after exec. You must add a question mark after exec and then name=value

    url/exec?name=value
    

    其中 namevalue 将替换为您的选择.

    Where name and value will be replaced with your choices.

    将事件参数(由字母e"表示)放入doGet(e) 函数中,而不是您要使用的函数.

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

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

    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.

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

    For an explanation on URL Parameters, see this documentation:

    网址参数

    这篇关于从外部 URL 调用自定义 GAS 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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