Skip to content
Advertisement

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. 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

So far, I’ve implemented a Drive file picker where the user’s Drive files are showing. Picking a file returns the ID for that file.


The first problem is that I can’t seem to set the mime type so only xlsl files are pickable. Google searches tell me that

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..

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

Advertisement

Answer

So I googled and looked around the community for a while and have these suggestions to resolve your issues:

First Problem:

Since you are just aiming to edit Sheets, why not just use setSelectionFilter instead? 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:

“The Google Spreadsheet Api allows to make complex operations in spreadsheets, like accessing data by row and column.”

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement