2010-08-24 48 views
2

我想複製一個文件這裏提到:使用gdata將Java客戶端發送到Google文檔列表API。

http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#CopyingDocs

我使用的是最新的Java GDATA庫,它沒有一個很好的包裝方法來做到這一點,我是一個福利局到java gdata lib。

我已經有了一個認證的DocsService,如果這很有用的話。

獎勵積分如果將其包裝到一個採用兩個字符串的方法中,一個是源名稱,另一個是副本名稱。

回答

1

好,我做到了....

只是讓大家看到... this.dService是DocsService,已經被驗證。

// This method returns the Resource ID to be used to make the copy 
public String loadDocsSpreadsheetEntryId(String sourceName) { 
    String resourceId = null; 
    try { 

    Logger.info("Loading feed URL"); 
    URL url = new URL("https://docs.google.com/feeds/default/private/full"); 
    DocumentQuery query = new DocumentQuery(url); 
    query.setTitleQuery(sourceName); 
    query.setTitleExact(true); 

    Logger.info("Loaded feed URL"); 
    DocumentListFeed dfeed = this.dService.getFeed(query, DocumentListFeed.class); 
    Logger.info("got feed"); 
    for (DocumentListEntry entry : dfeed.getEntries()) { 
     Logger.info(entry.getTitle().getPlainText()); 
     if(entry.getTitle().getPlainText().equalsIgnoreCase(sourceName)) 
     { 
      Logger.info("found doc"); 
      resourceId = entry.getResourceId(); 
     } 
    } 
    } catch(Exception e) { 
     logException(e, "Loading Source Spreadsheet to copy"); 
    } 

    return resourceId; 
} 

    public void createSpreadsheetFrom(String destination, String source) { 
     try { 
     URL entryUrl = new URL("http://docs.google.com/feeds/default/private/full"); 
     Map<String, String> parameters = new HashMap<String, String>(); 
     String resourceID = loadDocsSpreadsheetEntryId(source); 
     Logger.info("Resource id %s", resourceID); 

     DocumentListEntry newEntry = new DocumentListEntry(); 
     newEntry.setId(resourceID); 
     newEntry.setTitle(new PlainTextConstruct(destination)); 
     this.dService.insert(entryUrl, newEntry); 

     } catch(Exception e) { 
      logException(e, "Copying Spreadsheet"); 
     } 

} 
+0

感謝馬克發佈您的解決方案,這正是我一直在尋找的東西,我處於需要複製電子表格的相同情況。很有幫助! – Brummo 2011-01-01 23:44:06