如何使用API​​阅读Google电子表格中的工作表?

时间:2013-01-28 13:31:41

标签: java spreadsheet google-sheets google-spreadsheet-api

我有一个谷歌电子表格,其中包含2-3个单独的工作表。我正在寻找一种方法来查看工作表#2中包含的行,然后遍历前面找到的每一行的列。

我想要使用哪种API方法?我无法从google spreedsheet API页面找到这个。

1 个答案:

答案 0 :(得分:1)

package com.Sensor.Data;

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.util.ServiceException;

import java.io.IOException;
import java.net.URL;


public class readData {

    public static final String GOOGLE_ACCOUNT_USERNAME = "piprojectutd@gmail.com"; 
    public static final String GOOGLE_ACCOUNT_PASSWORD = "password"; 

    public static final String SPREADSHEET_URL = <insert your spreadsheet url here until key onlye i.e"https://spreadsheets.google.com/feeds/spreadsheets/0AjyNkJ5x_vajdDRZQ1RBSEw5aHFLRU1yOHFYeFFoekE";>



    public String getData(){
        String status="";

    try{
    /** 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 = spreadsheet.getWorksheets().get(0).getListFeedUrl();

        // Print entries
        ListFeed feed = 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));
                status=entry.getCustomElements().getValue(tag);

            }
        }

    }catch(Exception e){
        System.out.println(e);
    }
    System.out.println(status);
    return(status);
    }
}