Workbooks: List all Input Tables (JavaScript)
Workbooks: List all Input Tables (JavaScript)
This script searches through all workbooks for specific elements of the type “input-table” and logs details about them, including the latest version number.
Only workbooks owned by or shared with the specified user are searched.
Each section of the script has inline comments provided.
Refer to the Sigma REST API Recipes QuickStart for step-by-step instructions.
Example script
JavaScript
// This script lists all instances of input-tables, across all workbooks, where they exist.// Utilizes Sigma's API as documented in their Swagger documentation.// Load necessary dependencies and set up the environmentrequire('dotenv').config({ path: 'sigma-api-recipes/.env' }); // Load environment variables for configurationconst getBearerToken = require('../get-access-token'); // Import function to obtain a bearer token for API authenticationconst axios = require('axios'); // Import Axios for making HTTP requestsconst baseURL = process.env.baseURL; // Load the base URL for API requests from environment variables// Handle any unhandled promise rejections to prevent the script from failing silentlyprocess.on('unhandledRejection', error => {console.error('Unhandled promise rejection:', error);});// Function to fetch and log details of 'input-table' elements within a specific workbookasync function fetchElementsOfWorkbook(workbook, accessToken) {try {// Fetch all pages within the workbookconst pagesResponse = await axios.get(`${baseURL}/workbooks/${workbook.workbookId}/pages`, {headers: { 'Authorization': `Bearer ${accessToken}` }, // Include the authorization header});// Iterate through each page in the workbookfor (const page of pagesResponse.data.entries) {const elementsUrl = `${baseURL}/workbooks/${workbook.workbookId}/pages/${page.pageId}/elements`; // Construct the URL to fetch elementstry {// Fetch elements for the current pageconst elementsResponse = await axios.get(elementsUrl, {headers: { 'Authorization': `Bearer ${accessToken}` }, // Include the authorization header});// Filter for elements of type 'input-table'const inputTableElements = elementsResponse.data.entries.filter(element => element.type === 'input-table');if (inputTableElements.length > 0) {// Log workbook and page details if 'input-table' elements are foundconsole.log(`Workbook: "${workbook.name}", Path: "${workbook.path}/${workbook.workbookId}", Page: "${page.name}"`);inputTableElements.forEach(element => {// Log details for each 'input-table' elementconsole.log(` - Input Table: ${element.name}, Element ID: ${element.elementId}, Latest Version: ${workbook.latestVersion}`);});}} catch (error) {// Silently continue if there's an error fetching elements for the page}}} catch (error) {// Silently continue if there's an error fetching pages for the workbook}}// Main function to list workbooks and find 'input-table' elements within themasync function listWorkbooksAndFindInputTables() {console.log('Starting to search for input-table elements across all workbooks...');const accessToken = await getBearerToken(); // Obtain the bearer token for API authenticationif (!accessToken) {console.error('Failed to obtain Bearer token.'); // Log an error if the token cannot be obtainedreturn;}try {// Fetch all workbooks accessible to the tokenconst workbooksResponse = await axios.get(`${baseURL}/workbooks`, {headers: { 'Authorization': `Bearer ${accessToken}` }, // Include the authorization header});// Iterate through each workbook to search for 'input-table' elementsfor (const workbook of workbooksResponse.data.entries) {await fetchElementsOfWorkbook(workbook, accessToken); // Process each workbook}} catch (error) {// Silently continue if there's an error fetching all workbooks}console.log('Completed searching for input-table elements.'); // Indicate completion of the script}// Execute the main function if the script is run directlyif (require.main === module) {listWorkbooksAndFindInputTables();}// Export the main function for use in other modulesmodule.exports = listWorkbooksAndFindInputTables;
Response Example
Workbook: "Images in Tables", Path: "My Documents/QuickStarts/Images in Tables/02dac1b8-ec2d-451c-8dd1-db2d13fa97de", Page: "Page 1"all-input-tables.js:37- Input Table: Source URLS, Element ID: palj7CIHH0, Latest Version: 8all-input-tables.js:40Workbook: "Source Data for Hightouch", Path: "My Documents/QuickStarts/Hightouch-Hubspot/12ff9839-9d00-47a9-89db-14be5c762369", Page: "Lead Approval"all-input-tables.js:37- Input Table: Lead Management, Element ID: n9gyvwdZEO, Latest Version: 5all-input-tables.js:40Workbook: "QuickStart", Path: "My Documents/QuickStarts/Input Tables/23d0aba6-2090-4c8e-b4b2-b870cbe1104d", Page: "Page 1"all-input-tables.js:37- Input Table: Sales Forecast, Element ID: vsyHs-kVa5, Latest Version: 5all-input-tables.js:40Workbook: "Application Embedding", Path: "My Documents/Workbooks - Specific Feature Demo/294848a2-802b-4bde-b66a-7bd53bdaefd7", Page: "Input Tables"all-input-tables.js:37- Input Table: New Input Table, Element ID: AIyNV0eFWr, Latest Version: 166all-input-tables.js:40Completed searching for input-table elements.
Endpoints used
- Get authentication token:
getBearerTokenfunction - Get all workbooks
${baseURL}/workbooks - Get pages within a workbook
${baseURL}/workbooks/${workbook.workbookId}/pages - Get elements on a page
${baseURL}/workbooks/${workbook.workbookId}/pages/${page.pageId}/elements

