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
// Load environment variables from a specific .env file for configurationrequire('dotenv').config({ path: 'sigma-api-recipes/.env' });// Import the function to obtain a bearer token from the authenticate-bearer moduleconst getBearerToken = require('../get-access-token');// Import Axios for making HTTP requestsconst axios = require('axios');// Load use-case specific variables from environment variablesconst baseURL = process.env.baseURL; // Base URL for the Sigma APIconst workbookId = process.env.WORKBOOK_ID; // Workbook ID to copyconst memberId = process.env.MEMBERID; // ID of the member whose "My Documents" folder we'll use// Function to retrieve the ID of the member's "My Documents" folderasync function getMyDocumentsFolderId(accessToken) {try {// Send the request to retrieve the member's detailsconst response = await axios.get(`${baseURL}/members/${memberId}`,{ headers: { Authorization: `Bearer ${accessToken}` } });// Extract the ID of the member's "My Documents" folderconst homeFolderId = response.data.homeFolderId;// Log the retrieved folder IDconsole.log('Retrieved "My Documents" folder ID:', homeFolderId);return homeFolderId;} catch (error) {// Log any errors that occur during the processconsole.error('Failed to retrieve "My Documents" folder ID:', error.response ? error.response.data : error);return null;}}// Function to copy the workbook to the specified folderasync function copyWorkbook(accessToken, destinationFolderId) {try {// Define the request payload to copy the workbookconst copyPayload = {name: 'New Workbook Name', // Specify the name of the copied workbookdescription: 'Description of the copied workbook', // Specify the description of the copied workbookownerId: memberId, // Specify the ID of the user who will own the copied workbookdestinationFolderId: destinationFolderId // Use the passed destinationFolderId argument};// Send the request to copy the workbookconst response = await axios.post(`${baseURL}/workbooks/${workbookId}/copy`,copyPayload,{ headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json' } });// Log the success message and copied workbook detailsconsole.log('Workbook copy initiated successfully.');console.log('Copied workbook details:', response.data);// Return the ID of the copied workbook for further processing if neededreturn response.data.workbookId;} catch (error) {// Log any errors that occur during the processconsole.error('Failed to copy workbook:', error.response ? error.response.data : error);return null;}}// Main function to execute the workflowasync function main() {// Obtain the bearer token for authenticationconst accessToken = await getBearerToken();if (!accessToken) {console.error('Failed to obtain bearer token.');return;}// Retrieve the ID of the member's "My Documents" folderconst myDocumentsFolderId = await getMyDocumentsFolderId(accessToken);if (!myDocumentsFolderId) {console.error('Failed to retrieve "My Documents" folder ID.');return;}// Copy the workbook and place it in the "My Documents" folderconst copiedWorkbookId = await copyWorkbook(accessToken, myDocumentsFolderId); // Pass myDocumentsFolderId hereif (!copiedWorkbookId) {console.error('Failed to copy workbook.');return;}// Perform any additional actions with the copied workbook if neededconsole.log(`Workbook successfully copied with ID: ${copiedWorkbookId}`);}// Execute the main functionmain();
Endpoints used
- Get authentication token:
getBearerTokenfunction - Get details of a user:
${baseURL}/members/${memberId} - Copy a workbook:
${baseURL}/workbooks/${workbookId}/copy

