The Google Sheets API Advanced Service has a strange habit of switching between using Google Sheet names like 'Sheet1'
and the sheet ID that you would see in the URL, like 482103711
.
This can be problematic when working with complex processes in the Sheets API.
One of the first things I do when running a complex process is to make an initial call to the Sheets.Spreadsheets.get()
method to generate an array of all of the sheet tabs by name and sheet ID. Yes, it is an additional call to the API but if we do it initially in conjunction with any other core requests from the spreadsheet then we only need to do it one time.
Let’s take a look at some sample code:
The Code Example
Our goal below is to extract all of the sheet tab names by title
and sheetId
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/** * @typedef {Array.<SheetInfo>} SheetsByNameAndId */ /** * @typedef {object} SheetInfo * @property {string} title - The title of the sheet. * @property {number} sheetId - The ID of the sheet. */ /** * Retrieves an array of objects by sheet name and ID * @param {String} ssID - the Spreadsheet ID * @returns {SheetsByNameAndId} */ function getSheetNamesAndIds(ssID) { const payload = { fields: "sheets(properties.sheetId,properties.title)" } try { const resp = Sheets.Spreadsheets.get(ssID, payload) console.log(JSON.stringify(resp, null, " ")) const sheets = resp.sheets.map(sheet => sheet.properties) return sheets } catch (error) { console.error("Error retrieving sheet names and IDs:", error); throw new Error("Failed to retrieve sheet information: " + error.message); } } function runsies() { const ssID = "11VW5OKgjQTl8yQ3vIXDfXrMP_Sc1xFAR3Z6jUSt5AD4" const sheets = getSheetNamesAndIds(ssID) console.log(sheets) } |
Add the Google Sheets API Advance Service
Before you start, you will need to include the Google Sheets API advanced service in your project.

- Click the plus icon next to ‘Service’.
- Select Google Sheets API.
- Ensure v4 is selected.
- Use Sheets as the identifier for the API in the project.
- Click ‘Add’.
Runsies
In the example code above we are calling our simple getSheetNamesAndIds()
method from the runsies()
function.
We are providing our own sheet example which comes from this sample sheet:

You will need to add your own sheet ID here. Of course, if your project is bound to an existing sheet, you could always use the SpreadsheetApp.getActiveSpreadsheet().getId()
method to do this.
About Type Definitions
We’ll use the Google Apps Script’s preferred JSDoc documentation notation to assist with autocompletion using @typedef
for both the array and the sheet info.
The Sheets.Spreadsheets.get() method
Line 23
the Google API Sheets.SpreadSheets.get() method takes two parameters:
- The spreadsheet ID
- (optional) A payload of arguments
Without the optional payload, you will get the full bloated object containing all the properties of the Google Sheet. This can be quite a large dataset.
Of course, the larger the dataset, the longer it takes to transfer this information to you slowing down your runtime.
The Payload
Lines 18-20
There are a few things you can add to the payload, but for us, we want to use the ‘fields’ property. The fields property is a field mask that allows us to only retrieve what we choose from the API and, significantly minimise the returned object and speed up our process.
When preparing this function, I would first run the get()
method without the fields payload to find where the sheet tab data is stored and then build the fields string from this.
"sheets(properties.sheetId,properties.title)"
This means that inside the object it would be this:
1 2 3 4 5 6 7 8 9 |
{ sheets: [ properties: { sheetId: , title: } , ... ] } |
As you can see, curved braces indicate arrays and dots connect nested properties.
Try…Catch Statement
Lines 22, 29-31
We include a JavaScript try…catch statement to attempt to make a request to the Sheets API.
If the request fails the catch statement allows us to generate a custom error. I have left two options here. In the execution, you may just wish to log an error with console.error()
method. However, if you wish to terminate the process use the JavaScript throw new Error()
approach.
The Returned Response
Line 23
The response from the get()
method call is stored in the resp
variable. Using our example the raw response should look a little like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
{ "sheets": [ { "properties": { "title": "Astronauts", "sheetId": 0 } }, { "properties": { "sheetId": 482103711, "title": "Valentina Tereshkova" } }, { "properties": { "sheetId": 587263747, "title": "John Glenn" } }, { "properties": { "title": "Yuri Gagarin", "sheetId": 2043742544 } }, { "properties": { "sheetId": 542647973, "title": "Buzz Aldrin" } }, { "properties": { "sheetId": 1160322904, "title": "Neil Armstrong" } } ] } |
As you can see, this is not the cleanest of responses, yet. Let’s tidy it up.
Mapping an array of just titles and sheet IDs
Line 26
Finally, we use the JavaScript map method to iterate over the resp.sheets
property. On each iteration of each sheet, we select the properties
erm… property and return the tile
and sheetId
before it is returned from the function.
The sheets
variable should then look like this:
1 2 3 4 5 6 7 8 |
[ { title: 'Astronauts', sheetId: 0 }, { sheetId: 482103711, title: 'Valentina Tereshkova' }, { sheetId: 587263747, title: 'John Glenn' }, { title: 'Yuri Gagarin', sheetId: 2043742544 }, { sheetId: 542647973, title: 'Buzz Aldrin' }, { sheetId: 1160322904, title: 'Neil Armstrong' } ] |
Conclusion
I have a more practical use case for extracting the ID and title of all the Google Sheets with Google Sheets API advanced service in my tutorial:
Creating a Google Sheets Row Archiver with Google Sheets API Advanced Service and Apps Script
It is a much more practical and useful example.
In the comments, I would love to know how you would apply and modify this code for your own products. Your comments provide insight and inspiration for many developers.
If you have found the tutorial helpful, why not shout me a coffee ☕? I'd really appreciate it.
~ Yagi