使用 App Scripts 打开表单并进行选择 [英] Use App Scripts to open form and make a selection

查看:22
本文介绍了使用 App Scripts 打开表单并进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我正在测试 Google Drive 表单,该表单将记录学校选举的选票以确保其安全.

有没有办法从共享 URL 和列表/输入数据打开表单?简而言之,我可以编写一个脚本来充当机器人来投票并尝试使表单崩溃吗?

示例网址::

//模拟POST形成函数 sendHttpPost() {//从<form action>复制整个URLvar formAction = "https://docs.google.com/spreadsheet/formResponse?formkey=#FORMKEY#&amp;ifq";无功载荷 = {"entry.0.single": "Nelson",//名字"entry.1.single": "10",//用户数"entry.2.single": "user@example.com"//电子邮件 ID};//因为payload是一个JavaScript对象,所以会被解释为//一个 HTML 表单.(我们不需要指定 contentType;它会//自动默认为application/x-www-form-urlencoded"//或 'multipart/form-data')变量选项 = {"方法": "发布",有效载荷":有效载荷};var response = UrlFetchApp.fetch(formAction, options);}

结果

这是上述脚本的结果,表单回复已添加到电子表格中.

To put this briefly I am testing a Google drive form that will record votes for a school election to ensure that it is secure.

Is there a way to open a form from the shared URL and list/input data? In short, can I write a script to act like a bot that will vote and try to crash the form?

Sample URL: http://docs.google.com/forms/d/RANDOM_STRING/viewform

解决方案

Edit: Some time around the end of 2014 a change in the Google Forms service invalidated this hack. Look at Is it possible to 'prefill' a google form using data from a google spreadsheet? and How to prefill Google form checkboxes? for a solution that relies on the Form methods.


A Google Form, when shown as a "live form", is just an HTML Form, with all the regular behaviors of a form. You can view the HTML source of a live form, and get the information that will help you simulate POST requests.

HTML Form

For example, look at the form from Spreadsheet Email Trigger. Here is the form HTML, cleaned up for readability:

<form action="https://docs.google.com/spreadsheet/formResponse?formkey=#FORMKEY#&amp;ifq"

 method="POST" id="ss-form">

  <br>
  <label class="ss-q-title" for="entry_0">First Name
    <span class="ss-required-asterisk">*</span>
  </label>
  <label class="ss-q-help" for="entry_0"></label>
  <input type="text" name="entry.0.single" value="" class="ss-q-short" id="entry_0">
  <br>
  <label class="ss-q-title" for="entry_1">No of User
    <span class="ss-required-asterisk">*</span>
  </label>
  <label class="ss-q-help" for="entry_1"></label>
  <select name="entry.1.single" id="entry_1">
    <option value="5">5</option>
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="30">30</option>
  </select>
  <br>
  <label class="ss-q-title" for="entry_2">Email ID
    <span class="ss-required-asterisk">*</span>
  </label>
  <label class="ss-q-help" for="entry_2"></label>
  <input type="text" name="entry.2.single" value="" class="ss-q-short" id="entry_2">
  <br>
  <input type="hidden" name="pageNumber" value="0">
  <input type="hidden" name="backupCache" value="">


  <input type="submit" name="submit" value="Submit">
  <div class="password-warning">Never submit passwords through Google Forms.</div>
</form>

Important elements are marked in this screenshot:

Script to simulate a Google Form submission

Armed with the action URL and field names, we can code a function to programmatically submit a form, by modifying the example from the UrlFetch documentation:

// Simulate POST to form
function sendHttpPost() {

  // Copy the entire URL from <form action>
  var formAction = "https://docs.google.com/spreadsheet/formResponse?formkey=#FORMKEY#&amp;ifq";

  var payload = {
    "entry.0.single": "Nelson",            // First Name
    "entry.1.single": "10",                // No of users
    "entry.2.single": "user@example.com"   // Email ID
  };

  // Because payload is a JavaScript object, it will be interpreted as
  // an HTML form. (We do not need to specify contentType; it will
  // automatically default to either 'application/x-www-form-urlencoded'
  // or 'multipart/form-data')

  var options = {
    "method": "post",
    "payload": payload
  };

  var response = UrlFetchApp.fetch(formAction, options);
}

Result

Here's the result of the above script, a form response has been added to the spreadsheet.

这篇关于使用 App Scripts 打开表单并进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆