简体   繁体   English

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

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

I'm currently working on an app where the user can pick a Google Sheet file (.xlxs) from their Google Drive. 我目前正在开发一个应用程序,用户可以从其Google云端硬盘中选择一个Google表格文件(.xlxs)。 My app will then extract and work with certain content of that Sheet. 然后,我的应用程序将提取并处理该工作表的某些内容。

I'm using the Google Drive API made for Android and I utilize the example classes from Google Drive Android Demos 我使用的是专为Android设计的Google Drive API,并利用了Google Drive Android Demos中的示例类

So far, I've implemented a Drive file picker where the user's Drive files are showing. 到目前为止,我已经实现了一个Drive文件选择器,用于显示用户的Drive文件。 Picking a file returns the ID for that file. 选择一个文件将返回该文件的ID。


The first problem is that I can't seem to set the mime type so only xlsl files are pickable. 第一个问题是我似乎无法设置mime类型,因此只能选择xlsl文件。 Google searches tell me that Google搜索告诉我

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application / vnd.openxmlformats-officedocument.spreadsheetml.sheet

is the correct mime type. 是正确的哑剧类型。

I use it in this context 我在这种情况下使用它

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

I haven't encountered mime types before, but "text/plain" as a meme type seems to work, and they are found in the same lists of mime types out there.. 我以前没有遇到过mime类型,但是“ text / plain”作为meme类型似乎有效,并且可以在相同的mime类型列表中找到它们。

The second problem basically concerns how I should go about extract Strings from certain columns and rows in my sheet. 第二个问题基本上与我应该如何从工作表中的某些列和行中提取字符串有关。

This is the code that deals with the contents of the chosen file (may be modified of course, just don't know how). 这是处理所选文件内容的代码(当然可以修改,只是不知道如何)。

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;
    }

So I googled and looked around the community for a while and have these suggestions to resolve your issues: 因此,我用Google搜索并在社区中转了一圈,并提出了以下建议来解决您的问题:

First Problem : 第一个问题

Since you are just aiming to edit Sheets, why not just use setSelectionFilter instead? 由于您只是想编辑表格,为什么不只使用setSelectionFilter呢? It limits the files displayed on the picker to the specified file type. 它将选择器上显示的文件限制为指定的文件类型。

For the second problem: 对于第二个问题:

I think in order to play around(update) with Google sheets, you need to use the Sheets API as per this answer on a similar post : 我认为为了在Google表格中玩转(更新),您需要在类似的帖子上按照此答案使用Sheets API

"The Google Spreadsheet Api allows to make complex operations in spreadsheets, like accessing data by row and column ." “ Google Spreadsheet Api允许在电子表格中进行复杂的操作, 例如按行和列访问数据 。”

Hope this helps you somehow, at the very least gave you an idea what to tackle next. 希望这能以某种方式对您有所帮助,至少至少让您知道接下来要解决的问题。 Good luck. 祝好运。 :) :)

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

相关问题 如何使用Java和Drive API在Google Drive上创建电子表格 - How to create a Spread Sheet on Google Drive Using Java and Drive API 如何使用 Google Drive Java API v3 复制 Google Drive 上的文件? - How do I copy a file on a Google Drive using v3 of the Google Drive Java API? 如何使用Google Drive Android API删除Google Drive上的文件 - How to delete a file on google drive using Google Drive Android API 如何使用Java API在Google驱动器中上传Excel工作表 - how to upload an excel sheet in google drive using java api 如何在Android上设置Google Drive API? - How to set up Google Drive API on android? 如何通过Google V4 Java API获取工作表的列表 - How to get list of Sheet's via Google V4 Java API 如何在Amazon S3中读取文件的内容 - How do I read the content of a file in Amazon S3 如何仅使用Google Android应用中的Google Drive API在Google云端硬盘上创建和编辑电子表格文件? - How do I create & edit a spreadsheet file on google drive using Google Drive APIs only from my Android app? 如何从 Java 中的 Google Drive API 获取 Gdoc 内容? - How to get Gdoc content from Google Drive API in Java? 谷歌驱动器配额使用android api驱动器 - google drive quota using android api for drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM