此tutorial中给出的代码(下面给出的代码段)会检索经过身份验证的用户的所有电子表格列表。
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。但我没有得到任何反对它的示例片段。
如果我没有遵守StackOverflow的规范或错过了我应该提及的细节,请回答或评论。
答案 0 :(得分:1)
我按如下方式制定了解决方案:
首先,获取该特定文件夹的fileId。使用setQ()
传递文件夹和文件夹名称的查询检查。以下代码段非常有用:
result = driveService.files().list()
.setQ("mimeType='application/vnd.google-apps.folder'
AND title='" + folderName + "'")
.setPageToken(pageToken)
.execute();
然后,获取该特定文件夹中的文件列表。我是从tutorial找到的。片段如下:
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);
}
最后,检查电子表格并获取它们的工作表Feed。以下代码段可能会有所帮助。
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();
如果有人找到更好的解决方案,请回答。或者,如果我的解决方案存在问题,请发表评论。