[英]How to get spreadsheets list from specific collection from Google docs in Java
[英]How to get spreadsheets from a specific Google Drive folder?
本教程中提供的代碼(下面給出的代碼段)為經過身份驗證的用戶檢索所有電子表格的列表。
public class MySpreadsheetIntegration {
public static void main(String[] args) throws AuthenticationException,
MalformedURLException, IOException, ServiceException {
SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
// TODO: Authorize the service object for a specific user (see other sections)
// Define the URL to request. This should never change.
URL SPREADSHEET_FEED_URL = new URL(
"https://spreadsheets.google.com/feeds/spreadsheets/private/full");
// Make a request to the API and get all spreadsheets.
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,
SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
// Iterate through all of the spreadsheets returned
for (SpreadsheetEntry spreadsheet : spreadsheets) {
// Print the title of this spreadsheet to the screen
System.out.println(spreadsheet.getTitle().getPlainText());
}
}
}
但我不想得到所有的電子表格。 我只想獲取特定文件夾中的那些電子表格(如果該文件夾存在,否則終止程序)。 是否可以使用此 API? 如果是,如何?
就我的理解而言,SpreadsheetFeed 必須更改。 但我沒有得到任何針對它的示例片段。
我制定了解決方案如下:
首先,獲取該特定文件夾的 fileId。 使用setQ()
通過查詢檢查文件夾和文件夾名稱。 以下代碼段將很有用:
result = driveService.files().list()
.setQ("mimeType='application/vnd.google-apps.folder'
AND title='" + folderName + "'")
.setPageToken(pageToken)
.execute();
然后,獲取該特定文件夾中的文件列表。 我從本教程中找到了它。 片段如下:
private static void printFilesInFolder(Drive service, String folderId) throws IOException {
Children.List request = service.children().list(folderId);
do {
try {
ChildList children = request.execute();
for (ChildReference child : children.getItems()) {
System.out.println("File Id: " + child.getId());
}
request.setPageToken(children.getNextPageToken());
} catch (IOException e) {
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
} while (request.getPageToken() != null &&
request.getPageToken().length() > 0);
}
最后,檢查電子表格並為它們獲取工作表提要。 以下代碼段可能會有所幫助。
URL WORKSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/worksheets/" + fileId + "/private/full");
WorksheetFeed feed = service.getFeed(WORKSHEET_FEED_URL, WorksheetFeed.class);
worksheets = feed.getEntries();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.