使用Google电子表格中的数据“预填”Google表单有可能吗? [英] Is it possible to 'prefill' a google form using data from a google spreadsheet?

查看:113
本文介绍了使用Google电子表格中的数据“预填”Google表单有可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式,可以通过Google电子表格中的特定数据预填Google表单。该表格对每个人都有相同的标准问题,但前两个问题中的数据将通过现有Google电子表格中的独特数据进行预填充。这些数据根据他们在现有电子表格中的电子邮件地址是唯一的。

来源电子表格示例

Col 1 Col 2 Col 3
email name生日
@mike Mike Jones 1975年5月9日
@ jim Jim Smith 1985年4月19日






示例一



问题1 - 预填充谷歌电子表格中的数据(Mike Jones)



问题2 - 从谷歌电子表格预填充(1975年5月9日)

问题3 - 空白(等待用户回复)



p>




表格示例二

问题1 - 从Google电子表格预填充(Jim Smith)数据。

问题2 - 预填充数据(1985年4月19日),来自Google电子表格。

问题3 - 空白(等待用户回复)



p>




有谁知道这是否可以做到?如果是的话,任何帮助或方向将非常感激。



预先感谢您!

托德

驱动表单的文档。例如,您最终会得到一个这样的网址:

  https://docs.google.com/forms/ d /  -  form-id  -  / viewform?entry.726721210 = Mike + Jones& entry.787184751 = 1975-05-09& entry.1381372492& entry.960923899 
pre
$ b

buildUrls()



在本例中,问题1名称的ID为 726721210 ,而问题2,生日是 787184751 。问题3和问题4为空。



您可以通过将通过用户界面提供的URL作为模板来生成预填充的URL,如下所示:




$ b

function buildUrls(){
var template =https: //docs.google.com/forms/d/--form-id--/viewform?entry.726721210=##Name##&entry.787184751=##Birthday##&entry.1381372492&entry。 960923899\" ;
var ss = SpreadsheetApp.getActive()。getSheetByName(Sheet1); // Email,Name,Birthday
var data = ss.getDataRange()。getValues();

//跳过标题,然后为Sheet1中的每一行创建URL。
for(var i = 1; i< data.length; i ++){
var url = template.replace('## Name ##',escape(data [i] [1]) )
.replace('## Birthday ##',data [i] [2] .yyyymmdd()); //看到
下面的yyyymmdd Logger.log(url); //你可以在这里做更有用的事情。
}
};

这非常有效 - 您可以通过电子邮件向每个人发送预填充网址,有一些问题已经被填写。



betterBuildUrls()



与我们使用强力创建模板不同,我们可以通过编程将它们拼凑起来。这将有一个好处,即我们可以重新使用代码,而不必记住更改模板。



表单中的每个问题都是一个项目。对于这个例子,假设表单只有4个问题,正如你所描述的那样。项目 [0] 是名称, [1] 是生日等等。



我们可以创建一个表单响应,我们不会提交 - 相反,我们将部分填写表单,仅用于获取预填表单URL。由于Forms API能够理解每个项目的数据类型,所以我们可以避免操纵日期和其他类型的字符串格式,这可以简化我们的代码。



编辑:如何预填Google表单复选框?


$ b

  / ** 
*用于生成预填表单URL的表单API
* /
函数betterBuildUrls(){
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName(Sheet1);
var data = ss.getDataRange()。getValues(); //预填充数据

var formUrl = ss.getFormUrl(); //使用表单附加到工作表
var form = FormApp.openByUrl(formUrl);
var items = form.getItems();

//跳过标题,然后为Sheet1中的每一行创建URL。
for(var i = 1; i< data.length; i ++){
//创建一个表单响应对象,并预先填充它
var formResponse = form.createResponse();

//预填名称
var formItem = items [0] .asTextItem();
var response = formItem.createResponse(data [i] [1]);
formResponse.withItemResponse(response);

//预填充生日
formItem = items [1] .asDateItem();
response = formItem.createResponse(data [i] [2]);
formResponse.withItemResponse(response);

//获取预填的表单URL
var url = formResponse.toPrefilledUrl();
Logger.log(url); //你可以在这里做更有用的事情。
}
};






yymmdd函数



预填表单URL中的任何日期项目都应采用以下格式: yyyy-mm-dd 。这个辅助函数用一个新的方法扩展了Date对象来处理转换。

当从电子表格中读取日期时,最终会得到一个javascript Date对象,如只要数据的格式可以识别为日期即可。 (你的例子是不可识别的,所以你可以用 5/9/1975 来代替 1975年5月9日 / p>

  //从http://blog.justin.kelly.org.au/simple-javascript函数格式化日期为yyyy-mm-dd / 
Date.prototype.yyyymmdd = function(){
var yyyy = this.getFullYear()。toString();
var mm =(this.getMonth()+ 1).toString(); // getMonth()是基于零的
var dd = this.getDate()。toString();

return yyyy +' - '+(mm [1]?mm:0+ mm [0])+' - '+(dd [1]?dd:0+ dd [0]);
};


I'm looking for a way to 'pre fill' a google form with specific data from a google spreadsheet. The form will have the same 'standard' questions for everyone, but the data in the first two question will be 'prefilled' with unique data from an existing google spreadsheet. The data will be unique based on their email address in the existing spreadsheet.

SOURCE SPREADSHEET EXAMPLE

Col 1       Col 2        Col 3 
email       name         birthday  
@mike       Mike Jones   May 9th 1975  
@jim        Jim Smith    April 19th 1985


FORM EXAMPLE ONE

Question 1 - prefilled with data (Mike Jones) from a google spreadsheet.

Question 2 - prefilled with data (May 9th 1975) from a google spreadsheet.

Question 3 - blank (awaiting user response)

Question 4 - blank (awaiting user response)


FORM EXAMPLE TWO

Question 1 - prefilled with data (Jim Smith) from a google spreadsheet.

Question 2 - prefilled with data (April 19th 1985) from a google spreadsheet.

Question 3 - blank (awaiting user response)

Question 4 - blank (awaiting user response)


Does anyone know if this can be done? If yes, any help or direction will be GREATLY appreciated.

Thank you in advance!
Todd

解决方案

You can create a pre-filled form URL from within the Form Editor, as described in the documentation for Drive Forms. You'll end up with a URL like this, for example:

https://docs.google.com/forms/d/--form-id--/viewform?entry.726721210=Mike+Jones&entry.787184751=1975-05-09&entry.1381372492&entry.960923899

buildUrls()

In this example, question 1, "Name", has an ID of 726721210, while question 2, "Birthday" is 787184751. Questions 3 and 4 are blank.

You could generate the pre-filled URL by adapting the one provided through the UI to be a template, like this:

function buildUrls() {
  var template = "https://docs.google.com/forms/d/--form-id--/viewform?entry.726721210=##Name##&entry.787184751=##Birthday##&entry.1381372492&entry.960923899";
  var ss = SpreadsheetApp.getActive().getSheetByName("Sheet1");  // Email, Name, Birthday
  var data = ss.getDataRange().getValues();

  // Skip headers, then build URLs for each row in Sheet1.
  for (var i = 1; i < data.length; i++ ) {
    var url = template.replace('##Name##',escape(data[i][1]))
                      .replace('##Birthday##',data[i][2].yyyymmdd());  // see yyyymmdd below
    Logger.log(url);  // You could do something more useful here.
  }
};

This is effective enough - you could email the pre-filled URL to each person, and they'd have some questions already filled in.

betterBuildUrls()

Instead of creating our template using brute force, we can piece it together programmatically. This will have the advantage that we can re-use the code without needing to remember to change the template.

Each question in a form is an item. For this example, let's assume the form has only 4 questions, as you've described them. Item [0] is "Name", [1] is "Birthday", and so on.

We can create a form response, which we won't submit - instead, we'll partially complete the form, only to get the pre-filled form URL. Since the Forms API understands the data types of each item, we can avoid manipulating the string format of dates and other types, which simplifies our code somewhat.

(EDIT: There's a more general version of this in How to prefill Google form checkboxes?)

/**
 * Use Form API to generate pre-filled form URLs
 */
function betterBuildUrls() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("Sheet1");
  var data = ss.getDataRange().getValues();  // Data for pre-fill

  var formUrl = ss.getFormUrl();             // Use form attached to sheet
  var form = FormApp.openByUrl(formUrl);
  var items = form.getItems();

  // Skip headers, then build URLs for each row in Sheet1.
  for (var i = 1; i < data.length; i++ ) {
    // Create a form response object, and prefill it
    var formResponse = form.createResponse();

    // Prefill Name
    var formItem = items[0].asTextItem();
    var response = formItem.createResponse(data[i][1]);
    formResponse.withItemResponse(response);

    // Prefill Birthday
    formItem = items[1].asDateItem();
    response = formItem.createResponse(data[i][2]);
    formResponse.withItemResponse(response);

    // Get prefilled form URL
    var url = formResponse.toPrefilledUrl();
    Logger.log(url);  // You could do something more useful here.
  }
};


yymmdd Function

Any date item in the pre-filled form URL is expected to be in this format: yyyy-mm-dd. This helper function extends the Date object with a new method to handle the conversion.

When reading dates from a spreadsheet, you'll end up with a javascript Date object, as long as the format of the data is recognizable as a date. (Your example is not recognizable, so instead of May 9th 1975 you could use 5/9/1975.)

// From http://blog.justin.kelly.org.au/simple-javascript-function-to-format-the-date-as-yyyy-mm-dd/
Date.prototype.yyyymmdd = function() {
  var yyyy = this.getFullYear().toString();                                    
  var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based         
  var dd  = this.getDate().toString();             

  return yyyy + '-' + (mm[1]?mm:"0"+mm[0]) + '-' + (dd[1]?dd:"0"+dd[0]);
};

这篇关于使用Google电子表格中的数据“预填”Google表单有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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