Call a custom GAS function from external URL

冷暖自知 提交于 2019-11-26 22:36:08
  • In either a "stand alone" or bound 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 Parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!