Skip to content

Instantly share code, notes, and snippets.

@coccoinomane
Last active May 24, 2018 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save coccoinomane/c578f49aa5f6f28f76a6a8678e20c423 to your computer and use it in GitHub Desktop.
Save coccoinomane/c578f49aa5f6f28f76a6a8678e20c423 to your computer and use it in GitHub Desktop.
/**
* Make sure that Google Forms are stored in Google Spreadsheet
* as plain text; among other things, this script solves the problem
* whereby leading zeros are stripped from Google Form answers.
*
* This is a Google Script and it needs to be attached to a Google
* Spreadsheet:
* 1) Open the Google Spreadsheet linked to the Google Form, and
* go to Tools -> Script editor.
* 2) Paste the content of this file in the editor and save.
* 3) Attach this function to the onFormSubmit trigger using the
* "Edit -> Current project's triggers" menu.
*
* Big thanks to Bjorn Behrendt who inspired this script:
* https://groups.google.com/forum/#!topic/appsusergroup/IDNoGfZcieQ
*/
function onFormSubmit_ToString(e){
/* Form information; note that e.source is a SpreadSheet object */
var formURL = e.source.getFormUrl(); // Form URL
var form = FormApp.openByUrl(formURL); // Form object
var responses = form.getResponses(); // All responses
var lastresponse = responses[responses.length-1]; // Last response
var questions = Object.keys(e.namedValues); // Array of questions as written on the sheet
var answers = e.values; // Array of answers as written on the sheet
/* Just being paranoid */
if (e.range.getNumColumns() != answers.length) {
Logger.log("ERROR: Number of answers is different from number of columns");
return false;
}
/* Array that will contain the answers as given by the user, before
any automatic formatting done by Google Sheets */
var actual_answers = [];
actual_answers.push(e.values[0]); // First value is always the timestamp
/* Loop over the form items */
items = form.getItems();
for (i in items) {
/* Get info about the current item */
var item = items[i];
var itemID = item.getId();
var itemTitle = item.getTitle();
var itemType = item.getType();
/* Debug: log the form's question */
Logger.log("[" + itemID + "] " + itemTitle + " - " + itemType);
/* Skip non-question items */
if (questions.indexOf(itemTitle) == -1) {
Logger.log(" Not a question, skipping " + itemType);
continue;
}
/* Get the last answer to the current question. Throws error if the item is not
a question. */
var itemAnswer = lastresponse.getResponseForItem(item).getResponse();
Logger.log(" Answer: " + itemAnswer);
/* Store the answer */
actual_answers.push(itemAnswer);
} // end for
/* Check that the number of answers corresponds to the number of columns
minus one (the timestamp column) */
if (e.range.getNumColumns() != actual_answers.length) {
Logger.log("ERROR: Number of actual answers (" + actual_answers.length
+ ") is different from number of columns (" + e.range.getNumColumns() + ")");
return false;
}
/* Rewrite the response for the current question, making sure that
it is written as a string */
e.range.setNumberFormat("@STRING@");
e.range.setValues([actual_answers]);
} // end onFormSubmit
@radarforest
Copy link

Hey there!

I'm trying to add your code to a form and I keep getting an error because the number of actual answers is different from the number of columns. My form collects the e-mail address of the recipient, and that seems to throw off your code.

Any chance you could help me work through this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment