.
204 Apps Script Web Run Script With User Object
1) Create Project Script
Project name: 204
Script files:
1Util.gs
2TaskMan.gs
3Web.gs
web.html
2) Edit web.html
<!DOCTYPE html>
<html> <head> <base target="_top"> </head> <body> <h1>Home</h1> <input type="button" value="Not Clicked" onclick="google.script.run .withSuccessHandler(updateButton) .withUserObject(this) .taskMan('testscriptrun',{parameter: {testcodes:{}}})" /> <input type="button" value="Not Clicked" onclick="google.script.run .withSuccessHandler(updateButton) .withUserObject(this) .taskMan('testscriptrun',{parameter: {testcodes:{}}})" />
<script>
function updateButton(email, button) { button.value = 'Clicked by ' + email; } </script> </body> </html> |
3) Edit Util.gs
/*getHTML*/
function getHTML(file) {
if (!file) {
var outputHtml = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
outputHtml.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
outputHtml.addMetaTag('viewport', 'width=device-width, initial-scale=1');
return outputHtml;
};
var outputHtmlFile = HtmlService.createHtmlOutputFromFile(file); /*need to create html file*/
outputHtmlFile.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
outputHtmlFile.addMetaTag('viewport', 'width=device-width, initial-scale=1');
return outputHtmlFile;
}
/*createHTML*/
function createHTML(htmlcodes) {
if (!htmlcodes) {
var outputHtml = HtmlService.createHtmlOutput('<b>Hello, world!</b>');
outputHtml.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
outputHtml.addMetaTag('viewport', 'width=device-width, initial-scale=1');
return outputHtml;
};
var outputHtml = HtmlService.createHtmlOutput(htmlcodes);
outputHtml.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
outputHtml.addMetaTag('viewport', 'width=device-width, initial-scale=1');
return outputHtml;
}
/*createJSON*/
function createJSON(data) {
if (!data) {
data = []
};
var outputJson = ContentService.createTextOutput(JSON.stringify({
"status": "success",
"content": data
})).setMimeType(ContentService.MimeType.JSON);
return outputJson;
}
|
4) Edit TaskMan.gs
/*taskManager*/
function taskMan(cmd, e) {
switch (cmd) {
case "gethome":
return getHTML('home.html')
break;
case "testhtml":
var htmlcodes;
if (!e.parameter.testcodes) {
htmlcodes = '<b>Hello, test!</b>';
} else {
htmlcodes = e.parameter.testcodes;
}
return createHTML(htmlcodes)
break;
case "testjson":
var jsoncodes;
if (!e.parameter.testcodes) {
jsoncodes = {
test: 'Hello, test'
};
} else {
jsoncodes = e.parameter.testcodes;
}
return createJSON(jsoncodes)
break;
case "testscriptrun":
return "Test Script Run"
break;
}
}
|
5) Edit WebApp.gs
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 if (cmd == "gethome") {
return taskMan("gethome",e);
}
else if (cmd == "testhtml") {
return taskMan("testhtml",e);
}
else if (cmd == "testjson") {
return taskMan("testjson",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))
}
|
6) Publish and Test
URL:
.
201502
No comments:
Post a Comment