rowByKey method

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

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

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

Returns row as Future Map of String to String.

Throws GSheetsException.

Implementation

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