【发布时间】: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