java 如何使用 API 读取谷歌电子表格中的工作表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14563163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:41:51  来源:igfitidea点击:

How to read the worksheets in a google spreadsheet using the API?

javaspreadsheetgoogle-sheetsgoogle-spreadsheet-api

提问by Click Upvote

I have a google spreadsheet which contains 2-3 seperate worksheets. I'm looking for a way to look through the rows contained in worksheet # 2, and then loop through the columns for each row found earlier.

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

Which API method would I be looking for to do this? I couldn't find this from the google spreedsheet API page.

我会寻找哪种 API 方法来做到这一点?我无法从谷歌电子表格 API 页面找到这个。

回答by user2121527

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 = "[email protected]"; 
    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);
    }
}