Reputation: 171
I am trying to get sheet name of individual sheets in Google Spreadsheet using Google Sheet Api v4.
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?
Upvotes: 3
Views: 12747
Reputation: 171
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 ());
Upvotes: 6
Reputation: 8553
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
}
Upvotes: 0
Reputation: 393
Here, you can iterate over the workSheetList
to get all individual sheet title.
Spreadsheet response1= service.spreadsheets().get(spreadsheetId).setIncludeGridData (false).execute ();
List<Sheet> workSheetList = response1.getSheets();
for (Sheet sheet : workSheetList) {
System.out.println(sheet.getProperties().getTitle());
}
Upvotes: 4