ทำแบบสอบถามของ Google forms โดยใช้ Python
1. สร้าง Google Form ขึ้นมา เพื่อสร้างแบบประเมินหรือแบบสอบถามของเรา
ในหัวข้อนี้ผมจะถือว่าทุกคนสร้างแบบประเมินได้แล้วนะ ผมจึงยังไม่ลงลึกมาก

2. ทดสอบส่งแบบประเมินหรือแบบสอบถาม
ขั้นตอนนี้เราจะดู Response บน Google Form เพื่อส่องดูว่าเวลาที่เรากรอกข้อมูลส่งฟอร์มผ่าน Web Browser (ผมใช้เป็น Google Chrome) แล้วมันเกิดอะไรขึ้นบ้าง และมีตัวแปรที่จะนำไปใช้ ทีนี้เปิด Developer Tools เลยครับผม (Google Chrome Menu > More Tools > Developer Tools)
2.1 เลือก Network Tab

2.2 ลองกรอกข้อมูลแล้วส่งไปใหม่อีกสักรอบครับผม

จากภาพด้านบน ให้เลือก formResponse จะปรากฏข้อมูลที่เราจะส่ง ซึ่ง entry.176467582 , entry.2076101627 ........ คือ id หรือ ตัวแปรอ้างอิงในแต่ละข้อที่เราทำแบบประเมิน ซึ่งข้อมูลพวกนี้คือข้อมูล payload ที่เราจะไปส่งแบบอัตโนมัติ
2.3 เมื่อเราทราบข้อมูลที่จะต้องส่งแล้ว ให้มาที่ Tab Header เพื่อดู Request ที่เราจะนำไปใช้

จากภาพด้านบนเราจะใช้ Request URL และ Request Method
3. มาเริ่มเขียนโปรแกรมหรือสร้าง API ในการส่งแบบประเมินอัตโนมัติกันเลย
ในทีนี้ให้ทำการ pip install requests เพื่อใช้ในการสร้าง API แนะนำให้สร้าง env แยกจะดีกว่านะครับ โดยผมจะใช้ VSCode ในการเขียนโปรแกรม
gform.py
import random
form_url = 'https://docs.google.com/forms/u/0/d/e/1FAIpQLSfHBUQtcm-BkLmNlWppXEoXdxrjgUvUM1fv6oetwrWJH8ulXw/formResponse'
def callGform():
value = ['มากที่สุด (5)', 'มาก (4)', 'ปานกลาง (3)',
'น้อย (2)', 'น้อยที่สุด (1)'] # ระดับการประเมิน
suggestions = "ไม่มีจร้า" # ข้อเสนอแนะ
# ใช้ random.choice(value) เพื่อสุ่มระดับการประเมิน
# ใช้ entry แต่ละตัวคือชุดข้อมูลที่อ้างอิงในแต่ละข้อของแบบประเมิน
payload = {
"entry.1598024020": random.choice(value),
"entry.511057586": random.choice(value),
"entry.820219989": random.choice(value),
"entry.1536888005": random.choice(value),
"entry.1081501010": random.choice(value),
"entry.1016627210": random.choice(value),
"entry.1606843533": random.choice(value),
"entry.1711328391": random.choice(value),
"entry.2042848464": random.choice(value),
"entry.1989784115": random.choice(value),
"entry.1743732877": random.choice(value),
"entry.2043491057": random.choice(value),
"entry.426623522": random.choice(value),
"entry.148559807": random.choice(value),
"entry.131493263": random.choice(value),
"entry.1656012255": random.choice(value),
"entry.761453629": random.choice(value),
"entry.1973681773": random.choice(value),
"entry.1219662185": random.choice(value),
"entry.1196421183": random.choice(value),
"entry.1747755167": random.choice(value),
"entry.2096137980": random.choice(value),
"entry.1155374026": random.choice(value),
"entry.204445977": random.choice(value),
"entry.1892499685": random.choice(value),
"entry.2076101627": random.choice(value),
"entry.176467582": suggestions
}
if __name__ == '__main__':
res = requests.post(form_url, payload)
if res.status_code == 200:
return True
elif res.status_code == 400:
print('Check your payload')
return False
elif res.status_code == 404:
print('Check your form_url')
return False
return False
# จำนวนแบบประเมินที่ต้องการให้ทำ
number = 100
for x in range(number):
callGform()
4. Run Program และดูผล
เปิด terminal หรือ command line เพื่อสั่ง run โปรแกรมโดยพิมพ์ python gform.py

ผลลัพธ์
