在Android中使用API 4按名称查找Google云端硬盘工作表 [英] Find a Google Drive Sheet By Name Using API 4 In Android
问题描述
我遵循下面的"Android快速入门".
I followed the "Android Quickstart" below.
https://developers.google.com/sheets/api/quickstart/android
效果很好.
但是该示例将一个sheetsheetId硬编码为一个现有的电子表格.
But the sample hard-codes a spreadsheetId to an existing spreadsheet.
String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
我需要能够找到现有的电子表格,按名称 并存储ID (以备后用).
I need to be able to find an existing spreadsheet, by name, and store the id (for later use).
我想做这样的事情:
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();
}
}
非常感谢!
推荐答案
使用Sheets API v4似乎无法做到这一点.
It looks like this cannot be done with Sheets API v4.
但是...看起来确实可以通过兼容的Google Drive API v3来完成.
注意:关于此解决方案的最好之处在于,我可以对两个API使用相同的身份验证和凭据收集方法.例如,一旦我有了用于获取凭据的代码,就可以将其交替和连续地用于两个API.
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.
这就是我所做的:
将此添加到我的 build.gradle
(显示在我的Sheets API声明下方)
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'
}
我已经在使用 EasyPermissions
方法获取帐户和凭据.这里的例子很好.
I was already using the EasyPermissions
method for getting account and credentials. Great example here.
然后...
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();
...异步:
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();
}
}
然后,您可以直接为Sheets API使用ID:
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();
这篇关于在Android中使用API 4按名称查找Google云端硬盘工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!