gsheets 0.1.0-rc.1 gsheets: ^0.1.0-rc.1 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": "https://www.googleapis.com/robot/v1/metadata/x509/telegram%40marenkovproject.iam.gserviceaccount.com"
}
''';
// your spreadsheet id
const _spreadsheetId = '';
main() async {
// init GSheets
final gsheets = GSheets(_credentials);
// fetch spreadsheet by its id
final ss = await gsheets.spreadsheet(_spreadsheetId);
// get worksheet by title
Worksheet sheet = await ss.worksheetByTitle('example');
if (sheet == null) {
// create worksheet if does not exist yet
sheet = await ss.addWorksheet('example');
}
// 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'
final thirdColumn = {
'0': '1',
'1': '2',
'2': '3',
'3': '4',
'4': '5',
};
await sheet.values.map.insertColumn(3, thirdColumn);
// 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'
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
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());
}