0

I have been reading and watching many tutorials online but I could not find something that I can initially load some kind of dialog box or login page with a single input text so then I can process that using apps script.

onFormOpen() is not triggered on the user who completes the form.

On my onSubmitForm() function I have the following code:

function onFormSubmit(e){
  var formResponses = FormApp.getActiveForm().getResponses();
  var formResponse = formResponses[formResponses.length-1];
  var itemResponses = formResponse.getItemResponses();
  for (var j = 0; j < itemResponses.length; j++) {
    var itemResponse = itemResponses[j];
    Logger.log('Last response to the question "%s" was "%s"',
               itemResponse.getItem().getTitle(),
               itemResponse.getResponse())
  }
}

Is there any way I can prevent from here the form being submitted with some kind of logic condition, for ex: execute the form submission only if certain field matches some condition?

Rubén
  • 34,714
  • 9
  • 70
  • 166
pranvera hoti
  • 107
  • 3
  • 15
  • 1
    It is not possible to prevent a Form submission from saving data to the Form. And you can't delete a Form submission. I'm sure that it's designed that way on purpose. – Alan Wells Apr 06 '17 at 23:25
  • 1
    You will need to create your own custom Web App with an HTML form in order to do what you want. You can create a Web App with Apps Script, which is basically the same as a website, but you can't get a domain name. – Alan Wells Apr 07 '17 at 00:36
  • @SandyGood thanks for your answers, by creating custom Web App with Apps Script, I dont understand what is the benefit of doing this way and use apps scripts, where I could do it in pure HTML AND JS without need of using google apps script. is there anyway that I can include the google form when using Web App and manipulate with it when the form is being viewed by someone? I mean do I have more control over the form in Web App way! – pranvera hoti Apr 07 '17 at 07:46
  • 1
    With a Web App, you must use HTML, CSS and client side JavaScript. You can do whatever you want. [Apps Script documentation](https://developers.google.com/apps-script/guides/html/) Here is an Apps Script file that is a complete example of a Web App input form: [Link to my shared file](https://drive.google.com/open?id=125dG42eB9lM4SPq64p0dpR2CBH4ohfHiqu9TvFNM8s4Ra7pt-7kHXoTM) – Alan Wells Apr 07 '17 at 11:48

1 Answers1

1

Is there any way I can prevent from here the form being submitted with some kind of logic condition, for ex: execute the form submission only if certain field matches some condition?

You could use form rules:

You can create rules that people have to follow when they fill out your form. For example, if you ask for email addresses, you can make sure that people can only submit properly formatted email addresses.

Regarding onFormOpen() there isn't a built-in trigger named that way. You could use that as name for an on open installable trigger but it only runs when the form editor is opened, not when the form view is opened.

Regarding adding a custom logging, if you use a G Suite account you could limit the access to users from the same domain as your account but you could not limit access further this.

A hacky alternative is to take the source code from the form view and "adapt" it to be served by a Google Apps Script or host it on your own web server. This implies that you will inject your custom form rule/data validation. The easier way is by using the old Google Forms and copy/paste the source code of the form but it's possible to use UrlFetch.

Another alternative is to use the HtmlService Service from Google Apps Script to create and serve your form which practically will give you complete control of your form behavior. See Forms - HTML Service: Communicate with Server Functions

Related:

On https://webapps.stackexchange.com

Community
  • 1
  • 1
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Form Rules does not allow me to call a apps script function, do they? for example in my case I need to check if certain input is valid or not, by calling a javascript function that has an algorithm for checking checkdigit, Luhn Algorithm. – pranvera hoti Apr 07 '17 at 07:50
  • That's right. Form rules have a built in "rule builder" and optionally allow us to use regular expression but do no support JavaScript. – Rubén Apr 07 '17 at 08:16
  • So how would u suggest me alternatively how do I achieve such that I can validate a google form function using a javascript function, is that possible? I am looking for a straight answers yes or not! If yes could u propose a way of doing it? – pranvera hoti Apr 07 '17 at 08:17