google-apps-script - 谷歌表单中的 e.values 会跳过空答案,有解决方法吗?

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

我有一个 Google 表单,它将响应数据写入电子表格,其中包含一个脚本,该脚本应该将他/她对表单的答案邮寄给表单填写者。

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

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

有人知道解决方法吗?我很感激您的想法。

最佳答案

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

这是一个解决方法。还可在 this gist 中找到.

示例

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

修复表单事件(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
}

关于google-apps-script - 谷歌表单中的 e.values 会跳过空答案,有解决方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25938213/

相关文章:

java - Android 应用程序 - 将数据发布到 Google 表单

google-apps-script - 开发模式 - 其他用户制作的模板电子表格副本

javascript - 自动展开公式,然后复制粘贴值

html - 将原始文本/数据从 Google 表格提取到 HTML 网页

google-apps-script - Google 表格 - 基本脚本

google-forms - 当我尝试打开受限制的 Google 表单时,如何更改帐户?

html - 无法在移动 iOS Safari 上滚动 iframe

google-apps-script - 如何在 Google Apps 脚本中根据单元格的背景颜色反转字体颜色?

google-apps-script - Google Apps 脚本 POST 请求 UrlFetchApp

google-apps-script - 在特定文件夹中创建 Google 电子表格