如何在 Google 电子表格中获取单个工作表的工作表名称 - Google Sheet Api v4 - Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38123383/
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-11-03 03:08:55  来源:igfitidea点击:

How to get Sheet Name of individual Sheets in Google Spreadsheet - Google Sheet Api v4 - Java

javagoogle-sheetsgoogle-spreadsheet-apigoogle-sheets-api

提问by Ashwiinn Karaangutkar

I am trying to get sheet name of individual sheets in Google Spreadsheet using Google Sheet Api v4.

我正在尝试使用 Google Sheet Api v4 在 Google Spreadsheet 中获取单个工作表的工作表名称。

I tried the following way that gets the properties:

我尝试了以下获取属性的方法:

  Spreadsheet response1= service.spreadsheets().get(spreadsheetId).setIncludeGridData (false).execute ();

  System.out.println (response1.getProperties ().getTitle ());

However it displays the name of the actual Spreadsheet rather than displaying the names of individual sheets in the Spreadsheet.

但是,它显示实际电子表格的名称,而不是显示电子表格中各个工作表的名称。

Do anyone know how we can go about it?

有谁知道我们该怎么做?

回答by Ashwiinn Karaangutkar

Below is the answer to above question:

以下是上述问题的答案:

Spreadsheet response1= service.spreadsheets().get(spreadsheetId).setIncludeGridData (false)
.execute ();

System.out.println (response1.getSheets ().get (0).getProperties ().getTitle ());

Thank you.

谢谢你。

回答by Masum

Here, you can iterate over the workSheetListto get all individual sheet title.

在这里,您可以遍历workSheetList以获取所有单独的工作表标题。

Spreadsheet response1= service.spreadsheets().get(spreadsheetId).setIncludeGridData (false).execute ();

List<Sheet> workSheetList = response1.getSheets();

for (Sheet sheet : workSheetList) {
    System.out.println(sheet.getProperties().getTitle());
}

回答by Vlad

Kotlin version

科特林版

fun getSheets(): List<GSheetMeta> {
    return sheets.spreadsheets()
        .get("spreadsheetId")
        .setIncludeGridData(false)
        .execute()
        .sheets
        .map { GSheetMeta(it.properties.title, it.properties.sheetId) }
}

And model class

和模型类

class GSheetMeta(val name: String, val id: Int) {

    override fun toString() = name
}