Google表格脚本错误 - 无法将数组转换为对象[] []

时间:2016-02-05 13:28:06

标签: javascript google-sheets

我正在编写一个脚本来对字段中的数据进行编码以插入HTML(这些单元格包含PlanétariumRua João Bettega, 01等字词。我收到了一个错误,并且不知道如何处理它。错误显示"无法将数组转换为对象[] []"

我确实将一个双暗阵列传递给.setValues(),所以我知道不是它(发现这是一个常见的问题)。有任何想法吗?这是我的职责:

// columns[] and columns2encode[] are set in the scope above this function
function _encodeData() {
  var sheet = _getSheet();
  var data = sheet.getDataRange().getValues();
  var rowCnt = data.length;
  var colCnt = data[0].length;
  var data2set = new Array(rowCnt-1); // creates array w/ row.length indeces
  var colEncodeIndexList = [];
  var origVal, encodedVal, range, cell; // used later in for loop(s)

  toast('Start Encoding Data');

  // loop every row
  for(var ri = 0; ri < data.length; ri++) {
    if(ri !== 0) {
      data2set[ri] = new Array(colCnt); // creates array w/ length of ~ 29
    }

    // loop every cell (column entry in the given row)
    for(var ci = 0; ci < data[ri].length; ci++) {
      // get text content of current cell
      origVal = data[ri][ci];

      // if first row, headers - save matches in array for later
      if(ri === 0) { // I split the ifs so inArray isn't called EVERY time
        if(inArray(columns2encode, origVal)) { // add col header if "on the list"
          colEncodeIndexList.push(ci);
        }
      } else { // isn't the header row
        // if ! first row, values - col num must be in saved headers to encode OR it's a number
        if(!inArray(colEncodeIndexList, ci) || typeof origVal === 'number') {
          // just use origVal
          data2set[ri][ci] = origVal;
        } else {
          // use encodedVal
          encodedVal = _htmlEncode(origVal);

          data2set[ri][ci] = encodedVal;
        }
      } // else
    } // for (cells/cols)
  } // for (rows)

  // Now pass array of arrays (data) to setValues()
  var setRange = sheet.getRange(2, 1, (data.length-1), data[0].length);//.getA1Notation(); // starts at 2, 1 (row 2, col 1) to ignore column header
  setRange.setValues(data2set);

  toast('Finished Encoding Data');
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

问题是.example永远不会被初始化(它始终为NULL)。

解决问题的最简单方法是在输出到电子表格之前删除data2set [0]:

data2set[0]