Lim*_*awk 11 android google-sheets google-spreadsheet-api
我按照下面的"Android快速入门"进行操作.
https://developers.google.com/sheets/api/quickstart/android
效果很好.
但该示例将电子表格ID硬编码为现有电子表格.
String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
Run Code Online (Sandbox Code Playgroud)
我需要能够按名称查找现有的电子表格,并存储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();
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢提前!
看来Sheets API v4无法做到这一点。
但是...看起来确实可以使用兼容的Google Drive API v3来完成。
注意:关于此解决方案的最好之处在于,我可以对两个API使用相同的身份验证和凭据收集方法。例如,一旦我有了获取证书的代码,就可以将其交替和连续地用于两个API。
这是我所做的:
将此添加到我的文件中build.gradle(显示在我的Sheets 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'
}
Run Code Online (Sandbox Code Playgroud)
我已经在使用该EasyPermissions方法来获取帐户和凭据。很好的例子。
然后...
import com.google.api.services.drive.Drive;
import com.google.api.services.sheets.v4.Sheets;
Run Code Online (Sandbox Code Playgroud)
...
private static final String[] SCOPES = { SheetsScopes.SPREADSHEETS, DriveScopes.DRIVE_METADATA_READONLY };
Run Code Online (Sandbox Code Playgroud)
...
credentials = GoogleAccountCredential.usingOAuth2(getApplicationContext(), Arrays.asList(SCOPES));
Run Code Online (Sandbox Code Playgroud)
...
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();
Run Code Online (Sandbox Code Playgroud)
...异步:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以直接使用Sheets API的ID:
ValueRange response = sheetsService.spreadsheets().values().get(spreadsheetId, "A1:B2").execute();
List<List<Object>> values = response.getValues();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1185 次 |
| 最近记录: |