Reputation: 5042
With the recent study-from-home dynamics hastly implemented by budget schools, I am now facing a mindless-robot-like-task of filling in attendance for my kids everyday multiple times per child. The school shared this form:
I, being a developer, want to create a UI and have my kids submit thier attendance to this form on thier own when the teacher asks for one over Zoom. Ids it possible? I'm a .Net developer and have not developed anything for Office 365 Online and have no idea where to start looking for Google.
Upvotes: 18
Views: 77981
Reputation: 530
Easiest way is to simply launch Developer Tools (F12 or Ctrl+Shift+I usually) and go to the Network tab. Enable "Preserve log". Then just submit your form as usual, with the values you want (or test values).
In the Network tab, type "formResponse" in the filter bar. Find the latest entry and select it. Then go to Payload tab and click "view source". You should see something like this. The text you see in the form data is simply what your append to the link of your form plus a '?' (question mark). So in this case our link will be https://docs.google.com/forms/d/e/1FAIpQLSfrGn49hcbeioNNa25Osp4fwTG2xV3BmmN9-cMWWC2-xvcQyg/viewform?entry.1475351979=test&entry.280503419=test&entry.519373518=KG+1&entry.1124370742=French&entry.301819105=A&dlut=1662432187937&hud=true&entry.519373518_sentinel=&entry.301819105_sentinel=&entry.1124370742_sentinel=&fvv=1&partialResponse=%5Bnull%2Cnull%2C%22-6523749705829110087%22%5D&pageHistory=0&fbzx=-6523749705829110087.
Now let's just remove the extra parameters that aren't answers. Find the last answer you input and remove the rest. Our new link will be https://docs.google.com/forms/d/e/1FAIpQLSfrGn49hcbeioNNa25Osp4fwTG2xV3BmmN9-cMWWC2-xvcQyg/viewform?entry.1475351979=test&entry.280503419=test&entry.519373518=KG+1&entry.1124370742=French&entry.301819105=A
This will open the form page with pre-filled values. If you actually want to submit the response right away, replace "viewform" with "formResponse".
The google form I tried is from @AHunt's answer, so thank you for that sample google form.
Upvotes: 1
Reputation: 41
It is possible by reconstructing the form URL with pre-filled values.
In the Google Form, each question is named internally with "entry.". For each of the question, you need to find the entry number and assign them the right values and add it to the URL parameters like in the below image:
(Edited Oct 2021: The ids are no longer found in the name attribute of each input field in the HTML as shown in the image. They are now located in a div
within the form
element. @hdrz's answer below to look in the Javascript present just at the end of the body tag is now also defunct.)
I've recreated your form here https://docs.google.com/forms/d/e/1FAIpQLSfrGn49hcbeioNNa25Osp4fwTG2xV3BmmN9-cMWWC2-xvcQyg/viewform
And here is the reconstructed URL with prefill values https://docs.google.com/forms/d/e/1FAIpQLSfrGn49hcbeioNNa25Osp4fwTG2xV3BmmN9-cMWWC2-xvcQyg/viewform?entry.1475351979=Julia&entry.280503419=Andrews&entry.519373518=4&entry.301819105=E
Hope it helps
Upvotes: 19
Reputation: 11
You can find the IDs in the console development section of Chrome.
Fill the form open networks tabs.
You can find the ID of each query in the form.
Upvotes: 0
Reputation: 371
As a Google Form editor you can get a pre-filled URL (complete with entry values) by choosing the 'Get pre-filled link' option at the top right of the page, after clicking the three dots.
This will open the form in a new tab. The Submit (or Next) button will be replaced with a 'Get link' button.
After filling in the desired form responses, clicking 'Get link' will open a preview of what users will see. If everything is correct, clicking the 'COPY LINK' button (at the bottom left of page) will provide the URL.
The URL will look like this:
Upvotes: 5
Reputation: 61
I think I may be late but still give you a solution. I had made similar script to send my school attendance.
Every field in Google Forms is associated with a entry.<id>
. You have two ways to automate the form.
One way is to extract those IDs with and make a dictionary where entry.<id>
is the key and your answer is the value.Then you have to send a POST
request to the form URL with the dictionary as the data. You have automated the form.
To extract the IDs, inspect the html code and look at the <script>
at the (very) end of the page. It lòoks something like this :
var FB_PUBLIC_LOAD_DATA_ = [null,[null,[[2030831236,"First Name (in English)",null,0,[[1475351979,null,1]
]
]
,[86681139,"Last Name (in English)",null,0,[[280503419,null,1]
]
]
,[836880978,"Grade",null,2,[[519373518,[["KG 1",null,null,null,0]
,["KG 2",null,null,null,0]
,["1",null,null,null,0]
,["2",null,null,null,0]
,["3",null,null,null,0]
,["4",null,null,null,0]
,["5",null,null,null,0]
,["6",null,null,null,0]
,["7",null,null,null,0]
,["8",null,null,null,0]
,["9",null,null,null,0]
,["10",null,null,null,0]
,["11",null,null,null,0]
,["12",null,null,null,0]
]
,1,null,null,null,null,null,0]
]
]
,[221348070,"Section",null,2,[[301819105,[["A",null,null,null,0]
,["B",null,null,null,0]
,["C",null,null,null,0]
,["D",null,null,null,0]
,["E",null,null,null,0]
,["G",null,null,null,0]
]
,1,null,null,null,null,null,0]
]
]
,[366027193,"Subject",null,2,[[1124370742,[["Math",null,null,null,0]
,["Science",null,null,null,0]
,["English",null,null,null,0]
,["Arabic",null,null,null,0]
,["Islamic",null,null,null,0]
,["Social",null,null,null,0]
,["Moral",null,null,null,0]
,["Art",null,null,null,0]
,["Computer",null,null,null,0]
,["French",null,null,null,0]
,["Physics",null,null,null,0]
,["Chemistry",null,null,null,0]
,["Biology",null,null,null,0]
,["Business",null,null,null,0]
]
,1,null,null,null,null,null,0]
]
]
]
,null,null,[null,null,null,null,null,[null,null,null,[3,169,244,null,1]
,[217,242,253,null,1]
]
]
,null,null,null,"Attendance Form",48,null,null,null,null,null,[2]
]
As you can see there are two numbers with each field. One of them is the ID and another one I don't know. The second number is the ID we need. Using RegEx we can extract all the numbers and collect every second number in a list. This list will contain all the IDs.
Another way, as stated by others, is to reconstruct the URL with prefilled values. But in this too you have to extract the IDs.
I have included both of them in one and made this script :
import requests
from bs4 import BeautifulSoup
import re
def get_questions(url):
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
content = soup.body.find_all(text = re.compile('var FB'))
match = re.findall('[,]["][\w\s]+["][,]', str(content))
#It will match all the questions in the form
question_strings = [x.strip('"') for x in match]
match_ids = re.findall('(?<=\[\[)(\d+)', str(content))
#It will find all the numbers in the content
question_ids = ['entry.' + x for x in match_ids[1:]]
#It will leave the first numbers (they are not the ids)
return question_ids
# Below are only for when you want to know the form fills with their corresponding entry ids
# questions = dict(zip(question_strings, question_ids))
# return questions
def send_answers(url, fname, lname, grade, section, subject): #arrange this as per your form requirements
ids = get_questions(url)
answers = [fname, lname, grade, section, subject]
response = dict(zip(ids, answers))
if 'viewform' in url:
s = url.index('viewform')
response_url = url.replace(url[s::], 'formResponse?')
try:
r = requests.post(response_url, response)
if r.status_code == 200:
return '[!] Attendence posted !'
#In case an error happens, it will raise an exception
else:
raise Exception
#After raising the exception it will retry to submit using url reconstruction with prefilled values
except:
try:
ans_list = [x + '=' + y for x, y in zip(ids, answers)]
for i in range(0, len(ans_list)):
response_url += ans_list[i]
response_url += '&'
response_url.strip("&")
r = requests.get(response_url)
status = r.status_code
if status == 200:
return '[!] Attendance sent !'
else:
raise Exception
#If still an error happens, it will print out a message.
except:
return '[!] Attendance not sent !'
url = 'Form URL here'
fname = 'Your first name here'
lname = 'Your last name here'
grade = 'Your grade here'
section = 'Section here'
subject = 'Enter subject'
print(send_answers(url, fname, lname, grade, section, subject))
Hope it helps. Sorry for my bad English.
Upvotes: 6
Reputation: 471
As @AHunt writes, the entry number used to be in a name
attribute on the form fields. As of this answer time, I can no longer find the name
attribute on any of the form fields. However, now there is a script
tag at the end of the body
tag, with a short javascript code, and in it you can find the entry numbers, see here:
Also, it is possible to submit the form directly with pre-filled values. Just replace viewform
in the URL with formResponse
.
Similar URL as in the accepted answer, but now it will be submitted at once. Note that all required fields have to be filled!
Upvotes: 11