Learn the powerful enterprise adaptable database:

Getting Started With ADABAS & Natural

Tuesday, April 4, 2017

101 Apps Script: Publish As Web App


.
101 Apps Script: Publish As Web App

Objective:

This tutorial demonstrates app script that is attached to a spreadsheet.
If you want to run a standalone, jump to Step no 3.

1. Create Google Spreadsheet.

Give a name eg 101appdata.

2. Create App Script.

In Google Spreadsheet, go to menu and select Tools/Script Editor.
Gine a name eg 101appdatascript.

3. Add startup codes.


/*script id*/
var SCPID=ScriptApp.getScriptId();
/*active spreadsheet */
var SST=SpreadsheetApp.getActiveSpreadsheet();
/*active spreadsheet id*/
var SSTID=SpreadsheetApp.getActiveSpreadsheet().getId();
/*OR if you are writing a standalone script*/
/*var SSTID=1FhDxI7j0PFtQYF0gCsiAQauQ1QZ-nDTWNPydpQPD3GE*/
/* web request listeners */
/* pass request to handleResponse */
function doGet(e) {return handleResponse(e);}
function doPost(e) {return handleResponse(e);}
/* handle action request */
function handleResponse(e) {
 var lock = LockService.getPublicLock();
 lock.waitLock(30000); // wait 30 seconds before conceding defeat.
  try {
    var cmd = e.parameter.cmd;
    var output="test";
   
   
   
    //return ContentService.createTextOutput(JSON.stringify({"result": "success","data": output})).setMimeType(ContentService.MimeType.JSON);
    return output
  }
 catch (e) {/*if error return this*/return ContentService.createTextOutput(JSON.stringify({"result": "error","error": e})).setMimeType(ContentService.MimeType.JSON);}
 finally { /*release lock*/ lock.releaseLock();}
}
/*test function*/
function testFunction(){
  var e={parameter:{cmd:"test"}};
  Logger.log(handleResponse(e));
}

4. Test

Save.
Run testFunction().
Check Log

5. View execution transcript

It shows the series of code execution including system commands eg Lock and Log.

6. View Log.

It shows the Log output.
You can put the log anywhere in the code to get the app returns a value to the system.
This is what we should get when we run the script.

7. Publish As Web App

Before you publish, change the return statement so that the code returns the output in the form of Text Output instead of the regular javascript variable.
Refer screenshot below. Line no.26 is uncommented and line no.27 is commented.
Next, go to menu and select Publish/Deploy as web app...
Copy the link.

8. Test Web App.

Copy the link in step no. 7 and paste to web browser url box.
You should get the following.

.

No comments:

Post a Comment