限时后自动提交谷歌表格

问题描述 投票:0回答:1

如果用户未在20分钟内点击提交,我想在我的Google表单中使用应用脚本在20分钟内自动提交表单。无论如何要实现这个????

google-apps-script google-form google-form-quiz
1个回答
2
投票

不,即使您向其添加了应用程序脚本,也无法控制Google表单的客户端,因为Apps脚本在服务器上运行。

一种可能的解决方案是将您的表单作为Google Apps Script web app提供。此时,您可以编写客户端JavaScript并使用window.setTimeout在20分钟后提交表单。

以下是一些示例文件Code.gsquiz.html,它们可以提供启动Web应用程序的基本框架。一个空白项目将Code.gs作为默认文件,然后您必须添加文件>新建> HTML文件以启动另一个文件。

您可以在id的注释掉的行中输入您拥有的任何电子表格的Code.gs,以将响应附加到该电子表格中。 (您还可以根据需要创建新的电子表格来自动化该过程.Example of creating spreadsheet to hold data for Apps Script example can be found here

// file Code.gs
function doGet() {
  return HtmlService.createHtmlOutputFromFile("quiz");
}

function doPost(request) {
  if (request.answer) {
    console.log(request.answer);  // View > Execution transcript to verify this
    //var ss = SpreadsheetApp.openById(id).getSheetByName("Quiz Responses");
    //ss.appendRow([request.answer /* additional values comma separated here */ ]);
  }
}
<!DOCTYPE html>
<!-- file quiz.html -->
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1>Quiz</h1>
    <form>
      What is Lorem Ipsum?
      <input name="loremipsum" type="text"/>
      <button>Submit</button>
    </form>
    <script>
      const button = document.querySelector("button");
      const timeLimitMinutes = 1; // low number for demo; change to 20 for application
      const timeLimitMilliseconds = timeLimitMinutes * 60 * 1000;
      // For this demo we are not going to serve a response page, so don't try to.
      button.addEventListener("submit", submitEvent => submitEvent.preventDefault());
      // attach our custom submit to both the button and to the timeout
      button.addEventListener("click", submitForm)
      window.setTimeout(submitForm, timeLimitMilliseconds)

      function submitForm() {
        button.setAttribute("disabled", true);
        document.querySelector("h1").textContent = "Quiz submitted";
        // for demo: submitting just a single answer.
        // research Apps Script documentation for rules on submitting forms, certain values not allowed
        // consider a helper function `makeForm()` that returns a safe object to submit.
        const answer = document.querySelector("input").value;
        google.script.run.doPost({ answer });
      }
    </script>
  </body>
</html>

使用发布测试>部署为Web应用程序...

© www.soinside.com 2019 - 2024. All rights reserved.