columnByKey method

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

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 (values 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 values starting from fromRow will be returned

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

Returns column as Future Map of String to String.

Throws GSheetsException.

Implementation

Future<Map<String, String>?> columnByKey(
  Object key, {
  int fromRow = 2,
  int length = -1,
  dynamic mapTo,
}) async {
  final cKey = parseKey(key);
  final mKey = parseStringOrNull(mapTo);
  checkIndex('fromRow', fromRow);
  checkMapTo(cKey, mKey);
  final columns = await _values.allColumns();
  if (columns.isEmpty) return null;
  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 _wrap(keys, values);
}