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

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

问题描述

我有一个 Google 表单,该表单将响应数据写入包含脚本的电子表格,该脚本应该将填写者对表单的回答邮寄给表单填写者.

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.

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

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!

我也尝试过使用 namedValues,但它也不能容忍空字段!

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.

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 in google forms 跳过空答案,有解决方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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