rowByKey method
Fetches row by its name, maps it to other row, and returns map.
Expands current sheet's size if requested range is out of sheet's bounds.
The column A considered to be row names
key
- name of a requested row (values of returned map)
fromColumn
- optional (defaults to 1), index of a column that requested row
starts from (cells before fromColumn
will be skipped),
columns start at index 1 (column A)
length
- optional (defaults to -1), the length of a requested row
if length is -1
, all cells starting from fromColumn
will be returned
mapTo
- optional, name of a row to map cells to (keys of returned
map), if mapTo
is null
then cells will be mapped to first row,
rows start at index 1
Returns row as Future Map of String to Cell.
Throws GSheetsException.
Implementation
Future<Map<String, Cell>?> rowByKey(
dynamic key, {
int fromColumn = 2,
dynamic mapTo,
int length = -1,
}) async {
final rKey = parseKey(key);
final mKey = parseStringOrNull(mapTo);
checkIndex('fromColumn', fromColumn);
checkMapTo(key, mKey);
final rows = await _cells._ws.values.allRows();
final rowIndex = whereFirst(rows, rKey);
if (rowIndex < 0) return null;
final mapToIndex = mKey.isNullOrEmpty ? 0 : whereFirst(rows, mKey!);
if (mapToIndex < 0) return null;
checkMapTo(rowIndex + 1, mapToIndex + 1);
final keys = extractSublist(
rows[mapToIndex],
from: fromColumn - 1,
length: length,
);
final values = extractSublist(
rows[rowIndex],
from: fromColumn - 1,
length: length,
);
return _wrapRow(keys, values, rowIndex + 1, fromColumn);
}