How can I process a form using Google Apps Script?

user4061059

I am a total noob when it comes to GAS, but I want to pass a form to a local JS function (to validate the data), which then calls a Google function (to add it to a spreadsheet). Problem is: I can't even get the values out of the form!

My code is like this at the moment:

index.html:

<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">

<div>
  <form id="register" action="javascsript:void(0)" onsubmit="validateForm(this)">
    Email: <input type="text" name="email" placeholder="[email protected]" /><br/>
    <p id="emailtext"></p><br/>
    Smartschool URL: <input type="text" name="url" /><br/>
    <p id="urltext"></p><br/>
    <input type="submit" value="Submit" />
  </form>
</div>

<?!= include('Javascript'); ?>

Javascript.html:

<script>
  function validateForm(form) {
      // THIS IS NEVER POPPING UP
      alert(form.email);
      return false;
  }
</script>

GoogleCode.gs:

function doGet(e) {
  return HtmlService.createTemplateFromFile('Page').evaluate();
}

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename)
      .getContent();
}
Alan Wells

I added a console.log statement to the JavaScript, and looked at the log in my Google Chrome browsers console, and it shows that a form object is getting passed.

I added this line:

console.log('form: ' + form);

To your Javascript file:

<script>
  function validateForm(form) {
    console.log('form: ' + form);
      // THIS IS NEVER POPPING UP
      alert(form.email);
      return false;
  }
</script>

The browser console prints:

form: [domado object HTMLFormElement FORM]

So, the form object is getting passed. You can enumerate all the properties in the Form object to see what is in there, and available to retrieve.

for (var thePropurtees in form) {
  console.log('thePropurtees: ' + thePropurtees);
};

You'll get a real long list of everything in the Form object, and you'll notice that email is not in the list. What is in the list is an elements property, that turns out to be another object inside the form object. There is an elements object inside of the form object.

If I enumerate the form elements:

for (var thePropurtees in form.elements) {
  console.log('thePropurtees: ' + thePropurtees);
};

I get this:

thePropurtees: 0
thePropurtees: 1
thePropurtees: 2
thePropurtees: item
thePropurtees: email
thePropurtees: url
thePropurtees: namedItem

So, your email data must be in a sub object.

I was able to get the value out of the email input field with this:

console.log('the email value: ' + form.elements.email.value);

There are three levels of objects you need to access, before you can get at the values.

1) Form object
2) Elements object
3) Input object (email)

Your alert would need to be like this:

alert(form.elements.email.value);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I authorize Google Speech-to-text from Google Apps script?

분류에서Dev

How do I slice strings in Google Apps Script like in Python?

분류에서Dev

How to get owners of libraries using Google Apps Script

분류에서Dev

Are there restrictions on using UrlFetchApp with Triggers in Google Apps Script

분류에서Dev

How can I determine if a process is a system process?

분류에서Dev

How to render Google Pay button in Google Apps Script

분류에서Dev

How can I prevent from script from dying if a process ID doesn't exist?

분류에서Dev

Post a tweet to my feed using Google Apps Script

분류에서Dev

How can I uninstall Django apps?

분류에서Dev

How to add newDataValidation() code block into my Google apps script

분류에서Dev

Need help updating dropdowns in HTML Web Apps using Google Apps Script

분류에서Dev

Google-apps-script SyntaxError

분류에서Dev

Google Apps Script 호출

분류에서Dev

I can't get the html form to run a php script

분류에서Dev

How can i echo a variable posted from a textbox form using php?

분류에서Dev

how can I delete apps that aren't installed in dash?

분류에서Dev

How can I run a script on resolution change?

분류에서Dev

Adjust file created date value for date between evaluation using Google Apps Script

분류에서Dev

Apps Script의 Google Docs API

분류에서Dev

Google 양식 + Apps Script onEdit

분류에서Dev

Google Spreedsheets Apps Script-On Cell Focus

분류에서Dev

Google Apps Script 잘못된 값

분류에서Dev

Google Apps Script와 Firebase 통신

분류에서Dev

How can I index a client's site (built using WordPress) on google via Search Engine Console?

분류에서Dev

How can I upload a new revision to an specific file in Google Drive using the REST API?

분류에서Dev

How Can I Create A Dump File of a Running Process in Linux?

분류에서Dev

How can I process a table without headers in SpecFlow?

분류에서Dev

How to make sure that the process is still running so that I can kill it?

분류에서Dev

How can I tell what process is unmounting a file system?

Related 관련 기사

  1. 1

    How can I authorize Google Speech-to-text from Google Apps script?

  2. 2

    How do I slice strings in Google Apps Script like in Python?

  3. 3

    How to get owners of libraries using Google Apps Script

  4. 4

    Are there restrictions on using UrlFetchApp with Triggers in Google Apps Script

  5. 5

    How can I determine if a process is a system process?

  6. 6

    How to render Google Pay button in Google Apps Script

  7. 7

    How can I prevent from script from dying if a process ID doesn't exist?

  8. 8

    Post a tweet to my feed using Google Apps Script

  9. 9

    How can I uninstall Django apps?

  10. 10

    How to add newDataValidation() code block into my Google apps script

  11. 11

    Need help updating dropdowns in HTML Web Apps using Google Apps Script

  12. 12

    Google-apps-script SyntaxError

  13. 13

    Google Apps Script 호출

  14. 14

    I can't get the html form to run a php script

  15. 15

    How can i echo a variable posted from a textbox form using php?

  16. 16

    how can I delete apps that aren't installed in dash?

  17. 17

    How can I run a script on resolution change?

  18. 18

    Adjust file created date value for date between evaluation using Google Apps Script

  19. 19

    Apps Script의 Google Docs API

  20. 20

    Google 양식 + Apps Script onEdit

  21. 21

    Google Spreedsheets Apps Script-On Cell Focus

  22. 22

    Google Apps Script 잘못된 값

  23. 23

    Google Apps Script와 Firebase 통신

  24. 24

    How can I index a client's site (built using WordPress) on google via Search Engine Console?

  25. 25

    How can I upload a new revision to an specific file in Google Drive using the REST API?

  26. 26

    How Can I Create A Dump File of a Running Process in Linux?

  27. 27

    How can I process a table without headers in SpecFlow?

  28. 28

    How to make sure that the process is still running so that I can kill it?

  29. 29

    How can I tell what process is unmounting a file system?

뜨겁다태그

보관