marmor
marmor

Reputation: 28239

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?

Upvotes: 2

Views: 2252

Answers (3)

Hamzah Djuned
Hamzah Djuned

Reputation: 11

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 (i.e., 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"

Upvotes: 1

user6637260
user6637260

Reputation:

Deploy your Google Apps Script as Web Apps > reference, by this way you can run function Get(e) or Post(e) and invoke other functions inside one of them with conditions....

Upvotes: 1

Jason Allshorn
Jason Allshorn

Reputation: 1625

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.

Upvotes: 1

Related Questions