Workbooks: List all Input Tables (JavaScript)

View as MarkdownOpen in Claude

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
1// This script lists all instances of input-tables, across all workbooks, where they exist.
2// Utilizes Sigma's API as documented in their Swagger documentation.
3
4// Load necessary dependencies and set up the environment
5require('dotenv').config({ path: 'sigma-api-recipes/.env' }); // Load environment variables for configuration
6const getBearerToken = require('../get-access-token'); // Import function to obtain a bearer token for API authentication
7const axios = require('axios'); // Import Axios for making HTTP requests
8
9const baseURL = process.env.baseURL; // Load the base URL for API requests from environment variables
10
11// Handle any unhandled promise rejections to prevent the script from failing silently
12process.on('unhandledRejection', error => {
13 console.error('Unhandled promise rejection:', error);
14});
15
16// Function to fetch and log details of 'input-table' elements within a specific workbook
17async function fetchElementsOfWorkbook(workbook, accessToken) {
18 try {
19 // Fetch all pages within the workbook
20 const pagesResponse = await axios.get(`${baseURL}/workbooks/${workbook.workbookId}/pages`, {
21 headers: { 'Authorization': `Bearer ${accessToken}` }, // Include the authorization header
22 });
23
24 // Iterate through each page in the workbook
25 for (const page of pagesResponse.data.entries) {
26 const elementsUrl = `${baseURL}/workbooks/${workbook.workbookId}/pages/${page.pageId}/elements`; // Construct the URL to fetch elements
27 try {
28 // Fetch elements for the current page
29 const elementsResponse = await axios.get(elementsUrl, {
30 headers: { 'Authorization': `Bearer ${accessToken}` }, // Include the authorization header
31 });
32
33 // Filter for elements of type 'input-table'
34 const inputTableElements = elementsResponse.data.entries.filter(element => element.type === 'input-table');
35 if (inputTableElements.length > 0) {
36 // Log workbook and page details if 'input-table' elements are found
37 console.log(`Workbook: "${workbook.name}", Path: "${workbook.path}/${workbook.workbookId}", Page: "${page.name}"`);
38 inputTableElements.forEach(element => {
39 // Log details for each 'input-table' element
40 console.log(` - Input Table: ${element.name}, Element ID: ${element.elementId}, Latest Version: ${workbook.latestVersion}`);
41 });
42 }
43 } catch (error) {
44 // Silently continue if there's an error fetching elements for the page
45 }
46 }
47 } catch (error) {
48 // Silently continue if there's an error fetching pages for the workbook
49 }
50}
51
52// Main function to list workbooks and find 'input-table' elements within them
53async function listWorkbooksAndFindInputTables() {
54 console.log('Starting to search for input-table elements across all workbooks...');
55 const accessToken = await getBearerToken(); // Obtain the bearer token for API authentication
56 if (!accessToken) {
57 console.error('Failed to obtain Bearer token.'); // Log an error if the token cannot be obtained
58 return;
59 }
60
61 try {
62 // Fetch all workbooks accessible to the token
63 const workbooksResponse = await axios.get(`${baseURL}/workbooks`, {
64 headers: { 'Authorization': `Bearer ${accessToken}` }, // Include the authorization header
65 });
66
67 // Iterate through each workbook to search for 'input-table' elements
68 for (const workbook of workbooksResponse.data.entries) {
69 await fetchElementsOfWorkbook(workbook, accessToken); // Process each workbook
70 }
71 } catch (error) {
72 // Silently continue if there's an error fetching all workbooks
73 }
74 console.log('Completed searching for input-table elements.'); // Indicate completion of the script
75}
76
77// Execute the main function if the script is run directly
78if (require.main === module) {
79 listWorkbooksAndFindInputTables();
80}
81
82// Export the main function for use in other modules
83module.exports = listWorkbooksAndFindInputTables;
Response Example
1Workbook: "Images in Tables", Path: "My Documents/QuickStarts/Images in Tables/02dac1b8-ec2d-451c-8dd1-db2d13fa97de", Page: "Page 1"
2all-input-tables.js:37
3 - Input Table: Source URLS, Element ID: palj7CIHH0, Latest Version: 8
4all-input-tables.js:40
5Workbook: "Source Data for Hightouch", Path: "My Documents/QuickStarts/Hightouch-Hubspot/12ff9839-9d00-47a9-89db-14be5c762369", Page: "Lead Approval"
6all-input-tables.js:37
7 - Input Table: Lead Management, Element ID: n9gyvwdZEO, Latest Version: 5
8all-input-tables.js:40
9Workbook: "QuickStart", Path: "My Documents/QuickStarts/Input Tables/23d0aba6-2090-4c8e-b4b2-b870cbe1104d", Page: "Page 1"
10all-input-tables.js:37
11 - Input Table: Sales Forecast, Element ID: vsyHs-kVa5, Latest Version: 5
12all-input-tables.js:40
13Workbook: "Application Embedding", Path: "My Documents/Workbooks - Specific Feature Demo/294848a2-802b-4bde-b66a-7bd53bdaefd7", Page: "Input Tables"
14all-input-tables.js:37
15 - Input Table: New Input Table, Element ID: AIyNV0eFWr, Latest Version: 166
16all-input-tables.js:40
17Completed 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