【问题标题】:How to get spreadsheets from a specific Google Drive folder?如何从特定的 Google Drive 文件夹中获取电子表格?
【发布时间】:2016-04-20 11:41:58
【问题描述】:

tutorial 中提供的代码(下面给出了 sn-p)检索经过身份验证的用户的所有电子表格的列表。

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

【问题讨论】:

  • 我认为电子表格 API 甚至没有“文件夹”的概念。您需要来自 Drive API 的 children.list。这将返回文件列表,而不是电子表格;但是 URL 包含电子表格 ID,因此您可以尝试通过这种方式访问​​它们……或者只使用 Apps Script API,它提供了一种将 File 对象传递给 SpreadsheetApp 的简洁方法,返回电子表格。
  • @Meta 好的,我明白了。但我必须先得到文件夹。另外,我可以从 fileId 获取 SpreadsheetEntry 吗?

标签: java google-sheets google-sheets-api


【解决方案1】:

我的解决方法如下:

首先,获取该特定文件夹的 fileId。使用setQ() 通过查询检查文件夹和文件夹名称。以下 sn-p 将很有用:

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

最后,检查电子表格并获取它们的工作表提要。以下 sn-p 可能会有所帮助。

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

【讨论】:

    猜你喜欢
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 2014-10-30
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多