Workbook - Copy Workbook to Member “My Documents” Folder (JavaScript)

View as MarkdownOpen in Claude

This script automates the process of copying a specific workbook for a designated user in Sigma.

Refer to the Sigma REST API Recipes QuickStart for step-by-step instructions.

Example script

JavaScript
1// Load environment variables from a specific .env file for configuration
2require('dotenv').config({ path: 'sigma-api-recipes/.env' });
3
4// Import the function to obtain a bearer token from the authenticate-bearer module
5const getBearerToken = require('../get-access-token');
6
7// Import Axios for making HTTP requests
8const axios = require('axios');
9
10// Load use-case specific variables from environment variables
11const baseURL = process.env.baseURL; // Base URL for the Sigma API
12const workbookId = process.env.WORKBOOK_ID; // Workbook ID to copy
13const memberId = process.env.MEMBERID; // ID of the member whose "My Documents" folder we'll use
14
15// Function to retrieve the ID of the member's "My Documents" folder
16async function getMyDocumentsFolderId(accessToken) {
17 try {
18 // Send the request to retrieve the member's details
19 const response = await axios.get(
20 `${baseURL}/members/${memberId}`,
21 { headers: { Authorization: `Bearer ${accessToken}` } }
22 );
23
24 // Extract the ID of the member's "My Documents" folder
25 const homeFolderId = response.data.homeFolderId;
26
27 // Log the retrieved folder ID
28 console.log('Retrieved "My Documents" folder ID:', homeFolderId);
29
30 return homeFolderId;
31 } catch (error) {
32 // Log any errors that occur during the process
33 console.error('Failed to retrieve "My Documents" folder ID:', error.response ? error.response.data : error);
34 return null;
35 }
36}
37
38// Function to copy the workbook to the specified folder
39async function copyWorkbook(accessToken, destinationFolderId) {
40 try {
41 // Define the request payload to copy the workbook
42 const copyPayload = {
43 name: 'New Workbook Name', // Specify the name of the copied workbook
44 description: 'Description of the copied workbook', // Specify the description of the copied workbook
45 ownerId: memberId, // Specify the ID of the user who will own the copied workbook
46 destinationFolderId: destinationFolderId // Use the passed destinationFolderId argument
47 };
48
49 // Send the request to copy the workbook
50 const response = await axios.post(
51 `${baseURL}/workbooks/${workbookId}/copy`,
52 copyPayload,
53 { headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json' } }
54 );
55
56 // Log the success message and copied workbook details
57 console.log('Workbook copy initiated successfully.');
58 console.log('Copied workbook details:', response.data);
59
60 // Return the ID of the copied workbook for further processing if needed
61 return response.data.workbookId;
62 } catch (error) {
63 // Log any errors that occur during the process
64 console.error('Failed to copy workbook:', error.response ? error.response.data : error);
65 return null;
66 }
67}
68
69// Main function to execute the workflow
70async function main() {
71 // Obtain the bearer token for authentication
72 const accessToken = await getBearerToken();
73 if (!accessToken) {
74 console.error('Failed to obtain bearer token.');
75 return;
76 }
77
78 // Retrieve the ID of the member's "My Documents" folder
79 const myDocumentsFolderId = await getMyDocumentsFolderId(accessToken);
80 if (!myDocumentsFolderId) {
81 console.error('Failed to retrieve "My Documents" folder ID.');
82 return;
83 }
84
85 // Copy the workbook and place it in the "My Documents" folder
86 const copiedWorkbookId = await copyWorkbook(accessToken, myDocumentsFolderId); // Pass myDocumentsFolderId here
87 if (!copiedWorkbookId) {
88 console.error('Failed to copy workbook.');
89 return;
90 }
91
92 // Perform any additional actions with the copied workbook if needed
93 console.log(`Workbook successfully copied with ID: ${copiedWorkbookId}`);
94}
95
96// Execute the main function
97main();

Endpoints used

  1. Get authentication token: getBearerToken function
  2. Get details of a user: ${baseURL}/members/${memberId}
  3. Copy a workbook: ${baseURL}/workbooks/${workbookId}/copy