5

I know how to write a simple app script that (once published) provide a simple form (name and message) that push values in a Google Spreadsheet.

My question is: how can I run the script from my site? Can I build an html form and when I submit the form writes in a spreadsheet?

Is there a way to "create" a link with values that must be pushed in the spreadsheet?

Where can I find examples?

Thanks

EDIT

GS CODE

var logSheetId = "[SHEETID]"; // Drive ID of log spreadsheet

function doGet(e){
var nome;
var messaggio;

nome = e.parameter.stringaNome;
messaggio=e.parameter.stringaMessaggio;
scriviSpreadsheet(nome,messaggio);
}

function scriviSpreadsheet(nome, messaggio) {
try {

// Open the log and record the new file name, URL and name from form
var ss = SpreadsheetApp.openById(logSheetId);
var sheet = ss.getSheets()[0];
sheet.appendRow([nome, messaggio]);

// Return the new file Drive URL so it can be put in the web app output
//return file.getUrl();
} catch (error) {
return error.toString();
}
}

HTML (external site)

<form id="myForm">
<input type="text" id="nome" name="nome" placeholder="Il tuo nome"/>
<input type="text" id="messaggio" name="messaggio"/>
<input type="button" value="Submit"
  onclick="scrivi()" />
</form>

<script>
function scrivi(){
  var nome= document.getElementById("nome").value;
  var messaggio = document.getElementById("messaggio").value;

  var location = "https://script.google.com/macros/s/[MYSCRIPTID]/exec?stringaNome=" + nome + "&stringaMessaggio=" + messaggio;
  window.location = location;

}

function onFailure(error) {
    alert(error.message);
}
</script>

I know it's not the best code ever but it works.

How can I avoid that any page open when I click the button?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Andrea Volontè
  • 85
  • 1
  • 3
  • 10
  • You could write an HTML form and embed it into the site, you could use a google form and embed it via iframe or you can make a google site and embed GAS directly – Robin Gertenbach Nov 08 '16 at 16:15
  • 2
    If your web page can make an HTTPS Request, then you can trigger an Apps Script function to run with a GET or POST request to the published URL of the Apps Script project. For example, If your web page can use JavaScript, then you can make an AJAX request. If your web page can run server side code, then your web site server side code can make a GET or POST request to the Apps Script URL. To handle a GET or POST request, Apps Script has two reserved function names: `doGet()` and `doPost()` They both monitor for the event of a request made to the published URL. – Alan Wells Nov 08 '16 at 16:26
  • Publish the script as a web app. Then you get a link you can use on its own, in an iframe, or direcetly into a Google Site page. For a non-Google Site, in your doGet(e) function you would call `HtmlService.createTemplateFromFile('Index').setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);` or `HtmlService.createHtmlOutput('Index').setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);` or one of the other calls. See https://developers.google.com/apps-script/reference/html/html-service and https://developers.google.com/apps-script/reference/html/x-frame-options-mode – Karl_S Nov 08 '16 at 16:29
  • @SandyGood I'm looking doGet and doPost documentation but it looks hard to me... can you provide a simple example? (not necessarly exactly the same function I need) Have I to format the url? – Andrea Volontè Nov 08 '16 at 17:21
  • @RobinGertenbach I'm trying to not use Google sites or Iframe. Thanks anyway! – Andrea Volontè Nov 08 '16 at 17:23
  • 2
    If you have ever created a Web App with Apps Script, then you have used the `doGet()` function, and published it. In the "Publish" menu, click "Deploy as web app", and a dialog box will appear. At the top, there is a field with the published URL. That is the URL that you use. See the following for more info: [Link to Stack Overflow answer](http://stackoverflow.com/a/30127428/2946873) – Alan Wells Nov 08 '16 at 17:47
  • @SandyGood now I'm able to use url in the correct way. Last question: when I click the button the browser load the GAS page to confirm that the script has been executed. How can I avoid this? I want that when I send my form the page don't change. – Andrea Volontè Nov 09 '16 at 09:18
  • Post the HTML for your button, and the code the runs in your question. – Alan Wells Nov 09 '16 at 13:42
  • @SandyGood I've edited my question with some code – Andrea Volontè Nov 09 '16 at 19:53
  • You should use Content Service to return content for anything that is not a Web App. `doGet()` is mostly used for serving HTML in a web page to the browser. I'm still not sure what you want for the end result. Do you want an entire new web page loaded? Do you want the web page to stay in the browser window, but get some different content displayed? Do you want the Web App to display in a different browser tab? – Alan Wells Nov 09 '16 at 21:28
  • @SandyGood my goal is: in www.mysite.com/form I have a form, when I submit the form data must be processed by GAS without opening any google page and then redirect on my webpage. – Andrea Volontè Nov 10 '16 at 08:46
  • I would use the AJAX request as h0dges mentioned, and avoid using the `action` and `method` attributes in the form tag. The form tag in HTML has "built-in" behavior that can interfere with what you want, as you have experienced. Unless you are using the features associated with a form tag, it's not needed. You can format the layout in a DIV tag. I'd also get rid of the submit button, because that triggers the default form behavior that you don't want in this case. Just use a regular button that triggers a function with an AJAX request in it. – Alan Wells Nov 10 '16 at 20:40

1 Answers1

8

EDIT: Tested and works. Don't forget to deploy as web app, give the form access to post to the script, and ensure the script has access to the target sheet by running the script manually once.

form.html:

<form action="https://script.google.com/macros/s/[SCRIPT ID]/exec" method="post">
  Input One:<br>
  <input type="text" name="inputOne"><br>
  Input Two:<br>
  <input type="text" name="inputTwo"><br>
  Input Three:<br>
  <input type="text" name="inputThree"><br>
  Input Four:<br>
  <input type="text" name="inputFour"><br>
  Input Five:<br>
  <input type="text" name="inputFive"><br>
  <input type="submit" value="Submit">
</form>

Code.gs:

function doPost(e) {

  var ss = SpreadsheetApp.openById("SHEET ID");
  var sheet = ss.getSheetByName("Sheet1");
  var sheet_counters = ss.getSheetByName("Counters");

  var id = sheet_counters.getRange("A2").getValue()+1;
  var timeZone = ss.getSpreadsheetTimeZone();
  var timestamp = Utilities.formatDate(new Date(), timeZone, "dd/MM/yyyy HH:mm:ss");

  sheet.appendRow([
    id,
    timestamp,
    e.parameter.inputOne,
    e.parameter.inputTwo,
    e.parameter.inputThree,
    e.parameter.inputFour,
    e.parameter.inputFive]);

  sheet_counters.getRange("A2").setValue(id);
}
h0dges
  • 663
  • 7
  • 15
  • I tried your example, when I try to run says that can't find doGet function. I have renamed the function "doPost" in "doGet" and when I run says that can't read the property "contents" from undefined (line 13). – Andrea Volontè Nov 09 '16 at 19:56
  • now the script works! What about if I dont want that the confirm page appear? I want that when I submit the form the page redirect on my home (or other page hosted on my site). Thanks again – Andrea Volontè Nov 10 '16 at 17:11
  • Get the doPost() script to return an HTMLService page that contains an redirect. Or handle the original post with jQuery.ajax() (http://api.jquery.com/jQuery.ajax/). Example given here: http://stackoverflow.com/a/9933872/3520117 If you feel I've answered your question please mark it as the right answer. Thanks. – h0dges Nov 10 '16 at 18:59