1

I have this code where I am trying to access a spreadsheet that I have created.

public class ExpressionExample {

    private static URL cellFeedUrl;

    public static final String SPREADSHEET_URL = "https://spreadsheets.google.com/feeds/spreadsheets/private/full/1QWX-zOkBe36M7oC9sn6z8ZuccGt7Wg-IFwtynn379kM";

    public static void main(String[] args) throws Exception {

        File p12 = new File("key.p12");

        HttpTransport httpTransport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();

        String[] SCOPESArray = { SPREADSHEET_URL };
        final List SCOPES = Arrays.asList(SCOPESArray);
        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId("XXXX@developer.gserviceaccount.com")
                .setServiceAccountScopes(SCOPES)
                .setServiceAccountPrivateKeyFromP12File(p12).build();

        SpreadsheetService service = new SpreadsheetService("new data service ");

        service.setOAuth2Credentials(credential);

        URL metafeedUrl = new URL(SPREADSHEET_URL);

        SpreadsheetEntry spreadsheet = service.getEntry(metafeedUrl,
                SpreadsheetEntry.class);

        WorksheetEntry sheet = ((WorksheetEntry) spreadsheet.getWorksheets()
                .get(0));

        cellFeedUrl = spreadsheet.getWorksheets().get(0).getCellFeedUrl();

        CellEntry cellA1 = new CellEntry(1, 1, "3");
        CellEntry cellB1 = new CellEntry(1, 2, "high mech");

        String line = "=IF(A1<5,IF(B1={\"high mech\",\"low mech\",\"mid mech\"},10,IF(B1=\"electronic\",20,0)),IF(A1>=5,IF(B1=\"electronic\",40,0),0)) ";
        CellEntry cellc1 = new CellEntry(1, 3, line);

        service.insert(cellFeedUrl, cellA1);
        service.insert(cellFeedUrl, cellB1);
        service.insert(cellFeedUrl, cellc1);

        ExpressionExample e = new ExpressionExample();
        e.printCell(cellA1);
        e.printCell(cellB1);
        e.printCell(cellc1);

        CellQuery query = new CellQuery(cellFeedUrl);
        query.setMinimumRow(1);
        query.setMaximumRow(1);
        query.setMinimumCol(3);
        query.setMaximumCol(3);

        CellFeed feed = service.query(query, CellFeed.class);

        System.out.println(feed.getEntries().indexOf(cellc1));
        for (CellEntry entry : feed.getEntries()) {
            e.printCell(entry);
        }
    }

    public void printCell(CellEntry cell) {
        // String shortId = cell.getId().substring(cell.getId().lastIndexOf('/')
        // + 1);
        System.out.println(" -- Cell(" + "/" + ") formula("
                + cell.getCell().getInputValue() + ") numeric("
                + cell.getCell().getNumericValue() + ") value("
                + cell.getCell().getValue() + ")");
    }
}

I am getting below error while executing the program:

Exception in thread "main" ResourceNotFoundException: Not Found Entry not found

Any help would be appreciated.

KRR
  • 4,647
  • 2
  • 14
  • 14
Ngupta
  • 319
  • 4
  • 19
  • The scope for spreadsheet API is "https://spreadsheets.google.com/feeds". Other than that, how should anyone help you if you don't even say where the error occurred? Single-step through the program and find out where it stops. Is the p12 file really there? What does `p12.exists()` say? – Alex R Jun 09 '15 at 20:03
  • my apologies as I was not able to add exception as it was showing error on stack overflow. The error is coming on this line SpreadsheetEntry spreadsheet = service.getEntry(metafeedUrl, SpreadsheetEntry.class); I changed the scope also to "spreadsheets.google.com/feeds" and when I look at the service object, I do not see any entry there. can you please help – Ngupta Jun 10 '15 at 04:21
  • Is your service account authorized to look at the file? I recommend the usage of [FeedURLFactory](https://developers.google.com/gdata/javadoc/com/google/gdata/client/spreadsheet/FeedURLFactory) to construct the URLs. The ID you mention in the other comment is your document key / id. – Alex R Jun 10 '15 at 06:09
  • Why is the [tag:excel] tag on this question? Remove the tag if not relevant, or add info in the question that makes it relevant. – Mogsdad Jun 13 '15 at 11:18

1 Answers1

0

I think the Spreadsheet url that is used to get the entry should be:

"https://spreadsheets.google.com/feeds/spreadsheets/" + file Id

Try changing and see if you can get rid of that error.

Also check this for more info.

Community
  • 1
  • 1
KRR
  • 4,647
  • 2
  • 14
  • 14
  • I changed the scope to "https://spreadsheets.google.com/feeds/" and spreadsheet url to "https://spreadsheets.google.com/feeds/1QWX-zOkBe36M7oC9sn6z8ZuccGt7Wg-IFwtynn379kM" still it does not allow me to access the spreadsheet – Ngupta Jun 10 '15 at 04:26
  • is "1QWX-zOkBe36M7oC9sn6z8ZuccGt7Wg-IFwtynn379kM" my file id or file ID is somthing different ? I am a bit new to this thing. – Ngupta Jun 10 '15 at 05:29