2017-04-11 80 views
11

我遵循下面的「Android快速入門」。按名稱查找Google Drive Sheet按名稱使用API​​ 4在Android中

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

的偉大工程。

但是該示例將電子表格ID硬編碼到現有電子表格。

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(); 
    } 
} 

提前很多感謝!

+0

你有沒有試過在這[SO線索]提供的解決方案(http://stackoverflow.com/questions/38123383/how-to-get-sheet-名稱的個體表型 - 谷歌的電子表格-谷歌片)?他們使用了service.spreadsheets()。get(spreadsheetId).setIncludeGridData(false) .execute();然後使用getSheets()函數解析響應。 – noogui

+0

@noogui我沒有spreadsheetId可以通過。我討厭創建一張表來尋找工作表。感謝壽。 – LimaNightHawk

+0

你在做什麼甚至沒有在文檔中提到lol – noogui

回答

3

看起來這不能用Sheets API v4完成。

但是......它看起來好像可以通過兼容的Google Drive 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(); 
+1

我跟着你的代碼示例,但我得到403,權限不足錯誤消息。我已經實現了GoogleSheets API,該代碼在此代碼更改之前可用。 – Manikandan

+0

啊,是的!我敢打賭,你缺乏正確的範圍。你可能需要'DRIVE_METADATA_READONLY'範圍。我會更新答案以包含此內容。(我忘了這是我在開始工作時所做的其中一個步驟。) – LimaNightHawk

+1

使用DriveScopes更改範圍有效。 – Manikandan

2

它看起來對我像你混淆SpreadsheetSheet對象:

  • Spreadsheet是帶有Id的文件,可通過URL訪問,例如https://docs.google.com/spreadsheets/d/yourSpreadsheetId;它是存儲在Google雲端硬盤文件夾中的文檔(如Excel工作簿)。

  • a SheetSpreadsheet中的選項卡,其中包含單元格,就像工作簿中的頁面。它沒有Id,也沒有直接的URL。 實際上,API v4 reference表明表格中也包含Ids,但足以讓人混淆的是,這些僅僅是您無法在Google Apps腳本中訪問的參考編號,並且不會幫助您解決問題

因此,您無法直接在電子表格中訪問範圍,也無法找到按工作表編號的工作表。

同樣夠你的答案,這裏是我的建議:

_通過雲端硬盤文件瀏覽(通過mimeType == "application/vnd.google-apps.spreadsheet"過濾器)

__For每個電子表格文件,瀏覽其表(spreadsheet.sheets[]

___對於當前電子表格中的每張表格,通過查看其名稱(sheet.title

檢查它是否是一個表格。一旦找到正確的表格, n獲取其包含的電子表格的標識(spreadsheet.spreadsheetId)。

一兩件事:在你的答案的底部,你寫 ValueRange response = sheetsService.spreadsheets().values().get(spreadsheetId, "A1:B2").execute(); 您將無法從電子表格訪問單元格區域沒有先指定表含的範圍內:所以不是"A1:B2",你會需要"mySheetTitle!A1:B2"

我希望這是在清理混亂:)

+1

已更新的問題更清楚地顯示「工作表」與「電子表格」。關於mimeType的大提示(更新後的答案將其作爲搜索條件)。最後,您不需要範圍中的表限定符。範圍設置來自測試代碼和作品。 – LimaNightHawk

+0

如果電子表格只包含一張紙,那麼我相信它的工作原理,而不需要指定範圍內的紙張;但是,如果電子表格包含多個表單(這可能是最常見的情況),那麼它不知道要從哪個表單中進行選擇。 – flo5783