Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading a CSV file as Google Sheet using Android SDK

I have an Android app that allows users to export a .csv file to Google Drive so that they can edit it and then reimport the file into Android.

This used to work perfectly with the old Google Docs api's. I upgraded from that old API to an early version of Google Drive last year and was able to get it working well enough. Now, when I upgraded to the latest version, this feature of my app is effectively broken. I think it's a combination of the SDK permissions and breaking out Google Sheets from Google Drive.

What happens is I upload a file with this meta data:

    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                                        .setTitle(fileName)
                                                        .setMimeType("text/csv")
                                                        .setStarred(true)
                                                        .build();

The uploaded file then has a blue Google Docs icon. When a user access Google Drive to edit it they can only "preview" the document. While they are previewing it they can "Open" the document using Google Sheets which then creates a new document (this new document has the Green Google Sheets icon) and since Google SDK has this new "Feature" (quoted from: https://developers.google.com/drive/android/queries ):

Note: The Android Drive API only works with the https://www.googleapis.com/auth/drive.file scope. This means that only files which a user has opened or created with your application can be matched by a query.

The new file has can not be seen by my, so the user's edits can't be retrieved.

Is there someway I can upload it directly as a Google Sheets file? Or maybe another solution I've missed entirely?

Edits

Here is an example on how I am uploading my code using the Google Drive SDK:

In the constructor of my AsyncTask I create the google client:

mGoogleApiClient = new GoogleApiClient.Builder(activity)
                            .addApi(Drive.API)
                            .addScope(Drive.SCOPE_FILE)
                            .addConnectionCallbacks(this)
                            .addOnConnectionFailedListener(this)
                            .build();
mGoogleApiClient.connect();

Then in the doInBackground method I do something like this:

DriveApi.DriveContentsResult cResult = Drive.DriveApi.newDriveContents(mGoogleApiClient).await();

OutputStream os = cResult.getDriveContents().getOutputStream();
InputStream is = new FileInputStream(f);

//write the input stream to the output stream
....

DriveFileResult exportResult = Drive.DriveApi.getRootFolder(mGoogleApiClient)
                                                             .createFile(mGoogleApiClient, changeSet, cResult.getDriveContents())
                                                             .await();

It's pretty simple (once you figure it out) there just doesn't seem to be away to tell the SDK that I want this document to be a "Google Sheet" instead of a "Google Doc"

Thanks!

-Aaron

like image 634
Aaron Avatar asked Jul 12 '26 20:07

Aaron


1 Answers

You should convert the original explicitly into a Spreadsheet, otherwise it will end up as a straight upload into your Drive account. Try the following code:

java.io.File fileContent = new java.io.File(csvfilepath);
FileContent mediaContent = new FileContent("text/csv", fileContent);

// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/csv");

Insert request = service.files().insert(body, mediaContent);
request.setConvert(true);
File file = request.execute();
like image 151
Rivero Avatar answered Jul 14 '26 10:07

Rivero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!