在Flutter / Dart中从Google表格中读取

时间:2019-06-03 02:50:26

标签: google-sheets flutter dart google-api google-sheets-api

我正在尝试在Flutter应用程序中向Google表格读写数据,但我不知道该怎么做。我找不到该问题的任何文档或示例。我对Flutter / Dart和Google API都还很陌生,所以我真的不知道自己在做什么。

任何帮助将不胜感激!非常感谢。

1 个答案:

答案 0 :(得分:0)

以下是使用 googleapis 包从 Google 表格读取电子表格的方法。请注意,这需要使用 googleapis_auth 包进行身份验证。

Future readSpreadSheet() async {
  final httpClient = await clientViaApplicationDefaultCredentials(scopes: [
    StorageApi.devstorageReadOnlyScope,
  ]);
  try {
    final spreadsheetApi = SheetsApi(httpClient);
    spreadsheetApi.spreadsheets.get(spreadsheetId).then((Spreadsheet spreadsheet) {
      // Handle Spreadsheet
      // https://pub.dev/documentation/googleapis/latest/sheets.v4/Spreadsheet-class.html
    }).onError((error, stackTrace) {
      debugPrint('$error: $stackTrace');
    });
  } finally {
      httpClient.close();
  }
}

要获取 spreadsheetId,您需要使用 googleapis 包中的 drive.v3 API。