任何人都可以帮我阅读JAVA中谷歌电子表格中的数据

时间:2016-08-18 10:51:05

标签: java google-sheets

我正在尝试下面的代码从谷歌电子表格中获取数据,但是收到错误..任何人都可以帮助我从谷歌电子表格中获取数据

public class gsheet 
{
    public static final String GOOGLE_ACCOUNT_USERNAME = "gmail-id";
    public static final String GOOGLE_ACCOUNT_PASSWORD = "password"; 
    public static final String SPREADSHEET_URL = "spread-sheet-URL";


      public static void main(String[] args) throws IOException, ServiceException
      {
        SpreadsheetService service = new SpreadsheetService("JAVA");
        service.setUserCredentials(GOOGLE_ACCOUNT_USERNAME, GOOGLE_ACCOUNT_PASSWORD);
        URL metafeedUrl = new URL(SPREADSHEET_URL);
        SpreadsheetEntry spreadsheet = service.getEntry(metafeedUrl, SpreadsheetEntry.class);
        URL listFeedUrl = ((WorksheetEntry) spreadsheet.getWorksheets().get(0)).getListFeedUrl();
        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));
          }

        }

      }

}

我在运行代码时遇到以下错误

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/Maps
    at com.google.gdata.wireformats.AltRegistry.<init>(AltRegistry.java:118)
    at com.google.gdata.wireformats.AltRegistry.<init>(AltRegistry.java:100)
    at com.google.gdata.client.Service.<clinit>(Service.java:555)
    at googlesheet.gsheet.main(gsheet.java:24)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.Maps
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 4 more

1 个答案:

答案 0 :(得分:2)

您必须将google-collections.jar添加到类路径中。