Java 如何从特定的Google Drive文件夹获取电子表格?
本文提供的代码(下面给出的代码片段)检索经过身份验证的用户的所有电子表格的列表Java 如何从特定的Google Drive文件夹获取电子表格?,java,google-sheets,google-sheets-api,Java,Google Sheets,Google Sheets Api,本文提供的代码(下面给出的代码片段)检索经过身份验证的用户的所有电子表格的列表 public class MySpreadsheetIntegration { public static void main(String[] args) throws AuthenticationException, MalformedURLException, IOException, ServiceException { SpreadsheetService serv
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());
}
}
}
公共类MySpreadsheetIntegration{
公共静态void main(字符串[]args)引发AuthenticationException,
MalformedURLException、IOException、ServiceException{
电子表格服务=新的电子表格服务(“MySpreadsheetIntegration-v1”);
//TODO:为特定用户授权服务对象(请参阅其他部分)
//定义要请求的URL。这永远不会更改。
URL电子表格\u提要\u URL=新URL(
"https://spreadsheets.google.com/feeds/spreadsheets/private/full");
//向API发出请求并获取所有电子表格。
SpreadsheetFeed=service.getFeed(电子表格\u feed\u URL,
电子表格feed.class);
列表电子表格=feed.getEntries();
//遍历返回的所有电子表格
用于(电子表格输入电子表格:电子表格){
//将此电子表格的标题打印到屏幕上
System.out.println(电子表格.getTitle().getPlainText());
}
}
}
但我不想得到所有的电子表格。我只想获取特定文件夹中的电子表格(如果该文件夹存在,则终止该程序)。是否可以使用此API?如果是,如何进行
据我所知,电子表格提要必须更改。但我没有收到任何反对它的示例片段。我制定了如下解决方案:
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);
}
首先,获取该特定文件夹的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();
我认为电子表格API甚至没有“文件夹”的概念。你需要从驱动程序API。这将返回文件列表,而不是电子表格列表;但是URL包含电子表格ID,所以您可以尝试通过这种方式访问它们。。。或者改用应用程序脚本API,它提供了一种干净的方式将文件对象传递给SpreadsheetApp,返回一个电子表格。@Meta好的,我明白了。但我必须先拿到文件夹。另外,我可以从fileId获取电子表格条目吗?