Learn the powerful enterprise adaptable database:

Getting Started With ADABAS & Natural

Sunday, February 1, 2015

201 Apps Script Project Startup


.

201 Apps Script Project Startup

1) Create Apps Script

Give a name e.g 201

2) Create Script Files

Create separate files to contains groups of related functions.
Later on, you can easily copy the codes to other projects and reuse them.

3) Add Web App Codes

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 {
    Logger.log(e)
    var cmd = e.parameter.cmd;
   
    if (cmd == "appsetup") {
       return "appsetup"
      //return taskMan("appsetup", e);
    } else{ /*default case*/
       
       strOutput={"status": "success","content": "hello!"}
       Logger.log(JSON.stringify(strOutput))
       
       return ContentService.createTextOutput(JSON.stringify(strOutput))
       .setMimeType(ContentService.MimeType.JSON);
       
    }
   
  } catch (e) { /*if error return this*/

       strOutput={"status": "error","content": e}
       Logger.log(JSON.stringify(strOutput))
       
       return ContentService.createTextOutput(JSON.stringify(strOutput))
       .setMimeType(ContentService.MimeType.JSON);
  } finally { /*release lock*/
    lock.releaseLock();
  }
}
function testHandleResponse(){
   var e0={} //invalid empty parameter
   var e1={parameter:{}} //valid empty parameter
   var e2={parameter:{cmd:"appsetup"}} //valid parameter
   Logger.log(handleResponse(e2))
}

4) Observe testHandleResponse() Outcome

no
testHandleResponse parameter
Outcome
1
e0
[18-01-07 10:31:11:970 HKT] {}
[18-01-07 10:31:11:971 HKT] {"status":"error","content":{"message":"Cannot read property \"cmd\" from undefined.","name":"TypeError","fileName":"3Web","lineNumber":13,"stack":"\tat 3Web:13 (handleResponse)\n\tat 3Web:45 (testHandleResponse)\n"}}
[18-01-07 10:31:12:024 HKT] TextOutput
2
e1
[18-01-07 10:31:49:208 HKT] {parameter={}}
[18-01-07 10:31:49:209 HKT] {"status":"success","content":"hello!"}
[18-01-07 10:31:49:268 HKT] TextOutput
3
e2
[18-01-07 10:32:07:906 HKT] {parameter={cmd=appsetup}}
[18-01-07 10:32:07:959 HKT] appsetup

5) Publish As Web App

6) Observe URL Call Outcome

{"status":"success","content":"hello!"}
Note: the server regards basic URL call as a call with valid empty parameter.
{"status":"success","content":"hello!"}
Note: the server switches basic URL call with non-existent parameter to default case.
The script completed but the returned value is not a supported return type.

Note: the returning value is in the form of an unsupported type.
.

201502

No comments:

Post a Comment