简体   繁体   中英

How to get list of Sheet's via Google V4 Java API

I'm trying to get a list of sheet names via the google API.

I can get cell values, but I can't find out how to get the list of sheets.

Here is the code I've tried.

Credential credential = authorize();
Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();

System.out.println("__");
Set<Entry<String, Object>> entrySet = service.spreadsheets().get(spreadsheetId).entrySet();
for (Entry<String, Object> entry : entrySet) {
   System.out.println(entry.getKey() +"    " + entry.getValue());
} 
System.out.println("__");

System.out.println("1::" + service.spreadsheets().get(spreadsheetId).get("spreadsheetId"));
System.out.println("2::" + service.spreadsheets().get(spreadsheetId).get(spreadsheetId));
System.out.println("3::" + service.spreadsheets().get(spreadsheetId).get("sheet"));
System.out.println("4::" + service.spreadsheets().get(spreadsheetId).get("accesToken"));
System.out.println("5::" + service.spreadsheets().get(spreadsheetId).get("properties"));
System.out.println("6::" + service.spreadsheets().get(spreadsheetId).get("sheets"));
System.out.println("7::" + service.spreadsheets().get(spreadsheetId).get("sheets[]"));

Here is the output

__
spreadsheetId    1iV5qliFI8xNhqyLJoOBTQxxxxxxxxxxxxxxxxxx
__
1::1iV5qliFI8xNhqyLJoOBTQxxxxxxxxxxxxxxxxxx
2::null
3::null
4::null
5::null
6::null
7::null

My pom.xml entries

<dependency>
    <groupId>com.google.api-client</groupId>
    <artifactId>google-api-client</artifactId>
    <version>1.22.0</version>
</dependency>
<dependency>
    <groupId>com.google.oauth-client</groupId>
    <artifactId>google-oauth-client-jetty</artifactId>
    <version>1.22.0</version>
</dependency>
<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-sheets</artifactId>
    <version>v4-rev471-1.22.0</version>
</dependency>

Can anyone suggest how I get the list of sheets in the spreadsheet please.

It's pretty un-intuitive TBH. I had to dig quite a bit in the API to get the hang of it, but with the following you can get the Sheets:

Sheets service = getSheetsService();
Spreadsheet sp = service.spreadsheets().get(spreadsheetId).execute();
List<Sheet> sheets = sp.getSheets();

So the gist is calling execute() .

Afterwards, you can access sheet.getProperties().getTitle() to read the title.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM