Labels

Learn the powerful enterprise adaptable database:

Getting Started With ADABAS & Natural

Monday, February 1, 2016

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){
 // get the query "greeting" parameter and set a default to "Hello"
 var greeting = e.parameter.greeting || "Hello";
 // get the query "name" parameter and set a default to "World!"
 var name = e.parameter.name || "World";
 // create the PDF
 var pdf = makePDF(greeting,name)
 // save the PDF to Drive
 var driveFile = DriveApp.createFile(pdf).setName("greetings.pdf");
 // tell the user how to access it
 var fileURL = driveFile.getUrl();
 var fileName = driveFile.getName();
 var HTMLOutput = HtmlService.createHtmlOutput("<p>Your made a PDF.</p>"
 + "<p> You can download it here: "
 + '<a target="blank" href="' + fileURL + '">' + fileName + '</a></p>');
 return HTMLOutput
}
function makePDF(greeting, name){
 // use an externally hosted stylesheet
 var style = '<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">';
 // 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>Here is your PDF.</p>";
 var PDFOutput = HtmlService.createHtmlOutput('<div class="container">');
 PDFOutput.append(style);
 PDFOutput.append(heading.evaluate().getContent());
 PDFOutput.append(content);
 PDFOutput.append('</div>');
 var blob = Utilities.newBlob(PDFOutput.getContent(), "text/html", "text.html");
 var pdf = blob.getAs("application/pdf");
 return pdf
}

REFERENCES:

https://developers.google.com/apps-script/guides/web 

.

No comments:

Post a Comment