使用 API 4 In Android 按名称查找 Google 驱动器 Sheet
Find a Google Drive Sheet By Name Using API 4 In Android
我按照下面的"Android Quickstart"。
https://developers.google.com/sheets/api/quickstart/android
效果很好。
但示例将 spreadsheetId 硬编码到现有电子表格。
String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
我需要能够找到现有的电子表格,按名称, 并存储 ID(供以后使用)。
我想做这样的事情:
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();
}
}
非常感谢!
表格 API v4.
似乎无法做到这一点
但是...看起来确实可以使用兼容的 Google 驱动器 API v3.
注意:此解决方案最好的部分是我可以对两个 API 使用相同的身份验证和凭据收集方法。例如,一旦我有了获取凭据的代码,我就可以将它用于两个 API 的互换和连续。
这是我所做的:
将此添加到我的 build.gradle
(显示在我的表格 API 声明下方)
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
方法获取帐户和凭据。 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();
}
}
那么你可以直接使用表格的id API:
ValueRange response = sheetsService.spreadsheets().values().get(spreadsheetId, "A1:B2").execute();
List<List<Object>> values = response.getValues();
在我看来你混淆了 Spreadsheet
和 Sheet
对象:
a Spreadsheet
是一个带有 Id 的文件,可通过 URL 访问,例如 https://docs.google.com/spreadsheets/d/yourSpreadsheetId ;它是存储在 Google Drive 文件夹中的文档(类似于 Excel 工作簿)。
a Sheet
是 Spreadsheet
中的一个选项卡,带有单元格,就像工作簿中的一个页面。它没有 Id,也没有直接 URL。 实际上 API v4 reference 表示 Sheet 也有 ID,但令人困惑的是,这些只是参考编号,您甚至无法在 Google Apps 脚本中访问,并且不会'解决不了你的问题.
因此您不能直接在 Spreadsheet 中访问范围,也不能通过 Id 找到 Sheet。
与您的回答类似,以下是我的建议:
_ 浏览您的云端硬盘文件(按 mimeType == "application/vnd.google-apps.spreadsheet"
过滤)
__For 每个 Spreadsheet 文件,浏览其 Sheets (spreadsheet.sheets[]
)
___For 当前 Spreadsheet 中的每个 Sheet,通过查看其名称来检查它是否是那个 (sheet.title
)
一旦找到正确的sheet,就可以得到它包含的Spreadsheet的Id (spreadsheet.spreadsheetId
).
还有一件事:在你答案的底部,你写了
ValueRange response = sheetsService.spreadsheets().values().get(spreadsheetId, "A1:B2").execute();
如果不首先指定包含范围的 Sheet,您将无法从 spreadsheet 访问单元格范围:因此您需要 "mySheetTitle!A1:B2"
而不是 "A1:B2"
。
我希望这能消除困惑:)
我按照下面的"Android Quickstart"。
https://developers.google.com/sheets/api/quickstart/android
效果很好。
但示例将 spreadsheetId 硬编码到现有电子表格。
String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
我需要能够找到现有的电子表格,按名称, 并存储 ID(供以后使用)。
我想做这样的事情:
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();
}
}
非常感谢!
表格 API v4.
似乎无法做到这一点但是...看起来确实可以使用兼容的 Google 驱动器 API v3.
注意:此解决方案最好的部分是我可以对两个 API 使用相同的身份验证和凭据收集方法。例如,一旦我有了获取凭据的代码,我就可以将它用于两个 API 的互换和连续。
这是我所做的:
将此添加到我的 build.gradle
(显示在我的表格 API 声明下方)
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
方法获取帐户和凭据。 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();
}
}
那么你可以直接使用表格的id API:
ValueRange response = sheetsService.spreadsheets().values().get(spreadsheetId, "A1:B2").execute();
List<List<Object>> values = response.getValues();
在我看来你混淆了 Spreadsheet
和 Sheet
对象:
a
Spreadsheet
是一个带有 Id 的文件,可通过 URL 访问,例如 https://docs.google.com/spreadsheets/d/yourSpreadsheetId ;它是存储在 Google Drive 文件夹中的文档(类似于 Excel 工作簿)。a
Sheet
是Spreadsheet
中的一个选项卡,带有单元格,就像工作簿中的一个页面。它没有 Id,也没有直接 URL。 实际上 API v4 reference 表示 Sheet 也有 ID,但令人困惑的是,这些只是参考编号,您甚至无法在 Google Apps 脚本中访问,并且不会'解决不了你的问题.
因此您不能直接在 Spreadsheet 中访问范围,也不能通过 Id 找到 Sheet。
与您的回答类似,以下是我的建议:
_ 浏览您的云端硬盘文件(按 mimeType == "application/vnd.google-apps.spreadsheet"
过滤)
__For 每个 Spreadsheet 文件,浏览其 Sheets (spreadsheet.sheets[]
)
___For 当前 Spreadsheet 中的每个 Sheet,通过查看其名称来检查它是否是那个 (sheet.title
)
一旦找到正确的sheet,就可以得到它包含的Spreadsheet的Id (spreadsheet.spreadsheetId
).
还有一件事:在你答案的底部,你写了
ValueRange response = sheetsService.spreadsheets().values().get(spreadsheetId, "A1:B2").execute();
如果不首先指定包含范围的 Sheet,您将无法从 spreadsheet 访问单元格范围:因此您需要 "mySheetTitle!A1:B2"
而不是 "A1:B2"
。
我希望这能消除困惑:)