2015-06-09 139 views
1

我有这个代码,我试图访问我创建的电子表格。无法使用谷歌电子表格API访问电子表格

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("[email protected]") 
       .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() + ")"); 
    } 
} 

我得到以下错误而执行程序:

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

任何帮助,将不胜感激。

+0

电子表格API的范围是“https://spreadsheets.google.com/feeds”。除此之外,如果你甚至没有说错误发生在哪里,应该如何帮助你?单步执行程序并找出停止的地方。 p12文件真的在那里吗? “p12.exists()”说什么? –

+0

我的歉意,因为我无法添加异常,因为它显示堆栈溢出错误。 误差即将该线路上 SpreadsheetEntry电子表格= service.getEntry(metafeedUrl, \t \t \t \t SpreadsheetEntry.class); 我将范围也更改为“spreadsheets.google.com/feeds”,当我查看服务对象时,我没有看到任何条目。 您能否请帮助 – Ngupta

+0

您的服务帐户是否有权查看该文件?我建议使用[FeedURLFactory](https://developers.google.com/gdata/javadoc/com/google/gdata/client/spreadsheet/FeedURLFactory)来构建网址。您在其他评论中提到的ID是您的文档密钥/ ID。 –

回答

0

我认为这是用来获取项应是电子表格网址:

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

试着改变,看看你是否能摆脱错误的。

另请参阅this了解更多信息。

+0

我改变了范围“https://spreadsheets.google.com/feeds/” 和 电子表格网址为“https://spreadsheets.google.com/feeds/1QWX-zOkBe36M7oC9sn6z8ZuccGt7Wg-IFwtynn379kM” 还是它不允许我访问电子表格 – Ngupta

+0

是否在电子表格url的末尾添加了文件ID? – KRR

+0

是“1QWX-zOkBe36M7oC9sn6z8ZuccGt7Wg-IFwtynn379kM”我的文件ID或文件ID有什么不同? 我对这件事有点新鲜。 – Ngupta