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!

有帮助吗?

解决方案

You have two problems. I'm not sure about the second problem, but the first is that you are trying to use a cellFeedURL without the correct key, you are just using worksheetName, which is probably not correct. If you do something like this:

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

You will get the correct CellFeed. However, your second problem is that if you do it this way, all the CellEntry.getCell() in the CellFeed populate as null. I am not sure why, or if it can be solved while logged in as public/basic.

其他提示

The following code should work for your first issue. Probably the second issue is coming due to the query parameter in CellFeed. Make sure that the other dependent jars are available or not. I had worked on spreadsheet API long back. This might help you.

    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
            }
         }
        }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top