Protected
Protected

Reputation: 362

Reject Google Forms submission or remove response

I'm using the current, "new" version of Google Forms. I have a form, Form A, and the associated script Script A. That script contains a function, onFormSubmit, associated with Form A's form submit trigger. The function receives one argument, an event, which contains the Form and the submitted FormResponse in the fields "source" and "response" respectively.

In the body of this function, how can I prevent the event/reject the form submission?

Alternatively, if this is not possible, how can I quietly prevent the FormResponse from being stored, or quietly remove it from the list of responses?

I see there is a Form.deleteAllResponses method. Do I have to delete all responses and then add all responses back again, except for the current one? Or is there a better way?

Upvotes: 3

Views: 3757

Answers (2)

zimady
zimady

Reputation: 137

The way I have handled this is by having a second sheet (tab within the same Google Sheet) into which Form responses are copied. The first thing that happens in the onFormSubmit() function is that the newest row in the responses sheet is copied to the duplicate sheet. You could implement a selection statement that chooses whether to make the copy or not depending on your criteria.

This means that there is always a copy of the raw responses from the form (important for auditing purposes) but also a means for correcting/modifying responses if errors were made by the submitter.

In case it is useful, this is my function that does the copy (note that my settings object is abstracted elsewhere but hopefully there is enough to make clear what is going on).

/**
 * Copies form submissions from the responses sheet to another sheet.
 *
 * @param {event object} e the event object received from a form submit trigger
 * @param {Settings} settings an object containing the settings this function expects to use
 * @return {integer} the position of the new row in the destination sheet
 */
function copyFormSubmissionToSheet(e, settings) {
  var sourceSheet = SpreadsheetApp.getActive().getSheetByName(settings.nameOfFormResponsesSheet);
  var destinationSheet = SpreadsheetApp.getActive().getSheetByName(settings.name OfApprovalsSheet);

  var newRow = e.range["rowStart"];
  var columnCount = sourceSheet.getLastColumn();

  var newResponseRange = sourceSheet.getRange(newRow, 1, 1, columnCount);

  var newResponseDestinationRange = destinationSheet.getRange(destinationSheet.getLastRow()+1, 1, 1, columnCount);

  newResponseRange.copyTo(newResponseDestinationRange);

  var newDataSheetRow = destinationSheet.getLastRow();

  return newDataSheetRow;
}

Upvotes: 2

Mr.Rebot
Mr.Rebot

Reputation: 6791

Try experimenting with the event trigger and use:

setAcceptingResponses(enabled)

Sets whether the form is currently accepting responses. The default for new forms is true.

Here is a code sample from a related SO post:

function onFormSubmit(){
  var af = FormApp.getActiveForm();
  var defaultClosedFor = af.getCustomClosedFormMessage();
  af.setCustomClosedFormMessage("The form is currently processing a submission, please refresh the page.");
  af.setAcceptingResponses(false);


<put your script stuff here>


   af.setAcceptingResponses(true);
  af.setCustomClosedFormMessage(defaultClosedFor);
}

Hope this helps.

Upvotes: 2

Related Questions