Auto-Fill 或自动化 Google 表格

Auto-Fill or automate a Google Form

随着最近 study-from-home 预算学校仓促实施的动态,我现在面临 mindless-robot-like-task 每个 child 每天多次为我的孩子填写出勤率。学校分享了这张表格:

作为一名开发人员,我想创建一个 UI 并让我的孩子在老师要求通过 Zoom 提交他们自己的出勤率时自行提交此表格。有可能吗?我是 .Net 开发人员,没有为 Office 365 Online 开发任何东西,也不知道从哪里开始寻找 Google.

可以通过使用预填值重建表单 URL。 在 Google 表单中,每个问题在内部都以“entry.”命名。对于每个问题,您需要找到条目号并为它们分配正确的值并将其添加到 URL 参数中,如下图所示: (2021 年 10 月编辑:如图所示,在 HTML 中每个输入字段的名称属性中不再找到 ID。它们现在位于 form 中的 div 中element.@hdrz 在下面查看 Javascript 的答案现在也已失效。)

我在此处重新创建了您的表单https://docs.google.com/forms/d/e/1FAIpQLSfrGn49hcbeioNNa25Osp4fwTG2xV3BmmN9-cMWWC2-xvcQyg/viewform

这里是重建的 URL 预填充值 https://docs.google.com/forms/d/e/1FAIpQLSfrGn49hcbeioNNa25Osp4fwTG2xV3BmmN9-cMWWC2-xvcQyg/viewform?entry.1475351979=Julia&entry.280503419=Andrews&entry.519373518=4&entry.301819105=E

希望对您有所帮助

正如@AHunt 所写,条目编号曾经位于表单字段的 name 属性中。截至本回答时间,我无法再在任何表单字段上找到 name 属性。但是,现在在 body 标签的末尾有一个 script 标签,带有一个简短的 javascript 代码,您可以在其中找到条目号,请参见此处:

此外,可以直接提交带有 pre-filled 值的表单。只需将 URL 中的 viewform 替换为 formResponse.

与已接受的答案类似URL,但现在将立即提交。请注意,必须填写所有必填字段!

https://docs.google.com/forms/d/e/1FAIpQLSfrGn49hcbeioNNa25Osp4fwTG2xV3BmmN9-cMWWC2-xvcQyg/formResponse?entry.1475351979=Julia&entry.280503419=Andrews&entry.519373518=4&entry.301819105=E&entry.1124370742=Art

我想我可能迟到了,但还是给你一个解决方案。我制作了类似的脚本来发送我的学校出勤率。 Google 表单中的每个字段都与 entry.<id> 关联。您有两种方法可以使表单自动化。

一种方法是提取这些 ID 并制作字典,其中 entry.<id> 是键,您的答案是 value.Then 您必须向表单发送 POST 请求URL 以字典为数据。您已将表单自动化。

要提取 ID,请检查 html 代码并查看页面(非常)末尾的 <script>。它看起来像这样:

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]
]

如您所见,每个字段都有两个数字。其中一个是身份证,另一个我不知道。第二个数字是我们需要的 ID。使用正则表达式,我们可以提取所有数字并收集列表中每隔一个数字。此列表将包含所有 ID。

另一种方法,如其他人所述,是用预填充值重建 URL。但在这方面你也必须提取 ID。

我将它们合二为一并制作了这个脚本:

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))

希望对您有所帮助。抱歉我的英语不好。

作为 Google 表单编辑器,您可以获得 预填充 URL (完成条目值) 通过选择页面右上角的“获取预填充 link”选项,单击三个点后.

这将在新选项卡中打开表单。 提交 (或下一步按钮将替换为'获取link' 按钮。

填写所需的表单回复后,单击“获取 link”将打开用户将看到的预览。如果一切正确,单击“复制 LINK”按钮 (位于页面左下方) 将提供 URL.

URL 将如下所示:

https://docs.google.com/forms/d/e/yourFormidhere/viewform?usp=pp_url&entry.1890935147=exampleprefilledvalue1&entry.1928475566=exampleprefilledvalue2&entry.2145528193=exampleprefilledvalue3

您可以在 Chrome 的控制台开发部分找到这些 ID。
填写表格打开网络选项卡。
您可以在表单中找到每个查询的 ID。