使用java从谷歌电子表格中读取数据

时间:2017-01-10 16:35:20

标签: java google-sheets

我正在使用以下代码从电子表格中读取数据,但它无效。

    import java.net.URL;
    import com.google.gdata.client.spreadsheet.SpreadsheetService;
    import com.google.gdata.data.spreadsheet.ListEntry;
    import com.google.gdata.data.spreadsheet.ListFeed;
    import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
    import com.google.gdata.data.spreadsheet.WorksheetEntry;
    import com.google.gdata.util.ServiceException;

    public class ReadSpreadsheet {
    public static final String GOOGLE_ACCOUNT_USERNAME = "xxxx@gmail.com";
    // Fill in google account username
    public static final String GOOGLE_ACCOUNT_PASSWORD = "xxxx"; // Fill in google account password
  public static final String SPREADSHEET_URL = "https://spreadsheets.google.com/feeds/spreadsheets/1L8xtAJfOObsXL-XemliUV10wkDHQNxjn6jKS4XwzYZ8"; //Fill in google spreadsheet URI

  public static void main(String[] args) throws IOException, ServiceException
  {
    /** Our view of Google Spreadsheets as an authenticated Google user. */
    SpreadsheetService service = new SpreadsheetService("Print Google Spreadsheet Demo");

    // Login and prompt the user to pick a sheet to use.
    service.setUserCredentials(GOOGLE_ACCOUNT_USERNAME, GOOGLE_ACCOUNT_PASSWORD);

    // Load sheet
    URL metafeedUrl = new URL(SPREADSHEET_URL);
    SpreadsheetEntry spreadsheet = service.getEntry(metafeedUrl, SpreadsheetEntry.class);
    URL listFeedUrl = ((WorksheetEntry) spreadsheet.getWorksheets().get(0)).getListFeedUrl();

    // Print entries
    ListFeed feed = (ListFeed) service.getFeed(listFeedUrl, ListFeed.class);
    for(ListEntry entry : feed.getEntries())
    {
      System.out.println("new row");
      for(String tag : entry.getCustomElements().getTags())
      {
        System.out.println("     "+tag + ": " + entry.getCustomElements().getValue(tag));
      }
    }
  }
    }

我遇到的问题是以下行: SpreadsheetService服务=新的SpreadsheetService(“打印Google电子表格演示”);

这里我需要使用以下链接编写一些特定格式的应用程序名称: https://developers.google.com/gdata/javadoc/com/google/gdata/client/spreadsheet/SpreadsheetService

但是我无法理解应该写成的版本格式,如“[company-id] - [app-name] - [app-version]”

此外,我在我的selenium脚本中使用此代码来读取/写入数据。所以请任何人建议我实际的解决方案,如果可以在不使用此代码的情况下从硒读取/写入数据,那么它将会很棒。

我得到的错误是

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.of([Ljava/lang/Object;)Lcom/google/common/collect/ImmutableSet;
at com.google.gdata.wireformats.AltFormat$Builder.setAcceptableTypes(AltFormat.java:399)
at com.google.gdata.wireformats.AltFormat$Builder.setAcceptableXmlTypes(AltFormat.java:387)
at com.google.gdata.wireformats.AltFormat.<clinit>(AltFormat.java:49)
at com.google.gdata.client.Service.<clinit>(Service.java:558)
at ReadSpreadsheet.main(ReadSpreadsheet.java:21)

我在这个项目中使用以下jar:

    gdata-core-1.0.jar
    gdata-spreadsheet-3.0.jar
    guava-20.0
    mailapi.jar`

0 个答案:

没有答案