【问题标题】:Google sheets - range.getColumnIndex() not working?谷歌表 - range.getColumnIndex() 不起作用?
【发布时间】:2020-12-08 20:18:28
【问题描述】:

我使用的是这个代码:https://gist.github.com/pamelafox/1878143

一切都适用于行:

// getRowsData iterates row by row in the input range and returns an array of objects.
// Each object contains all the data for a given row, indexed by its normalized column name.
// Arguments:
//   - sheet: the sheet object that contains the data to be processed
//   - range: the exact range of cells where the data is stored
//   - columnHeadersRowIndex: specifies the row number where the column names are stored.
//       This argument is optional and it defaults to the row immediately above range; 
// Returns an Array of objects.
function getRowsData_(sheet, options) {
  var headersRange = sheet.getRange(1, 1, sheet.getFrozenRows(), sheet.getMaxColumns());
  var headers = headersRange.getValues()[0];
  var dataRange = sheet.getRange(sheet.getFrozenRows()+1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
  var objects = getObjects_(dataRange.getValues(), normalizeHeaders_(headers));
  if (options.structure == STRUCTURE_HASH) {
    var objectsById = {};
    objects.forEach(function(object) {
      objectsById[object.id] = object;
    });
    return objectsById;
  } else {
    return objects;
  }
}

但是当我执行以下公式时,出现以下错误:TypeError: range.getColumnIndex is not a function

// getColumnsData iterates column by column in the input range and returns an array of objects.
// Each object contains all the data for a given column, indexed by its normalized row name.
// Arguments:
//   - sheet: the sheet object that contains the data to be processed
//   - range: the exact range of cells where the data is stored
//   - rowHeadersColumnIndex: specifies the column number where the row names are stored.
//       This argument is optional and it defaults to the column immediately left of the range; 
// Returns an Array of objects.
function getColumnsData_(sheet, range, rowHeadersColumnIndex) {
  rowHeadersColumnIndex = rowHeadersColumnIndex || range.getColumnIndex() - 1;
  var headersTmp = sheet.getRange(range.getRow(), rowHeadersColumnIndex, range.getNumRows(), 1).getValues();
  var headers = normalizeHeaders_(arrayTranspose_(headersTmp)[0]);
  return getObjects(arrayTranspose_(range.getValues()), headers);
}

【问题讨论】:

  • 我在第二个代码 sn-p 中没有看到getLastColumn。还始终提供有关如何使用函数的详细信息。你在这里传递什么论据?解释你的目标是什么以及哪一部分完全不起作用等总是很好的。还有你在任何地方定义的功能。什么是normalizeHeaders_,什么是getObjects,为什么不用它?
  • 糟糕,抱歉打错了。我的最终目标是从我的谷歌表中创建一个 json,其中标题位于第一列。
  • 查看我的更新答案。您需要使用getColumn() 而不是getColumnIndex()

标签: google-apps-script google-sheets


【解决方案1】:

根据您编辑的问题进行更新:

getColumnIndex() 不是range 对象的函数。

你想改用的是:

getColumn()

示例:

function getColumnsData_(sheet, range, rowHeadersColumnIndex) {
  rowHeadersColumnIndex = rowHeadersColumnIndex || range.getColumn() - 1;
  var headersTmp = sheet.getRange(range.getRow(), rowHeadersColumnIndex, range.getNumRows(), 1).getValues();
  var headers = normalizeHeaders_(arrayTranspose_(headersTmp)[0]);
  return getObjects(arrayTranspose_(range.getValues()), headers);
}

函数使用示例:

根据文档的解释,您应该像这样定义参数:

function myFunction(){
  const sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  const range = sheet.getRange('A1:A5');
  const rowHeadersColumnIndex = 2;
  getColumnsData_(sheet, range, rowHeadersColumnIndex)
}

根据需要修改参数。

为了使上述函数起作用,您需要定义其他必要的函数:normalizeHeaders_getObjects 但我假设您已经完成了。

【讨论】:

    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 2015-09-28
    • 2016-07-03
    相关资源
    最近更新 更多