0

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?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.