谷歌形式的e.values跳过空的答案,有没有解决方法? [英] e.values in google forms skips empty answers, is there a workaround?

查看:68
本文介绍了谷歌形式的e.values跳过空的答案,有没有解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google表单,它将回复数据写入一个电子表格,其中包含一个脚本,该脚本假定他/她的表单填写了表单填充信息。

之前我已经成功使用 e.values ,并且邮件生成得很好。现在似乎存在一些问题,因为空的答案被跳过,这意味着例如 e.values [9] 实际上变成了第9列,而不是10,因为它曾经是(至少在2014年的春天)。因此,如果有一个或多个字段留空,则以下答案会在数组中向后移动一个或多个步骤,具体取决于剩余问题的数量。像这样的行为给精心策划的脚本造成严重破坏!



我也尝试使用 namedValues ,但它不能容忍空领域!



有人知道工作吗?我会欣赏想法。

解决方案

open issue 已被标记为按预期工作。所以没有帮助。



这是一个解决方法。也可在此要点中找到。



例子



 函数onFormSubmit(e){
fixFormEvent(e);
...
}



fixFormEvent(e)



  / ** 
*将空白回应强制为事件对象的值属性,以便值的索引
*正确反映问题订购。 (在新表格+新表格中,空白回复
*将在事件对象中跳过。
*
*请参阅http://stackoverflow.com/a/26975968/1677912
*
* @param {event} e作为一个Spreadsheet Form对象接收的事件,该事件的值
*属性将被这个函数修改
* @return {event}相同的事件,用于链接
* /
函数fixFormEvent(e){
var ss = SpreadsheetApp.getActive();
var formUrl = ss.getFormUrl(); //使用表单附加到工作表
var form = FormApp.openByUrl(formUrl);
var items = form.getItems();

var resp = [e.namedValues [Timestamp ]];

for(var i = 0; i< items.length; i ++){
switch(items [i] .getType()){
case FormApp .ItemType.IMAGE:
case FormApp.ItemType.PAGE_BREAK:
case FormApp.ItemType.SECTION_HEADER:
//没有响应的项目​​ - 跳过它
break;

ca se FormApp.ItemType.CHECKBOX:
大小写FormApp.ItemType.DATE:
大小写FormApp.ItemType.DATETIME:
大小写FormApp.ItemType.DURATION:
大小写FormApp.ItemType.GRID :
案例FormApp.ItemType.LIST:
案例FormApp.ItemType.MULTIPLE_CHOICE:
案例FormApp.ItemType.PARAGRAPH_TEXT:
案例FormApp.ItemType.SCALE:
案例FormApp.ItemType.TEXT:
case FormApp.ItemType.TIME:
//如果item有响应,请将其附加到数组。如果不是,则追加空白。
var itemTitle = items [i] .getTitle();
var type = items [i] .getType();
if(itemTitle ===)抛出新的错误(Untitled item);
var itemResp = [];
if(e.namedValues中的itemTitle){
itemResp = e.namedValues [itemTitle];
}
resp.push(itemResp);
休息;

默认值:
Logger.log(Unknown item type,index =+ items [i] .getIndex());
休息;
}
}
e.values = resp;
return e; //链接
}


I have a Google form, which writes response data to a spreadsheet containing a script that is supposed mail the form-filler with his/her answers to the form.

I had success with e.values before, and the mail generated just fine. Now there seems to be some problems, as empty answers are skipped, meaning that e.values[9] for example becomes actually column 9, instead of 10, as it used to be (in the spring of 2014, at least). So if there are one or more fields left empty the following answers move backward in the array one or more steps, depending on the number of questions left empty. Behaviour like this wreaks havoc in carefully planned scripts!

I also tried to use namedValues, but it can't tolerate empty fields either!

Does anybody know of a work around? I'd appreciate ideas.

解决方案

That open issue has been marked Working as intended. So no help there.

Here's a work-around. Also available in this gist.

Example

function onFormSubmit(e) {
  fixFormEvent( e );
  ...
}

fixFormEvent( e )

/**
 * Force blank reponses into event object's values property, so that the value's index
 * correctly reflects the question order. (With "new Sheets" + "new Forms", blank responses
 * are skipped in the event object.
 *
 * see http://stackoverflow.com/a/26975968/1677912
 *
 * @param {event} e   Event received as a Spreadsheet Form object. The event's value
 *                    property will be modified by this function.
 * @return {event}    The same event, for chaining
 */
function fixFormEvent( e ) {
  var ss = SpreadsheetApp.getActive();
  var formUrl = ss.getFormUrl();             // Use form attached to sheet
  var form = FormApp.openByUrl(formUrl);
  var items = form.getItems();

  var resp = [e.namedValues["Timestamp"]];

  for (var i=0; i<items.length; i++) {
    switch (items[i].getType()) {
      case FormApp.ItemType.IMAGE:
      case FormApp.ItemType.PAGE_BREAK:
      case FormApp.ItemType.SECTION_HEADER:
        // Item without a response - skip it
        break;

      case FormApp.ItemType.CHECKBOX:
      case FormApp.ItemType.DATE:
      case FormApp.ItemType.DATETIME:
      case FormApp.ItemType.DURATION:
      case FormApp.ItemType.GRID:
      case FormApp.ItemType.LIST:
      case FormApp.ItemType.MULTIPLE_CHOICE:
      case FormApp.ItemType.PARAGRAPH_TEXT:
      case FormApp.ItemType.SCALE:
      case FormApp.ItemType.TEXT:
      case FormApp.ItemType.TIME:
        // If item has a response, append it to array. If not, append blank.
        var itemTitle = items[i].getTitle();
        var type = items[i].getType();
        if (itemTitle === "") throw new Error( "Untitled item" );
        var itemResp = [];
        if (itemTitle in e.namedValues) {
          itemResp = e.namedValues[itemTitle];
        }
        resp.push( itemResp );
        break;

      default:
        Logger.log( "Unknown item type, index=" + items[i].getIndex() );
        break;
    }
  }
  e.values = resp;
  return e;  // For chaining
}

这篇关于谷歌形式的e.values跳过空的答案,有没有解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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