首页 > 解决方案 > 如何在谷歌表单中预填日期

问题描述

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

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

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

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

标签: google-apps-scriptgoogle-sheetsgoogle-forms

解决方案


我以 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 应用程序...


推荐阅读