Workbook - Copy Workbook to Member “My Documents” Folder (JavaScript)
Workbook - Copy Workbook to Member “My Documents” Folder (JavaScript)
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 2 require('dotenv').config({ path: 'sigma-api-recipes/.env' }); 3 4 // Import the function to obtain a bearer token from the authenticate-bearer module 5 const getBearerToken = require('../get-access-token'); 6 7 // Import Axios for making HTTP requests 8 const axios = require('axios'); 9 10 // Load use-case specific variables from environment variables 11 const baseURL = process.env.baseURL; // Base URL for the Sigma API 12 const workbookId = process.env.WORKBOOK_ID; // Workbook ID to copy 13 const 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 16 async 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 39 async 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 70 async 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 97 main();
Endpoints used
- Get authentication token:
getBearerTokenfunction - Get details of a user:
${baseURL}/members/${memberId} - Copy a workbook:
${baseURL}/workbooks/${workbookId}/copy

