简体   繁体   中英

call a Google Spreadsheets Apps Script function from Google Sheets API v4

I have a Spreadsheet with some Apps Script functions bound to it.

I also have an Android client using Google Sheets API v4 to interact with that spreadsheet.

Is there a way for the Android client to call/run some function in the Apps Script code?

The reason I need to code to be run on the Apps Script side, and not simply on the Android client, is because I'm sending some email when something happens to the doc, and I would like the email to be sent from the owner account of the spreadsheet, and not from the Android user authenticated via the API.

I know I can trigger functions implicitly like by adding rows to a doc, but is there a way to directly run a specific function?

Yes. You can make GET and POST requests to Google apps-scripts. from anywhere that can make REST type calls including clients. If you need authentication there is also the apps-script client libraries. I wrote a short script for emailing from a request from one apps-script to another here . But, it would work if you called the emailing script from your client also.

将Google Apps脚本部署为Web Apps> reference ,通过这种方式,您可以运行函数Get(e)Post(e)并在有条件的情况下调用其中一个函数中的其他函数。

You might have gotten the answer to your question. Just in case you have not, below are some points that may help with your development:

1) Create the server side script (ie, Google Apps Script) function like usual:

function myFunction(inputVar) {
// do something
return returnVar;
}

2) Create a doGet(e) or doPost(e) function like below - can be in the same .gs file with the function in 1) :

function doGet(e) {

    var returnVar = "";
    if (e.parameter.par1 != null) {
        var inputVar = e.parameter.par1;
        returnVar = myFunction(inputVar);
    }

    return HtmlService.createHtmlOutput(returnVar);

}

3) Publish and deploy your project as webapp. Note the deployed URL.

4) From your Android client do HTTP call with the URL as: your_webapp_url?par1="input value"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM