Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a Google Drive Sheet By Name Using API 4 In Android

I followed the "Android Quickstart" below.

https://developers.google.com/sheets/api/quickstart/android

Works great.

But the sample hard-codes a spreadsheetId to an existing spreadsheet.

String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";

I need to be able to find an existing spreadsheet, by name, and store the id (for later use).

I'd like to do something like this:

private com.google.api.services.sheets.v4.Sheets sheetsService = null;


HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

sheetsService = new com.google.api.services.sheets.v4.Sheets.Builder(
        transport, jsonFactory, credential)
        .setApplicationName("My Application Name")
        .build();

String spreadsheetId = null;
List<Spreadsheet> allSpreadsheets = sheetsService.spreadsheets().getAListOfAllSpreadsheets;
for (Spreadsheet spreadsheet : allSpreadsheets) {
    if (spreadsheet.getName().equals("My Sheet")){
        // found!
        spreadsheetId = spreadsheet.getId();
    }
}

Much thanks in advance!

like image 340
LimaNightHawk Avatar asked Apr 11 '17 12:04

LimaNightHawk


1 Answers

It looks like this cannot be done with Sheets API v4.

However...it does look like it can be done with the compatible Google Drive API v3.

Note: the best part about this solution was that I could use the same method of authentication and credential gathering for both APIs. E.g., once I had the code for getting the credentials, I could use it for both API's interchangeably and consecutively.

Here's what I did:

Added this to my build.gradle (shown below my Sheets API declaration)

compile('com.google.apis:google-api-services-sheets:v4-rev468-1.22.0') {
    exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-drive:v3-rev69-1.22.0') {
    exclude group: 'org.apache.httpcomponents'
}

I was already using the EasyPermissions method for getting account and credentials. Great example here.

Then...

import com.google.api.services.drive.Drive;
import com.google.api.services.sheets.v4.Sheets;

...

private static final String[] SCOPES = { SheetsScopes.SPREADSHEETS, DriveScopes.DRIVE_METADATA_READONLY };

...

credentials = GoogleAccountCredential.usingOAuth2(getApplicationContext(), Arrays.asList(SCOPES));

...

protected Drive driveService = new Drive.Builder(transport, jsonFactory, credential)
            .setApplicationName("My Application Name")
            .build();

protected Sheets sheetsService = new Sheets.Builder(transport, jsonFactory, credential)
            .setApplicationName("My Application Name")
            .build();

... async:

    Drive.Files.List request = driveService.files().list()
            .setPageSize(10)
            // Available Query parameters here:
            //https://developers.google.com/drive/v3/web/search-parameters
            .setQ("mimeType = 'application/vnd.google-apps.spreadsheet' and name contains 'smith' and trashed = false")
            .setFields("nextPageToken, files(id, name)");

    FileList result = request.execute();

    List<File> files = result.getFiles();
    String spreadsheetId = null;
    if (files != null) {
        for (File file : files) {

            // More code here to discriminate best result, if you want
            spreadsheetId = file.getId();
        }
    }

Then you can directly use the id for the Sheets API:

    ValueRange response = sheetsService.spreadsheets().values().get(spreadsheetId, "A1:B2").execute();
    List<List<Object>> values = response.getValues();
like image 160
LimaNightHawk Avatar answered Oct 07 '22 16:10

LimaNightHawk