Skip to content Skip to sidebar Skip to footer

Im New To Code, Why My Userform Not Redirecting To Same Link Or Not Auto Form Reset

Created userform by using HTML it is successfully returning data to google sheets but it is not reset the form after click submit. Can anyone help me please, I'm new to this code i

Solution 1:

Issue:

If I understand you correctly, you are setting some values to your spreadsheet via google.script.run.addNewItem(this);, and you want to reset the form fields after this is done.

Answer :

In order to do that, you should use withSuccessHandler(function) to execute a callback function if addNewItem executes successfully, and reset the form fields. An easy way to reset those fields is using HTMLFormElement.reset().

Therefore, your script tag could be something like this:

<script>document.querySelector("#myform").addEventListener("submit", 
  function(e) {
    e.preventDefault();
    google.script.run.withSuccessHandler(resetForm)
                     .addNewItem(this);
  });
  functionresetForm() {
    document.querySelector("#myform").reset();
  }
</script>

Other issues:

There are several other issues here. If you are using the exact code you provided, it's not possible that you are currently able to write data to the spreadsheet:

  • In the HTML you provided, the <script> tag is not getting closed (</script>).
  • In addNewItem, the parameter is called form_data, but then you are using data. Both variables should match, or you'll get an error.
  • You are using google.script.host.close(), which is used to close a dialog or sidebar, but has no place in a web app like yours.

Post a Comment for "Im New To Code, Why My Userform Not Redirecting To Same Link Or Not Auto Form Reset"