Gamaray
Gamaray

Reputation: 68

Apps script: Get html form values inputted into a web app

I am working on a script that requires html form data to be inputted to a web app, I then want to be able to process that form data and put it on a google sheet. The thing is I can't seem to do that. Here is my code:

Index.html

<form id="Form" onsubmit="event.preventDefault(); 
google.script.run.getData(this)">
  <input type="text" name="firstname">
  <input type="submit">
</form>

Code.gs

function getData(data) {
    var sheet= SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Logs');
    sheet.getCurrentCell().setValue(data.name);
}

What am I doing wrong?

Upvotes: 0

Views: 869

Answers (2)

Kristkun
Kristkun

Reputation: 5953

The issue in your code is that your your data doesn't have a name member/key. Hence, you are writing a null value in your current cell which will result to an empty cell value.

Sample Code:

function getData(data) {
  Logger.log(data)
  Logger.log(data.name)
  var sheet= SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Logs');
  sheet.getCurrentCell().setValue(data.firstname);
}

Execution Logs:

enter image description here

Output:

enter image description here

  • In this example, my current active cell is in A1

Upvotes: 1

Neven Subotic
Neven Subotic

Reputation: 1429

Check the documentation for a practical example. In your case, you have a form which you submit. In that case the form does not know which is the active cell. Therefore you should replace getCurrentCell()with getRange() or appendRow()

Reference

  • appendRow - You could do this appendRow([formObject.name])
  • getRange - Paired with getLastRow(), you can then set the value in it

Upvotes: 0

Related Questions