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?

12
  • 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 Nov 8, 2016 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 8, 2016 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 developers.google.com/apps-script/reference/html/html-service and developers.google.com/apps-script/reference/html/…
    – Karl_S
    Nov 8, 2016 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? Nov 8, 2016 at 17:21
  • 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
    – Alan Wells
    Nov 8, 2016 at 17:47

1 Answer 1

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);
}
3
  • 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). Nov 9, 2016 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 Nov 10, 2016 at 17:11
  • Get the doPost() script to return an HTMLService page that contains an redirect. Or handle the original post with jQuery.ajax() (api.jquery.com/jQuery.ajax). Example given here: stackoverflow.com/a/9933872/3520117 If you feel I've answered your question please mark it as the right answer. Thanks.
    – h0dges
    Nov 10, 2016 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.