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

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

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

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

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

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

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

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

我已经实现了一些代码,但它不工作。下面是我想在我的 Android 应用程序中同步的我的电子表格公共(public) URL。

我正在按照 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();
}

最佳答案

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

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

关于android - 如何在 Android 应用程序中同步 Google 电子表格并从中读取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12363287/

相关文章:

google-drive-api - 使用 Google Docs API 从 Google Drive 插入图片

pdf - Google文档PDF查看器如何工作?

google-sheets - 如果是正数说是如果负数说不是

javascript - Google 脚本函数参数中需要引号吗?

java - 在 RecyclerView/ListView 中创建没有数据的 View

android - 当没有 Internet 连接时如何显示自定义对话框?

android - Broadcom NFC Android 堆栈中收发功能中的 1 字节响应问题

android - 使用 WebView 查看文档时如何隐藏 Google Doc 的下载选项?

javascript - 如何修复 "Service invoked too many times in a short time: exec qps."

未显示 Android 推送通知消息