insertColumnByKey method
Updates column values with values from map
by column names.
Expands current sheet's size if inserting range is out of sheet's bounds.
The first row considered to be column names
map
- map containing values to insert (not null nor empty)
key
- name of a requested column (values of returned map)
fromRow
- optional (defaults to 2), row index for the first inserted value,
rows start at index 1
mapTo
- optional, name of a column to which keys of the map
will be
mapped to, if mapTo
is null
then values will be mapped to column A
appendMissing
- optional (defaults to false
), whether keys of map
(with its related values) that are not present in a mapTo
column
should be added
overwrite
- optional (defaults to false
), whether clear cells of
key
column if map
does not contain value for them
eager
- optional (defaults to true
), whether to add key
if absent
Returns Future true
in case of success.
Throws GSheetsException.
Implementation
Future<bool> insertColumnByKey(
Object key,
Map<String, dynamic> map, {
int fromRow = 2,
dynamic mapTo,
bool appendMissing = false,
bool overwrite = false,
bool eager = true,
}) async {
final cKey = parseKey(key);
final mKey = parseStringOrNull(mapTo);
checkIndex('fromRow', fromRow);
checkMapTo(cKey, mapTo);
checkMap(map);
final columns = await _values.allColumns();
final mapToIndex = mKey.isNullOrEmpty ? 0 : whereFirst(columns, mKey!);
if (mapToIndex < 0) return false;
var columnIndex = whereFirst(columns, cKey);
if (columnIndex < 0) {
if (!eager || columns.isEmpty || columns.first.isEmpty) return false;
columnIndex = columns.length;
await _values._ws._update(
values: [cKey],
range: await _values._ws._columnRange(columnIndex + 1, 1, 1),
majorDimension: dimenColumns,
);
} else {
checkMapTo(columnIndex + 1, mapToIndex + 1);
}
final keys = get(columns, at: mapToIndex, or: <String>[])!;
return _insertColumns(
columnIndex + 1,
[map],
keys,
fromRow,
mapToIndex + 1,
appendMissing,
overwrite,
);
}