Search code examples
javascripthtmlgoogle-apps-scriptgoogle-sheetsphonegap

Web App HTML, JS + Google Apps Script: How to go back to my app after return in GAS?


I'm building a web app using HTML, CSS and JavaScript and using Google Spreadsheet as a database. I'm planning to use PhoneGap to turn my web app into a real app.

I was able to read from my Google Sheet, using the Google Visualization API, now I'm working about the writing options: I used a function set with Google Apps Script, inside the Google Spreadsheet and it works correctly.

This is how my app works and what I need:

  1. I query my first Google Spreadsheet (Database)
  2. If I cannot find what I need, I can make a request
  3. I send the request: the action of the form send my data to the Google Apps Script code
  4. The GAS writes my data in a second Google Spreadsheet (Register)
  5. The GAS should send me back to my page
  6. The page sends a feedback on screen

As you can understand by the bold point, I'm not able to go back to my webpages... I don't know if there's any method or function to do this. The only thing I found was using ContentServiceto make the page "write another form and send it using JavaScript", but I don't think is the best solution...

I'll post my code down here... hoping someone can help me

function doPost(e){
 var sheet = SpreadsheetApp.openById("myID").getSheetByName('SheetName');
 var column = 1; 
 var colArray = sheet.getRange(2, column, sheet.getLastRow()).getValues();
 var maxi = Math.max.apply(Math, colArray);


  var id = maxi+1;
  var name = e.parameter['name'];
  var surname = e.parameter['surname'];
  var serial = e.parameter['serial'];
  var eMail = e.parameter['mail'];
  var text = e.parameter['text'];
  var area = e.parameter['area'];
  var date = new Date();
  var ans = ""
  var flag = "Work In Progress";

  var vals = [id, date, name, surname, serial, eMail, area, text, ans, flag];

  var sheetObj = sheet.appendRow(vals);

  // return ContentService.createTextOutput(someOutput);

On the return line I should go back to my web app/app but I can't find a way to do it. Someone can help me or give me some advice?

Thanks a lot for your help!

S.


Solution

  • This is the solution to your problem.

    Instead of using return ContentService.createTextOutput(someOutput);

    use HtmlService to return to your form

    return HtmlService.createHtmlOutputFromFile('redirect').setSandboxMode(HtmlService.SandboxMode.IFRAME);
    

    And your HTML file should look like this,

    Update: redirect.html should be in the same project as that of your app script file.

    redirect.html

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <title>This Is A Sample Title</title>
    <script>
    function test()
    {
        window.location.href = "www.hostname.com";
    }
    </script>
    </head>
    <body onload="test()">
    Login
    </body>
    </html>
    

    See if this works for you.