Tutorial: Batch Protecting Multiple Excel Files with Aspose.Cells Cloud API
Prerequisites
Before starting this tutorial, make sure you have:
- An Aspose Cloud account with an active subscription or free trial
- Your Client ID and Client Secret from the Aspose Cloud Dashboard
- Multiple Excel files uploaded to your cloud storage
- Basic understanding of Excel protection features and security best practices
Introduction to Batch Protection
Excel file protection is crucial for securing sensitive business data, financial information, and proprietary content. When dealing with multiple files, applying protection individually can be time-consuming. Aspose.Cells Cloud API offers batch protection functionality that allows you to:
- Protect multiple Excel files simultaneously
- Apply consistent security settings across files
- Save time and reduce API calls
- Ensure all critical files are properly secured
In this tutorial, you’ll learn how to implement batch protection operations efficiently.
Understanding Protection Types
Excel files can be protected at different levels with various protection types:
Workbook Protection Types
- Structure: Prevents users from adding, deleting, renaming, or moving worksheets
- Windows: Prevents users from changing the size or position of workbook windows
- All: Applies both structure and windows protection
Worksheet Protection Types
- Contents: Prevents changes to cell data
- Objects: Prevents changes to shapes, charts, and other objects
- Scenarios: Prevents changes to saved scenarios
- Format: Prevents changes to formatting
- All: Applies comprehensive protection to the worksheet
Setting Up Batch Protection
The batch protection endpoint accepts a JSON request with these key components:
- SourceFolder: The folder in cloud storage containing your Excel files
- MatchCondition: Rules to determine which files to protect (regex or exact matches)
- ProtectionType: The type of protection to apply (“Structure”, “Windows”, “All”, etc.)
- Password: The password to set for protected files
- OutFolder: The destination folder for the protected files
Let’s examine each component in detail.
File Selection with MatchCondition
The MatchCondition
property specifies which files to include in the batch protection:
- RegexPattern: A regular expression pattern to match filenames
- FullMatchConditions: An array of exact filename matches
For example, to protect all Excel files that start with “Financial” and end with “.xlsx”:
"MatchCondition": {
"RegexPattern": "(^Financial)(.+)(xlsx$)"
}
Protection Type and Password
The ProtectionType
specifies what kind of protection to apply, and the Password
sets the password that will be required to remove the protection:
"ProtectionType": "All",
"Password": "YourSecurePassword123"
Always use strong, secure passwords (mix of letters, numbers, and special characters) to protect sensitive files.
Implementing Batch Protection
Now let’s implement batch protection using different programming languages.
Try It Yourself: Using cURL
curl -v "https://api.aspose.cloud/v3.0/cells/batch/protect" \
-X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer <your_jwt_token>" \
-d "{
\"SourceFolder\": \"FinancialReports\",
\"OutFolder\": \"ProtectedReports\",
\"MatchCondition\": {
\"RegexPattern\": \"(^Financial)(.+)(xlsx$)\"
},
\"Password\": \"Secure@Password123\",
\"ProtectionType\": \"All\"
}"
Replace <your_jwt_token>
with your actual authentication token. This request will protect all Excel files in the “FinancialReports” folder whose names start with “Financial” and end with “xlsx”, placing the protected files in the “ProtectedReports” folder.
Implementation in C#
// Initialize the API with your client credentials
CellsApi cellsApi = new CellsApi(clientId, clientSecret);
// Upload some files for testing (if needed)
var uploadFolder = "FinancialReports";
cellsApi.UploadFile(new UploadFileRequest(
uploadFiles: new Dictionary<string, Stream> {
{ "Financial_Q1_2025.xlsx", File.OpenRead("Financial_Q1_2025.xlsx") }
},
path: $"{uploadFolder}/Financial_Q1_2025.xlsx"
));
cellsApi.UploadFile(new UploadFileRequest(
uploadFiles: new Dictionary<string, Stream> {
{ "Financial_Q2_2025.xlsx", File.OpenRead("Financial_Q2_2025.xlsx") }
},
path: $"{uploadFolder}/Financial_Q2_2025.xlsx"
));
// Create a match condition to select files
var matchCondition = new MatchConditionRequest
{
RegexPattern = "(^Financial)(.+)(xlsx$)"
};
// Prepare the batch protect request
var batchRequest = new BatchProtectRequest
{
SourceFolder = uploadFolder,
OutFolder = "ProtectedReports",
MatchCondition = matchCondition,
Password = "Secure@Password123",
ProtectionType = "All"
};
// Execute the batch protection
var result = cellsApi.PostBatchProtect(new PostBatchProtectRequest(
batchProtectRequest: batchRequest
));
Console.WriteLine($"Batch protection completed with status: {result.Status}");
Implementation in Java
// Initialize API client with your credentials
CellsApi cellsApi = new CellsApi(clientId, clientSecret);
// Upload some files for testing (if needed)
String uploadFolder = "FinancialReports";
cellsApi.uploadFile(new UploadFileRequest(
new File("Financial_Q1_2025.xlsx"),
uploadFolder + "/Financial_Q1_2025.xlsx",
null
));
cellsApi.uploadFile(new UploadFileRequest(
new File("Financial_Q2_2025.xlsx"),
uploadFolder + "/Financial_Q2_2025.xlsx",
null
));
// Create a match condition to select files
MatchConditionRequest matchCondition = new MatchConditionRequest();
matchCondition.setRegexPattern("(^Financial)(.+)(xlsx$)");
// Prepare the batch protect request
BatchProtectRequest batchRequest = new BatchProtectRequest();
batchRequest.setSourceFolder(uploadFolder);
batchRequest.setOutFolder("ProtectedReports");
batchRequest.setMatchCondition(matchCondition);
batchRequest.setPassword("Secure@Password123");
batchRequest.setProtectionType("All");
// Execute the batch protection
CellsCloudResponse response = cellsApi.postBatchProtect(
new PostBatchProtectRequest(batchRequest)
);
System.out.println("Batch protection completed with status: " + response.getStatus());
Implementation in Python
import requests
import json
# Authentication setup
client_id = 'your_client_id'
client_secret = 'your_client_secret'
# Get authentication token
auth_url = "https://api.aspose.cloud/connect/token"
auth_data = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret
}
auth_response = requests.post(auth_url, data=auth_data)
token = auth_response.json().get('access_token')
# API endpoint
url = "https://api.aspose.cloud/v3.0/cells/batch/protect"
# Prepare the request payload
payload = {
"SourceFolder": "FinancialReports",
"OutFolder": "ProtectedReports",
"MatchCondition": {
"RegexPattern": "(^Financial)(.+)(xlsx$)"
},
"Password": "Secure@Password123",
"ProtectionType": "All"
}
# Set headers
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {token}'
}
# Execute the API call
response = requests.post(url, headers=headers, data=json.dumps(payload))
# Check if successful
if response.status_code == 200:
print("Batch protection completed successfully!")
print(response.json())
else:
print(f"Error: {response.status_code}")
print(response.text)
Advanced Batch Protection Techniques
Protecting Specific Worksheets
To protect only specific worksheets within workbooks, you can use the batch processing first and then apply worksheet-specific protection:
// After batch protection, apply worksheet-specific protection
foreach (var filename in protectedFilesList)
{
// Get workbook from storage
var workbook = cellsApi.GetWorkbook(new GetWorkbookRequest(
name: filename,
folder: "ProtectedReports"
));
// Apply worksheet protection to Sheet1
cellsApi.PutProtectWorksheet(new PutProtectWorksheetRequest(
name: filename,
sheetName: "Sheet1",
protectWorksheetParameter: new ProtectWorksheetParameter
{
Password = "SheetPassword123",
ProtectionType = "All" // or use specific types like "Contents", "Objects"
},
folder: "ProtectedReports"
));
}
Using Exact Filename Matching
Instead of regex patterns, you can specify exact filenames for more precise control:
var matchCondition = new MatchConditionRequest
{
FullMatchConditions = new List<string> {
"Financial_Q1_2025.xlsx",
"Financial_Q2_2025.xlsx"
}
};
Applying Different Protection Types
You can run multiple batch operations with different protection types:
// First batch: Structure protection for all files
batchRequest.ProtectionType = "Structure";
var result1 = cellsApi.PostBatchProtect(new PostBatchProtectRequest(
batchProtectRequest: batchRequest
));
// Second batch: Contents protection for specific files
batchRequest.ProtectionType = "Contents";
batchRequest.MatchCondition.RegexPattern = "(^Sensitive)(.+)(xlsx$)";
var result2 = cellsApi.PostBatchProtect(new PostBatchProtectRequest(
batchProtectRequest: batchRequest
));
Managing Protected Files
Verifying Protection Status
After applying batch protection, you might want to verify that your files are properly protected:
// Get the list of files in the output folder
var filesList = cellsApi.GetFilesList(new GetFilesListRequest(
path: "ProtectedReports"
));
// Check each file's protection status
foreach (var fileInfo in filesList.Value)
{
if (fileInfo.IsFolder)
continue;
var workbookInfo = cellsApi.GetWorkbookInfo(new GetWorkbookInfoRequest(
name: fileInfo.Name,
folder: "ProtectedReports"
));
Console.WriteLine($"File: {fileInfo.Name}");
Console.WriteLine($" - Is Protected: {workbookInfo.IsProtected}");
Console.WriteLine($" - Protection Type: {workbookInfo.ProtectionType}");
}
Removing Protection When Needed
To unprotect a file when you need to modify it:
// Unprotect a workbook
cellsApi.DeleteWorkbookProtection(new DeleteWorkbookProtectionRequest(
name: "Financial_Q1_2025.xlsx",
password: "Secure@Password123",
folder: "ProtectedReports"
));
// Make changes to the file...
// Re-protect the workbook
cellsApi.PutWorkbookProtection(new PutWorkbookProtectionRequest(
name: "Financial_Q1_2025.xlsx",
protectWorkbookRequest: new ProtectWorkbookRequest
{
Password = "Secure@Password123",
ProtectionType = "All"
},
folder: "ProtectedReports"
));
Best Practices for Excel Protection
Security Recommendations
- Use Strong Passwords: Include a mix of uppercase, lowercase, numbers, and special characters.
- Manage Passwords Securely: Store passwords in a secure password manager, not in code.
- Layer Your Protection: Apply protection at both workbook and worksheet levels for critical files.
- Regular Updates: Periodically update protection settings and passwords.
- Limited Access: Restrict access to protected files only to those who need it.
Protection Limitations
Excel protection has some inherent limitations you should be aware of:
- Not Encryption: Worksheet protection is not the same as file encryption and can be bypassed by determined users.
- VBA Access: Users with VBA knowledge might be able to work around some protections.
- Old Versions: Older Excel versions might handle protection differently.
For the highest level of security, consider combining Excel protection with file encryption and secure file sharing practices.
Troubleshooting Common Issues
Issue: “Access Denied” Errors
Solution: Verify your authentication token is valid and has the necessary permissions to access and modify files in the specified folders.
Issue: Files Not Being Protected
Solution: Double-check your regex pattern or exact file matches. Test your regex pattern using an online regex tester to ensure it matches the intended files.
Issue: Protection Applied but Files Can Still Be Modified
Solution: Excel protection has limitations. For sensitive data, consider using stronger file encryption methods in addition to Excel’s built-in protection.
What You’ve Learned
In this tutorial, you’ve learned:
- How to apply batch protection to multiple Excel files with a single API request
- How to select specific files for protection using regex patterns and exact matches
- How to implement different protection types for various security needs
- How to manage protected files, including verification and removal of protection
- Best practices and limitations of Excel file protection
Further Practice
To reinforce your learning, try these exercises:
- Create a batch protection process that applies different protection types to different sets of files
- Implement a solution that tracks which files are protected and when they were last modified
- Develop a workflow that temporarily unprotects files, updates data, and then re-protects them
Next Steps
Continue your learning journey with these related tutorials: