3

My aim is to retrieve CellFeeds from Google spreadsheet URLs without authentication. I tried it with the following spreadsheet URL (published to web): https://docs.google.com/spreadsheet/ccc?key=0AvNWoDP9TASIdERsbFRnNXdsN2x4MXMxUmlyY0g3VUE&usp=sharing

This URL is stored in variable "spreadsheetName". First attempt was to take the whole URL as argument for Service.getFeed().
url = new URL(spreadsheetName); WorksheetFeed feed = service.getFeed(url, WorksheetFeed.class);

But then I ran into following exception :

com.google.gdata.util.RedirectRequiredException: Found
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved

Second attempt was to build the URL with the key from the origin URL, using FeedURLFactory:

String key = spreadsheetName.split("key=")[1].substring(0, 44);
url = FeedURLFactory.getDefault().getCellFeedUrl(key,
                    worksheetName, "public", "basic");
WorksheetFeed feed = service.getFeed(url, WorksheetFeed.class);

...and I got the next exception:

com.google.gdata.util.InvalidEntryException: Bad Request
Invalid query parameter value for grid-id.

Do you have any ideas what I did wrong or is there anybody who successfully retrieved data from spreadsheet URLs without authentication? Thx in advance!

4

2 回答 2

1

以下代码应该适用于您的第一个问题。由于 CellFeed 中的查询参数,可能会出现第二个问题。确保其他依赖 jar 可用或不可用。我很久以前就研究过电子表格 API。这可能会帮助你。

    import java.net.URL;
    import java.lang.*;
    import java.util.List;
    import com.google.gdata.client.spreadsheet.FeedURLFactory;
    import com.google.gdata.client.spreadsheet.ListQuery;
    import com.google.gdata.client.spreadsheet.SpreadsheetService;
    import com.google.gdata.data.spreadsheet.CustomElementCollection;
    import com.google.gdata.data.spreadsheet.ListEntry;
    import com.google.gdata.data.spreadsheet.ListFeed;
    import com.google.gdata.data.spreadsheet.WorksheetEntry;
    import com.google.gdata.data.spreadsheet.WorksheetFeed;

    public class SpreadsheetsDemo {
        public static void main(String[] args) throws Exception{
            String application = "SpreadsheetsDemo";
            String key = "0AvNWoDP9TASIdERsbFRnNXdsN2x4MXMxUmlyY0g3VUE";

            SpreadsheetService service = new SpreadsheetService(application);

            URL url = FeedURLFactory.getDefault().getWorksheetFeedUrl(key, "public", "basic");

            WorksheetFeed feed = service.getFeed(url, WorksheetFeed.class);
            List<WorksheetEntry> worksheetList = feed.getEntries();
            WorksheetEntry worksheetEntry = worksheetList.get(0);
            CellQuery cellQuery = new CellQuery(worksheetEntry.CellFeedLink);
            CellFeed cellFeed = service.Query(cellQuery);

            foreach (CellEntry cell in cellFeed.Entries)
            {
                //Iterate through the columns and rows
            }
         }
        }
于 2013-09-13T22:16:18.677 回答
1

你有两个问题。我不确定第二个问题,但第一个是你试图使用 acellFeedURL没有正确的密钥,你只是在使用worksheetName,这可能是不正确的。如果你做这样的事情:

public static void main(String... args) throws MalformedURLException, ServiceException, IOException {
    SpreadsheetService service = new SpreadsheetService("Test");
    FeedURLFactory fact = FeedURLFactory.getDefault();

    String key = "0AvNWoDP9TASIdERsbFRnNXdsN2x4MXMxUmlyY0g3VUE";
    URL spreadSheetUrl = fact.getWorksheetFeedUrl(key, "public", "basic");
    WorksheetFeed feed = service.getFeed(spreadSheetUrl,
            WorksheetFeed.class);

    WorksheetEntry entry = feed.getEntries().get(0);
    URL cellFeedURL = entry.getCellFeedUrl();
    CellFeed cellFeed = service.getFeed(cellFeedURL, CellFeed.class);
}

你会得到正确的CellFeed。但是,您的第二个问题是,如果您这样做,则所有CellEntry.getCell()填充CellFeednull. 我不确定为什么,或者是否可以在以public/basic.

于 2013-09-11T16:23:06.780 回答