columnByKey method

Future<Map<String, Cell>?> columnByKey(
  1. dynamic key, {
  2. int fromRow = 2,
  3. dynamic mapTo,
  4. int length = -1,
})

Fetches column by its name, maps it to other column and returns map.

Expands current sheet's size if requested range is out of sheet's bounds.

The first row considered to be column names

key - name of a requested column (values of returned map)

fromRow - optional (defaults to 1), index of a row that requested column starts from (cells before fromRow will be skipped), rows start at index 1

length - optional (defaults to -1), the length of a requested column if length is -1, all cells starting from fromRow will be returned

mapTo - optional, name of a column to map cells to (keys of returned map), if mapTo is null then cells will be mapped to column A, columns start at index 1 (column A)

Returns column as Future Map of String to Cell.

Throws GSheetsException.

Implementation

Future<Map<String, Cell>?> columnByKey(
  dynamic key, {
  int fromRow = 2,
  dynamic mapTo,
  int length = -1,
}) async {
  final cKey = parseKey(key);
  final mKey = parseStringOrNull(mapTo);
  checkIndex('fromRow', fromRow);
  checkMapTo(cKey, mKey);
  final columns = await _cells._ws.values.allColumns();
  final columnIndex = whereFirst(columns, cKey);
  if (columnIndex < 0) return null;
  final mapToIndex = mKey.isNullOrEmpty ? 0 : whereFirst(columns, mKey!);
  if (mapToIndex < 0) return null;
  checkMapTo(columnIndex + 1, mapToIndex + 1);
  final keys = extractSublist(
    columns[mapToIndex],
    from: fromRow - 1,
    length: length,
  );
  final values = extractSublist(
    columns[columnIndex],
    from: fromRow - 1,
    length: length,
  );
  return _wrapColumn(keys, values, columnIndex + 1, fromRow);
}