Google Sheets API v4 for Flutter/Dart

送分小仙女□ 提交于 2019-11-29 11:28:41

https://pub.dartlang.org/packages/googleapis can be used in Flutter and supports Sheets v4

See also

Michael Davies

I created a project, enabled the sheets API for it, then created a 'robot-style' service account, and download the key as JSON. I then created a Google Sheet, and made it publicly accessible, noting its key.

This code connects, and updates an existing sheet by appending a new line of data. No error handling, but some print() statements to help visualise the flow.

// Key for service account copied from downloaded file for demo purposes ;-)
final _key = {
  "type": "service_account",
  "project_id": //etc
  // ...
  // ...
};

print('getting oauth');
auth
    .obtainAccessCredentialsViaServiceAccount(
        auth.ServiceAccountCredentials.fromJson(_key),
        scopes,
        http.Client())
    .then((auth.AccessCredentials cred) {
  print('got oauth');

  auth.AuthClient client = auth.authenticatedClient(http.Client(), cred);
  SheetsApi api = new SheetsApi(client);
  ValueRange vr = new ValueRange.fromJson({
    "values": [
      [ // fields A - J
        "15/02/2019", "via API 3", "5", "3", "3", "3", "3", "3", "3", "3"
      ]
    ]
  });
  print('about to append');
  api.spreadsheets.values
      .append(vr, '1cl...spreadsheet_key...W5E', 'A:J',
          valueInputOption: 'USER_ENTERED')
      .then((AppendValuesResponse r) {
    print('append completed.');
    client.close();
  });
  print('called append()');
});
print('ended?');

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!