【问题标题】:How to prefill date in google form如何在谷歌表单中预填日期
【发布时间】:2019-07-29 12:07:24
【问题描述】:

我正在尝试在谷歌表单中预先填写日期。所以它将把它填充的日期作为默认值。

我尝试在谷歌表格中使用 TODAY() 函数。所以我更改了网址以从谷歌表格中的单元格获取日期。适用于除日期之外的所有内容..

https://docs.google.com/forms/d/e/1FAIpQLSf7cdT34eHp-GXagq3DsnxX1MD_c-G6lbF6yFWOMnUvtPYUUQ/viewform?entry.178275308=**&A6&**

知道如何以最简单的方式做到这一点吗?

【问题讨论】:

  • 你如何预填充这个 - 通过 URL 或脚本?
  • @ross 我通过 URL 尝试过,如果它更简单,我也可以使用脚本
  • 在问题本身中包含您尝试过的所有内容 - 您将什么传递给 URL 以尝试实现这一目标?
  • @ross 我编辑了帖子。您可以在 URL 的末尾看到我放置了我想从中获取信息的单元格。适用于除日期之外的所有内容。
  • 此 URL 是否在工作表的单元格中?您使用的是 Excel 还是 Google 表格?

标签: google-apps-script google-sheets google-forms


【解决方案1】:

将您的脚本部署为 Web 应用程序

  • 将您的 Apps 脚本发布为 Web App,并向用户提供 Web 部署的 URL
  • doGet() 函数中编写代码,以便每次用户打开链接时都会对其进行评估
  • 使用new Date() 获取当前日期并将其作为itemResponse 传递给prefilled Form
  • 使用<script> </script> 标记内的Java Script 方法window.top.location.href 创建到URL to the prefilled form 的重定向,并使用HtmlService 返回它
// will be automatically run every time the user opens the URL of the web deployment
function doGet(){
  //open your form
  var form = FormApp.openById('1Zs5Wo0wS9esNkcq8ztOZmQs3f5NrO7hgJ_0fYk262z4');
  //get the questions
  var questions = form.getItems();
  //get the question where you want to prefill the date as an answer, e.g. first question
  var question1=questions[0];
  //get the current date
  var time=Utilities.formatDate(new Date(), "GMT+2", "dd-MM-yyyy");
  // prefill form with current date
  var prefilledTime=form.createResponse().withItemResponse(question1.asTextItem().createResponse(time));  
 // Get the link to the prefilled form
  var URL=prefilledTime.toPrefilledUrl();
 // redirect to the prefilled form URL dynamically created above 
  return HtmlService.createHtmlOutput("<script>window.top.location.href=\"" + URL + "\";</script>");
}

【讨论】:

    【解决方案2】:

    如果您只需要提交表单的日期,Google 表单会在其时间戳中自动提供此日期。

    如果您想从现有电子表格中预先填写日期,那么只要您遵守 Google 的日期格式,则可以使用 URL 方法(月和日必须是 2 位数字,即“01”而不是“1”)。

    =substitute('Sheet_with_Form_Link'!$A$1,"2099-01-01",text(Sheet_with_date!A1,"yyyy-mm-dd"))
    

    其中
    Sheet_with_Form_Link'!$A$1 是 Google 电子表格中工作表中的一个单元格,其中包含通过“获取预填充链接”获得的 URL
    “2099-01-01”是您在使用“获取预填链接”时预填到表单中的数据
    Sheet_with_date!A1 是 Google 电子表格中工作表中的一个单元格,其中包含您想要的日期,例如

    之一
    =today()
    1920/03/26
    17Oct2002
    

    【讨论】:

      【解决方案3】:

      我以 ziganotschka 的代码为基础,但我必须进行一些更改,也许它可以帮助某人:

      function doGet(){
        //open your form open by URL
        var form = FormApp.openByUrl('https://docs.google.com/forms/d/1M-zgHyDOz2ob5StkwIoDCShJ9tePw-I5TGhdx/prefill')
        //get the questions
        var questions = form.getItems();
        //get the question where you want to prefill the date as an answer, e.g. first question
        var question1=questions[0];
        //get the current date
        var time=new Date();
        // prefill form with current date
        var prefilledTime=form.createResponse().withItemResponse(question1.asDateItem().createResponse(time));  
        // Get the link to the prefilled form
        var URL=prefilledTime.toPrefilledUrl();
        // redirect to the prefilled form URL dynamically created above 
        return HtmlService.createHtmlOutput("<script>window.top.location.href=\"" + URL + "\";</script>");
      }
      

      完成后,别忘了发布 -> 部署为 Web 应用程序...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-31
        • 1970-01-01
        • 2021-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-05
        • 1970-01-01
        相关资源
        最近更新 更多