gsheets 0.2.4 gsheets: ^0.2.4 copied to clipboard
A library for working with Google Sheets API v4. Manage your spreadsheets with gsheets in Dart.
import 'package:gsheets/gsheets.dart';
// your google auth credentials
const _credentials = r'''
{
"type": "service_account",
"project_id": "",
"private_key_id": "",
"private_key": "",
"client_email": "",
"client_id": "",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": ""
}
''';
// your spreadsheet id
const _spreadsheetId = '';
void main() async {
// init GSheets
final gsheets = GSheets(_credentials);
// fetch spreadsheet by its id
final ss = await gsheets.spreadsheet(_spreadsheetId);
// get worksheet by its title
var sheet = ss.worksheetByTitle('example');
// create worksheet if it does not exist yet
sheet ??= await ss.addWorksheet('example');
// update cell at 'B2' by inserting string 'new'
await sheet.values.insertValue('new', column: 2, row: 2);
// prints 'new'
print(await sheet.values.value(column: 2, row: 2));
// get cell at 'B2' as Cell object
final cell = await sheet.cells.cell(column: 2, row: 2);
// prints 'new'
print(cell.value);
// update cell at 'B2' by inserting 'new2'
await cell.post('new2');
// prints 'new2'
print(cell.value);
// also prints 'new2'
print(await sheet.values.value(column: 2, row: 2));
// insert list in row #1
final firstRow = ['index', 'letter', 'number', 'label'];
await sheet.values.insertRow(1, firstRow);
// prints [index, letter, number, label]
print(await sheet.values.row(1));
// insert list in column 'A', starting from row #2
final firstColumn = ['0', '1', '2', '3', '4'];
await sheet.values.insertColumn(1, firstColumn, fromRow: 2);
// prints [0, 1, 2, 3, 4, 5]
print(await sheet.values.column(1, fromRow: 2));
// insert list into column named 'letter'
final secondColumn = ['a', 'b', 'c', 'd', 'e'];
await sheet.values.insertColumnByKey('letter', secondColumn);
// prints [a, b, c, d, e, f]
print(await sheet.values.columnByKey('letter'));
// insert map values into column 'C' mapping their keys to column 'A'
// order of map entries does not matter
final thirdColumn = {
'0': '1',
'1': '2',
'2': '3',
'3': '4',
'4': '5',
};
await sheet.values.map.insertColumn(3, thirdColumn, mapTo: 1);
// prints {index: number, 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6}
print(await sheet.values.map.column(3));
// insert map values into column named 'label' mapping their keys to column
// named 'letter'
// order of map entries does not matter
final fourthColumn = {
'a': 'a1',
'b': 'b2',
'c': 'c3',
'd': 'd4',
'e': 'e5',
};
await sheet.values.map.insertColumnByKey(
'label',
fourthColumn,
mapTo: 'letter',
);
// prints {a: a1, b: b2, c: c3, d: d4, e: e5, f: f6}
print(await sheet.values.map.columnByKey('label', mapTo: 'letter'));
// appends map values as new row at the end mapping their keys to row #1
// order of map entries does not matter
final secondRow = {
'index': '5',
'letter': 'f',
'number': '6',
'label': 'f6',
};
await sheet.values.map.appendRow(secondRow);
// prints {index: 5, letter: f, number: 6, label: f6}
print(await sheet.values.map.lastRow());
// get first row as List of Cell objects
final cellsRow = await sheet.cells.row(1);
// update each cell's value by adding char '_' at the beginning
cellsRow.forEach((cell) => cell.value = '_${cell.value}');
// actually updating sheets cells
await sheet.cells.insert(cellsRow);
// prints [_index, _letter, _number, _label]
print(await sheet.values.row(1));
}