繁体   English   中英

如何通过Android的Drive API读取Google表格内容?

[英]How do I read Google Sheet content via Android's Drive API?

我目前正在开发一个应用程序,用户可以从其Google云端硬盘中选择一个Google表格文件(.xlxs)。 然后,我的应用程序将提取并处理该工作表的某些内容。

我使用的是专为Android设计的Google Drive API,并利用了Google Drive Android Demos中的示例类

到目前为止,我已经实现了一个Drive文件选择器,用于显示用户的Drive文件。 选择一个文件将返回该文件的ID。


第一个问题是我似乎无法设置mime类型,因此只能选择xlsl文件。 Google搜索告诉我

application / vnd.openxmlformats-officedocument.spreadsheetml.sheet

是正确的哑剧类型。

我在这种情况下使用它

IntentSender intentSender = Drive.DriveApi
            .newOpenFileActivityBuilder()
            .setMimeType(new String[]{"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})
            .build(getGoogleApiClient());

我以前没有遇到过mime类型,但是“ text / plain”作为meme类型似乎有效,并且可以在相同的mime类型列表中找到它们。

第二个问题基本上与我应该如何从工作表中的某些列和行中提取字符串有关。

这是处理所选文件内容的代码(当然可以修改,只是不知道如何)。

protected String doInBackgroundConnected(DriveId... params) {
        String contents = null;
        DriveFile file = params[0].asDriveFile();
        DriveContentsResult driveContentsResult =
                file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await();
        if (!driveContentsResult.getStatus().isSuccess()) {
            return null;
        }
        DriveContents driveContents = driveContentsResult.getDriveContents();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(driveContents.getInputStream()));
        StringBuilder builder = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            contents = builder.toString();
        } catch (IOException e) {
            Log.e(TAG, "IOException while reading from the stream", e);
        }

        driveContents.discard(getGoogleApiClient());
        return contents;
    }

因此,我用Google搜索并在社区中转了一圈,并提出了以下建议来解决您的问题:

第一个问题

由于您只是想编辑表格,为什么不只使用setSelectionFilter呢? 它将选择器上显示的文件限制为指定的文件类型。

对于第二个问题:

我认为为了在Google表格中玩转(更新),您需要在类似的帖子上按照此答案使用Sheets API

“ Google Spreadsheet Api允许在电子表格中进行复杂的操作, 例如按行和列访问数据 。”

希望这能以某种方式对您有所帮助,至少至少让您知道接下来要解决的问题。 祝好运。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM