【问题标题】:How to sync Google Spreadsheet in Android app and read rows from it如何在 Android 应用中同步 Google 电子表格并从中读取行
【发布时间】:2012-09-11 05:32:10
【问题描述】:

我的 Google Drive 中有一个电子表格文件,我想从 Android 应用程序同步它。成功同步后,我的应用程序中有一些搜索条件,我希望从我同步的文件中获得基于此条件的结果。我是第一次这样做,并没有获得好的资源。

1) 如何在 Android App 中同步 Google Drive 文件?

2) 如何从我已同步的电子表格中检索特定数据?

注意 - 我也对 Google Docs API 和 Google Drive SDK 感到困惑。

在我的应用程序中使用什么?

我每次只有一个文件要同步,而不是所有文件。并且该文件通过私钥对所有人开放。

我已经实现了一些代码,但它不起作用。下面是我想在我的 Android 应用中同步的电子表格公共网址。

我正在按照here 的步骤进行操作。

My Test Spreadsheet URL

try {
    String USERNAME = "xxxxx";
    String PASSWORD = "xxxxx";

    SpreadsheetService service =
        new SpreadsheetService("Testing");
    service.setUserCredentials(USERNAME, PASSWORD);
    // TODO: Authorize the service object for a specific user (see other sections)

    // Define the URL to request.  This should never change.
    URL SPREADSHEET_FEED_URL = new URL(
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

    // Make a request to the API and get all spreadsheets.
    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,
            SpreadsheetFeed.class);
    List<SpreadsheetEntry> spreadsheets = feed.getEntries();

    if (spreadsheets.size() == 0) {
        // TODO: There were no spreadsheets, act accordingly.
    }

    // TODO: Choose a spreadsheet more intelligently based on your
    // app's needs.
    SpreadsheetEntry spreadsheet = spreadsheets.get(0);
    System.out.println(spreadsheet.getTitle().getPlainText());

    // Make a request to the API to fetch information about all
    // worksheets in the spreadsheet.
    List<WorksheetEntry> worksheets = spreadsheet.getWorksheets();

    // Iterate through each worksheet in the spreadsheet.
    for (WorksheetEntry worksheet : worksheets) {
        // Get the worksheet's title, row count, and column count.
        String title = worksheet.getTitle().getPlainText();
        int rowCount = worksheet.getRowCount();
        int colCount = worksheet.getColCount();

        // Print the fetched information to the screen for this worksheet.
        System.out.println("\t" + title + "- rows:" + rowCount + " cols: " + colCount);
    }
} catch (Exception e) {
    e.printStackTrace();
}

【问题讨论】:

  • 如果有人有任何想法,请帮助我。我正在寻找建议...
  • 您是如何在 Android 应用中同步 Google Drive 文件的?

标签: android google-docs google-sheets google-spreadsheet-api


【解决方案1】:

我明白了。我没有添加权限,这就是它给我错误的原因。我们需要添加以下权限进行授权。

<uses-permission android:name="android.permission.ACCOUNT_MANAGER" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />

【讨论】:

  • 我不记得我现在使用的库..我必须从项目中检查它..给我一些时间我会更新你..但你可以看到上面的链接可能是你会找到答案...
  • 不,实际上我正在尝试阅读一个公开的电子表格,即使我们没有登录,我们也可以看到它,就像您提供了链接一样。但是您在您的代码中使用的电子表格服务,我无法访问。所以我问图书馆你必须在哪里使用这个类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多