Android Google Drive API - Generate new spreadsheet file with custom data

I’m building an Android application that lets users pick items from a list and I need to export this selected data into a new Google Sheets file on their Drive. I’m pretty new to working with Google’s Drive API so I’m struggling with the implementation details.

I managed to get the basic folder selection working by following some online examples, but now I’m stuck on how to actually push the data into a spreadsheet format. Here’s the callback method I’m currently using:

final ResultCallback<ContentsResult> dataCallback = new ResultCallback<ContentsResult>() {
    @Override
    public void onResult(ContentsResult response) {
        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                .setMimeType("text/html").build();
        IntentSender sender = Drive.DriveApi
                .newCreateFileActivityBuilder()
                .setInitialMetadata(changeSet)
                .setInitialContents(response.getContents())
                .build(getGoogleApiClient());
        try {
            startIntentSenderForResult(
                    sender, FILE_CREATE_REQUEST, null, 0, 0, 0);
        } catch (SendIntentException ex) {
            Log.e(TAG, "Failed to send intent", ex);
        }
    }
};

Should I be putting my actual data into the metadata somehow? My goal is to write to specific cells and worksheets in the spreadsheet but I can’t figure out the right approach. Any guidance would be helpful.

Looking at your code, I think you’re mixing up the Drive API with what you actually need for spreadsheet creation. The Drive API is mainly for file management, but for creating and populating Google Sheets with actual data, you’ll want to use the Google Sheets API alongside it.

What worked for me was first creating an empty spreadsheet using the Sheets API’s spreadsheets().create() method, then using the spreadsheets().values().update() method to populate it with your selected items data. You’ll need to format your data as a ValueRange object with your rows and columns properly structured.

The approach you’re showing with Drive API is more suited for uploading existing files rather than creating structured spreadsheet data. I’d recommend checking out the Google Sheets API documentation specifically - it has better examples for programmatically writing data to specific cells and worksheets which sounds like exactly what you need.

you’re setting mimetype to text/html but for sheets it should be ‘application/vnd.google-apps.spreadsheet’. also that callback looks like the old drive api - google deprecated it awhile back. i’d suggest switching to the newer rest api or using the sheets api directly like the other guy mentioned. way easier to work with imo

Had similar issues when I first tackled this problem. The main thing you’re missing is that you need to use both APIs together - Drive API for the file creation and Sheets API for data manipulation. Your current approach with ContentsResult won’t work for structured spreadsheet data.

What I ended up doing was using the Sheets API’s batchUpdate method to create the spreadsheet with proper formatting first, then using values().batchUpdate() to insert my data in one go. Much more efficient than individual cell updates. You’ll need to convert your selected items into a 2D array format that matches the spreadsheet structure.

Also worth noting that the Google API client setup requires both Drive and Sheets scopes in your authentication. The documentation examples usually show them separately which can be confusing when you need both services working together.