February 14th, 2025
Retrieve Named Table Data in Google Sheets with a Simple Google Apps Script Helper
Learn how to access data from a named table in Google Sheets using a custom Apps Script function. This step-by-step guide covers a workaround to Google’s lack of a native “table” object, giving you a quick and efficient way to fetch structured data.
Google Sheets offers a wide range of capabilities, but it doesn’t yet provide a native method for retrieving data from an Excel-like “table” object. If you’ve already named a cell range as a table in Sheets and want to programmatically read its rows and columns, you might be stuck. Enter getDataFromTable
, a helper function that uses a clever workaround to retrieve the data by inserting a temporary sheet and referencing the table via a formula.
In this post, we’ll walk through why you might need this approach, how the code works, and how to integrate it into your Google Apps Script projects.
Why a “Table” Workaround?
- No Built-In Named Table Access: While Google Sheets supports “named ranges,” it doesn’t treat them exactly like tables (with inherent row/column references).
- Structured Data: If you’re used to Excel’s table references, you may want a structured approach that references a contiguous block of data by a single name.
- Dynamic Ranges: This approach can handle a named range that might grow or shrink dynamically, saving you the hassle of manually updating references.
The Helper Function
Below is the complete getDataFromTable
function. It uses a temporary sheet to insert a formula referencing your named table, fetches the resulting values, and then deletes the temporary sheet. This prevents clutter in your main workbook while still letting you retrieve the data.
/******************************************************
HELPER: "getDataFromTable" referencing the named table
******************************************************/
function getDataFromTable(spreadsheetId, tableName) {
const ss = SpreadsheetApp.openById(spreadsheetId);
const tempSheet = ss.insertSheet(`temp_${Utilities.getUuid()}`);
try {
// Use a formula referencing the named table.
const formula = `=LET(
tableName, ${tableName},
ARRAYFORMULA(OFFSET(tableName, -1, 0, ROWS(tableName) + 1))
)`;
tempSheet.getRange("A1").setFormula(formula);
SpreadsheetApp.flush(); // Force formula evaluation
const values = tempSheet.getDataRange().getValues();
return values;
} catch (error) {
Logger.log(\`Error retrieving table "\${tableName}": \${error.message}\`);
return null;
} finally {
ss.deleteSheet(tempSheet);
}
}
How It Works
- Open the Spreadsheet: We first open the target spreadsheet by its spreadsheetId.
- Create a Temporary Sheet: We insert a blank sheet whose name includes a unique UUID to ensure no name collisions.
- Set a Formula:
- We use a
LET
function in Sheets, creating a local variable tableName
referencing your named range. OFFSET(tableName, -1, 0, ROWS(tableName)+1)
effectively selects the range that includes headers (one row above) plus all rows in the named range.- For truly dynamic behavior, you can modify the formula as needed (e.g., skip headers, different offsets, etc.).
- Flush and Retrieve: Calling
SpreadsheetApp.flush()
forces the formula to update. Then we read the resulting values. - Clean Up: Finally, the temporary sheet is deleted, so there’s no leftover data or clutter in your workbook.
Setting Up Your Named Table in Google Sheets
- Create a Named Range:
- Highlight a range of cells in your sheet.
- Go to Data > Named ranges.
- Give it a name—e.g., “employeeTable.”
- Reference This Name:
- Within your formula (in the script above), pass the named range (like
employeeTable
) as an argument to the OFFSET
function. - Since
tableName
is a parameter, you might call the function
const data = getDataFromTable("YOUR_SPREADSHEET_ID", "employeeTable");
Example Usage in a Script
Imagine you have a spreadsheet that tracks Employees with the named range “employeeTable,” which includes a header row and several columns of data. You want to log each employee’s name in your script:
function logEmployeeNames() {
const spreadsheetId = "YOUR_SPREADSHEET_ID";
const tableName = "employeeTable";
const tableData = getDataFromTable(spreadsheetId, tableName);
if (!tableData) {
Logger.log("No data found or error retrieving table.");
return;
}
// Assuming the first row are headers and the first column is "Name"
for (let i = 1; i < tableData.length; i++) {
const row = tableData[i];
const name = row[0]; // first column
Logger.log(`Employee Name: ${name}`);
}
}
What’s Happening?
- We call
getDataFromTable
to grab the entire range, including headers. - The function returns a 2D array of rows and columns.
- We skip the first row (headers) and log each subsequent row’s first column (the “Name” column).
Potential Use Cases
- Reporting: Pull structured data into scripts that create pivot tables, charts, or PDF reports.
- Merging Data: Use in combination with other Sheets or external APIs to combine data sets.
- Data Validation: Quickly extract table data for validation or verification scripts before pushing to a database.
Tips & Considerations
- Performance: Each call creates and deletes a temporary sheet. If you need to reference the table repeatedly, consider storing the data once or using caching.
- Security: Your script needs “edit” permissions on the spreadsheet to insert and delete sheets.
- Named Range Changes: If the underlying named range changes (e.g., you rename it, or drastically alter the columns), update your usage and formula references accordingly.
- Headers or No Headers: Adjust the
OFFSET
parameters if you do not want to include headers in your result.
Conclusion
While Google Sheets lacks a direct method to handle “tables” as first-class objects, you can still retrieve table-like data with a bit of creativity. By leveraging named ranges, dynamic formulas, and a temporary sheet, the getDataFromTable
helper makes it easier to manage structured data in your scripts. Give this approach a try for more seamless automations—especially if you’re transitioning from Excel’s table-centric workflows to Google Workspace.
This tutorial was written by Patrick Flanagan, a Google Apps Script enthusiast who specializes in custom workflow automations, helping businesses harness the full potential of Google Workspace and more. If you have questions or need assistance with your own App Script projects, reach out to learn more! https://www.univium.com/contact