如何在Android应用中同步Google电子表格并从中读取行

时间:2012-09-11 05:32:11

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

我的Google云端硬盘中有一个电子表格文件,我希望从Android应用中同步。成功同步后,我的应用程序中有一些搜索条件,我希望根据此条件从我同步的文件中获得结果。我这是第一次这样做,并没有获得良好的资源开始。

1)如何在Android应用中同步Google云端硬盘文件?

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

注意 - 我也很困惑 Google Docs API& Google云端硬盘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();
}

1 个答案:

答案 0 :(得分:3)

我明白了。我没有添加权限,这就是为什么它给我错误。我们需要在下面添加授权许可。

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