Workbooks: List all Input Tables (JavaScript)

View as Markdown

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 environment
require('dotenv').config({ path: 'sigma-api-recipes/.env' }); // Load environment variables for configuration
const getBearerToken = require('../get-access-token'); // Import function to obtain a bearer token for API authentication
const axios = require('axios'); // Import Axios for making HTTP requests
const 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 silently
process.on('unhandledRejection', error => {
console.error('Unhandled promise rejection:', error);
});
// Function to fetch and log details of 'input-table' elements within a specific workbook
async function fetchElementsOfWorkbook(workbook, accessToken) {
try {
// Fetch all pages within the workbook
const pagesResponse = await axios.get(`${baseURL}/workbooks/${workbook.workbookId}/pages`, {
headers: { 'Authorization': `Bearer ${accessToken}` }, // Include the authorization header
});
// Iterate through each page in the workbook
for (const page of pagesResponse.data.entries) {
const elementsUrl = `${baseURL}/workbooks/${workbook.workbookId}/pages/${page.pageId}/elements`; // Construct the URL to fetch elements
try {
// Fetch elements for the current page
const 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 found
console.log(`Workbook: "${workbook.name}", Path: "${workbook.path}/${workbook.workbookId}", Page: "${page.name}"`);
inputTableElements.forEach(element => {
// Log details for each 'input-table' element
console.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 them
async function listWorkbooksAndFindInputTables() {
console.log('Starting to search for input-table elements across all workbooks...');
const accessToken = await getBearerToken(); // Obtain the bearer token for API authentication
if (!accessToken) {
console.error('Failed to obtain Bearer token.'); // Log an error if the token cannot be obtained
return;
}
try {
// Fetch all workbooks accessible to the token
const workbooksResponse = await axios.get(`${baseURL}/workbooks`, {
headers: { 'Authorization': `Bearer ${accessToken}` }, // Include the authorization header
});
// Iterate through each workbook to search for 'input-table' elements
for (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 directly
if (require.main === module) {
listWorkbooksAndFindInputTables();
}
// Export the main function for use in other modules
module.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: 8
all-input-tables.js:40
Workbook: "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: 5
all-input-tables.js:40
Workbook: "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: 5
all-input-tables.js:40
Workbook: "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: 166
all-input-tables.js:40
Completed searching for input-table elements.

Endpoints used

  1. Get authentication token: getBearerToken function
  2. Get all workbooks ${baseURL}/workbooks
  3. Get pages within a workbook ${baseURL}/workbooks/${workbook.workbookId}/pages
  4. Get elements on a page ${baseURL}/workbooks/${workbook.workbookId}/pages/${page.pageId}/elements