2

教程中提供的代码(下面给出的代码片段)检索经过身份验证的用户的所有电子表格列表。

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 必须更改。但我没有得到任何反对它的示例片段。

4

1 回答 1

1

我制定了如下解决方案:

首先,获取该特定文件夹的 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();
于 2016-04-21T05:59:12.153 回答