1000 Apps Script As Web App
.
1000 Apps Script As Web App
1. Goto menu and select Publish/Deploy as web app...
2. Click Deploy.
3. Copy the URL and access it via Web Browser.
1) Return Plain Text
function doGet(){ textOutput = ContentService.createTextOutput("Hello World! Welcome to the web app.") return textOutput } |
2) Return JSON
function doGet(){ var appData = { "heading": "Hello World!", "body": "Welcome to the web app." }; var JSONString = JSON.stringify(appData); var JSONOutput = ContentService.createTextOutput(JSONString); JSONOutput.setMimeType(ContentService.MimeType.JSON); return JSONOutput } |
3) Return HTML
function doGet(){ var HTMLString = "<style> h1,p {font-family: Helvitica, Arial}</style>" + "<h1>Hello World!</h1>" + "<p>Welcome to the Web App"; HTMLOutput = HtmlService.createHtmlOutput(HTMLString); return HTMLOutput } |
4) Return Dynamic HTML
function doGet(e){ // use an externally hosted stylesheet var style = <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">; // get the query "greeting" parameter and set the default to "Hello" var greeting = e.parameter.greeting || "Hello"; // get the query "name" parameter and set the default to "World!" var name = e.parameter.name || "World"; // create and use a template var heading = HtmlService.createTemplate(<h1><?= greeting ?> <?= name ?>!</h1>) // set the template variables heading.greeting = greeting; heading.name = name; var content = "<p>Welcome to the web app.</p>"; var HTMLOutput = HtmlService.createHtmlOutput(); HTMLOutput.append(style); HTMLOutput.append(heading.evaluate().getContent()); HTMLOutput.append(content); return HTMLOutput } |
5) doPost processing
function doPost(e){ // Get the "text" query string value // eg. &text=Your%20Name var name = e.parameter.text // Get the Chuck Norris Joke var chuckQuote = postChuckNorris(name); // Return plain text Output return ContentService.createTextOutput(chuckQuote); } // Make a call to the Chuck Norris joke API // parse the JSON and return only the joke function postChuckNorris(name){ var queryString = makeQueryString(name) var chuckData = UrlFetchApp.fetch("http://api.icndb.com/jokes/random/" + queryString); var chuckJSON = JSON.parse(chuckData.getContentText()); var chuckQuote = chuckJSON.value.joke; return chuckQuote } // Helper function to assemble the query string for // calling the Chuck Norris API from a given name function makeQueryString(name){ var query = "" if (name !== undefined){ var names = name.split(" "); query = "?firstName=" + names[0]; if (names[1] !== undefined){ query += &lastName= + names[1]; } } return query } |
6) Create and Save PDF Document
function doGet(e){ link download
Subscribe to:
Post Comments (Atom)
|
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.