Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script e、 google表单中的值跳过空答案,有解决方法吗?_Google Apps Script_Google Sheets_Google Forms - Fatal编程技术网

Google apps script e、 google表单中的值跳过空答案,有解决方法吗?

Google apps script e、 google表单中的值跳过空答案,有解决方法吗?,google-apps-script,google-sheets,google-forms,Google Apps Script,Google Sheets,Google Forms,我有一个Google表单,它将响应数据写入包含脚本的电子表格,该脚本应该将表单填充者的答案邮寄到表单中 我以前成功地使用了e.values,邮件生成得很好。现在似乎出现了一些问题,因为跳过了空答案,这意味着e.values[9]实际上变成了第9列,而不是过去的第10列(至少在2014年春季)。因此,如果有一个或多个字段留空,则根据留空问题的数量,以下答案在数组中向后移动一个或多个步骤。这样的行为会在精心策划的脚本中造成严重破坏 我还尝试使用namedvalue,但它也不能容忍空字段 有人知道附近

我有一个Google表单,它将响应数据写入包含脚本的电子表格,该脚本应该将表单填充者的答案邮寄到表单中

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

我还尝试使用
namedvalue
,但它也不能容忍空字段


有人知道附近有工作吗?我很感激你的想法。

可以证实这种行为,并且(在回答这个问题时)对此有一个明确的解释

至于解决方法,您能否处理namedValues对象以确定缺少哪些值?

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

这里有一个解决办法。也可在中找到

例子 fixFormEvent(e)
/**
*强制将空白响应插入事件对象的values属性,以便该值的索引
*正确反映问题顺序。(带有“新表格”+“新表格”,空白回复)
*在事件对象中跳过。
*
*看http://stackoverflow.com/a/26975968/1677912
*
*@param{event}e事件作为电子表格表单对象接收。事件的值
*属性将被此函数修改。
*@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”];

对于(var i=0;i而不是
e.values
使用
e.range.getValues().flat()

e.range.getValues()
将返回一个数组,其中包含一个数组,该数组的响应值记录在电子表格中)

Array.prototype.flat()
将在“一维”数组中转换先前的二维数组

资源

function onFormSubmit(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
}