0

I have a Google Form with a text field inside.

I want to evaluate the content typed by the end-user before doing the post, but after a few days of searching I have not been able to do it.

In the code.gs, I am able to access the text field and grab the properties of it. If you see the sdi.html, this triggers the call of the function doEval().

My issue is that I don’t know how to add a handler in the Google Form itself so that I can check that the content of the text field is something acceptable to my app. For example: validate it if this is a number and if this is true then submit the form, otherwise cancel submit process and show an error message.

On legacy Google forms there were ways to create handlers, but now everything works under HtmlService approach, and I don’t see examples of how to do it.

Here is my code:

code.gs:

function doGet()
{
   return HtmlService.createHtmlOutputFromFile('sdi')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function doEval()
{
    var frm = FormApp.getActiveForm();
    var items = frm.getItems();
    var text= items[1].asTextItem();
    text.setTitle("I can access the field from here!");
}

sdi.html:

<!DOCTYPE html>
<html>
<head>
<script>
  function onSuccess(isOk)
  {
  }

  google.script.run.withSuccessHandler(onSuccess)
      .doEval();
</script>
</head>
<body>
</body>
</html>
3
  • Is there a button in the form to submit it? You can use typeof to test for the data type of the value. Right now, google.script.run. is not inside of a function. If you put it inside of a function, then you can either run google.script.run. or not.
    – Alan Wells
    Mar 29, 2016 at 1:01
  • what you show is not a google form. it's a webapp.
    – Zig Mandel
    Mar 29, 2016 at 2:52
  • Hi, yes you are right, I elaborated wrongly initially, it is form, but I never show it. Pls check the reply I just did to Spencer Easton on where I am explaining better. Cheers.
    – Nish Class
    Mar 29, 2016 at 22:34

1 Answer 1

0

You can validate the input of a user on a Goolge From using conditionals. Here is a very nice reference: https://support.google.com/docs/answer/3378864?hl=en

There is no way to run a script inside of the live form. You can only respond to form submissions.

I'm confused by your code as it looks like you are trying to use a webapp to validate the form. This isn't won't work. Use the script editor attached to your Form. There you can write the code you need to handle form submissions. Check out the Forms scripting quick start:

https://developers.google.com/apps-script/quickstart/forms

1
  • Thx on replying. I am using new Google forms, on which conditionals are not available yet. Secondly, I am using script editor, this is where I wrote 'code.gs', and I have attached to my Google Form. Actually the root of my question is, how (by using Script Editor) do I access the form before submit?, in order to validate either the data entered by end-user is valid or not, then I can take action on submit or not submit. Please advance.
    – Nish Class
    Mar 29, 2016 at 22:31

Your Answer

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

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