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":" at 3Web:13 (handleResponse) at 3Web:45 (testHandleResponse) "}} [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
https://script.google.com/macros/s/AKfycbz5aWadwvarEH0x429UE6RiF5YQkAoxTGS82BawSSbaIU4b9qJ4/exec
{"status":"success","content":"hello!"} |
Note: the server regards basic URL call as a call with valid empty parameter.
https://script.google.com/macros/s
visit link download
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.