I'm looking for a way to 'pre fill' a google form with specific data from a google spreadsheet. The form will have the same 'standard' questions for everyone, but the data in the first two question will be 'prefilled' with unique data from an existing google spreadsheet. The data will be unique based on their email address in the existing spreadsheet.
SOURCE SPREADSHEET EXAMPLE
Col 1 Col 2 Col 3
email name birthday
@mike Mike Jones May 9th 1975
@jim Jim Smith April 19th 1985
FORM EXAMPLE ONE
Question 1 - prefilled with data (Mike Jones) from a google spreadsheet.
Question 2 - prefilled with data (May 9th 1975) from a google spreadsheet.
Question 3 - blank (awaiting user response)
Question 4 - blank (awaiting user response)
FORM EXAMPLE TWO
Question 1 - prefilled with data (Jim Smith) from a google spreadsheet.
Question 2 - prefilled with data (April 19th 1985) from a google spreadsheet.
Question 3 - blank (awaiting user response)
Question 4 - blank (awaiting user response)
Does anyone know if this can be done? If yes, any help or direction will be GREATLY appreciated.
Thank you in advance!
Todd
You can create a pre-filled form URL from within the Form Editor, as described in the documentation for Drive Forms. You'll end up with a URL like this, for example:
buildUrls()
In this example, question 1, "Name", has an ID of
726721210
, while question 2, "Birthday" is787184751
. Questions 3 and 4 are blank.You could generate the pre-filled URL by adapting the one provided through the UI to be a template, like this:
This is effective enough - you could email the pre-filled URL to each person, and they'd have some questions already filled in.
betterBuildUrls()
Instead of creating our template using brute force, we can piece it together programmatically. This will have the advantage that we can re-use the code without needing to remember to change the template.
Each question in a form is an item. For this example, let's assume the form has only 4 questions, as you've described them. Item
[0]
is "Name",[1]
is "Birthday", and so on.We can create a form response, which we won't submit - instead, we'll partially complete the form, only to get the pre-filled form URL. Since the Forms API understands the data types of each item, we can avoid manipulating the string format of dates and other types, which simplifies our code somewhat.
(EDIT: There's a more general version of this in How to prefill Google form checkboxes?)
yymmdd Function
Any date item in the pre-filled form URL is expected to be in this format:
yyyy-mm-dd
. This helper function extends the Date object with a new method to handle the conversion.When reading dates from a spreadsheet, you'll end up with a javascript Date object, as long as the format of the data is recognizable as a date. (Your example is not recognizable, so instead of
May 9th 1975
you could use5/9/1975
.)