2014-06-15 36 views
0

我對Java很陌生,對Google Data API也是新手。我正嘗試在公開共享的電子表格中更改工作表的大小。我使用下面的代碼:Java Google Spreadsheet API:更新公共工作表'java.lang.UnsupportedOperationException:條目無法更新'

import com.google.gdata.client.authn.oauth.*; 
import com.google.gdata.client.spreadsheet.*; 
import com.google.gdata.data.*; 
import com.google.gdata.data.batch.*; 
import com.google.gdata.data.spreadsheet.*; 
import com.google.gdata.util.*; 

import java.io.IOException; 
import java.net.*; 
import java.util.*; 
static void writeToGoogleSpreadsheet(String spreadsheetKey) throws IOException, ServiceException { 
     SpreadsheetService service = new SpreadsheetService("com.example"); 
     String urlString = "https://spreadsheets.google.com/feeds/worksheets/" + spreadsheetKey + "/public/basic"; 

     URL url = new URL(urlString); 

    try { 
     WorksheetFeed worksheetFeed = service.getFeed(url, WorksheetFeed.class); 
     List<WorksheetEntry> worksheets = worksheetFeed.getEntries(); 
     WorksheetEntry worksheet = worksheets.get(0); 

     System.out.println(worksheet.getTitle().getPlainText()); 
     System.out.println(worksheet.getCanEdit()); 

     // Update the local representation of the worksheet. 
     worksheet.setTitle(new PlainTextConstruct("Updated Worksheet")); 
     worksheet.setColCount(40); 
     worksheet.setRowCount(40); 

     // Send the local representation of the worksheet to the API for 
     // modification. 
     worksheet.update(); 
    } finally{} 
} 

控制檯顯示正確的工作表名稱和大小,所以我敢肯定,我訪問權工作。但是,worksheet.update()會引發以下異常:

Exception in thread "main" java.lang.UnsupportedOperationException: Entry cannot be updated 
at com.google.gdata.data.BaseEntry.update(BaseEntry.java:635) 
atcom.example.GoogleSpreadsheetCommunicator.writeToGoogleSpreadsheet(GoogleSpreadsheetCommunicator.java:119) 
at com.example.Main.guildSim(Main.java:114) 
at com.example.Main.main(Main.java:73) 

有沒有人知道我在做什麼錯?

感謝您的閱讀和親切的問候,

卡雷爾

回答

0

您無法編輯公共提要, 變化urlString使用.../private/...

,你將需要使用的3種方式之一驗證你可以找到https://developers.google.com/google-apps/spreadsheets/#authorizing_requests_with_clientlogin

+0

這將解釋很多!謝謝!你有沒有關於這方面的未來參考文件?由於https://developers.google.com/google-apps/spreadsheets/#working_with_cell-based_feeds'注意:單元格Feed支持私人和公共的可見性,以及完整和基本投影。「表明可以編輯公共流。 – user3613853

+0

np!我從經驗中瞭解到這一點,面臨同樣的問題,所以我查看了BaseEntry.java的源代碼。同時請將答案標記爲已接受,以便可以幫助他人 –