Fixed issue with variables not being updated in Excel file

This commit is contained in:
denisacirstea
2025-09-12 12:37:08 +03:00
parent 2a55f69813
commit 1ae630d5d6
4 changed files with 76 additions and 35 deletions

View File

@@ -8,6 +8,9 @@ def update_excel_with_direct_xml(excel_path, store_name):
"""
Update all references to {store_name} in the Excel file by directly modifying XML
This function preserves all cell values and formulas that were previously set
by the update_excel_variables function.
Args:
excel_path: Path to the Excel file
store_name: The store name to replace {store_name} with
@@ -39,6 +42,10 @@ def update_excel_with_direct_xml(excel_path, store_name):
# Only modify XML files that might contain formulas or text
if item.filename.endswith('.xml'):
# Skip sheet8.xml which is the Variables sheet (based on common Excel structure)
if 'sheet8.xml' in item.filename:
print(f"Skipping Variables sheet: {item.filename}")
else:
# Convert to string for text replacement
try:
text_content = content.decode('utf-8')

View File

@@ -35,22 +35,33 @@ app.post('/calculate', async (req, res) => {
await updateConfig(formData);
console.log('Config file updated successfully');
// Run Python script to create Excel file
exec('python3 create_excel.py', (error, stdout, stderr) => {
if (error) {
console.error(`Error executing Python script: ${error}`);
console.error(`stderr: ${stderr}`);
} else {
// Run Python script to create Excel file synchronously
const { execSync } = require('child_process');
try {
console.log('Executing Python script...');
const stdout = execSync('python3 create_excel.py', { encoding: 'utf8' });
console.log(`Python script output: ${stdout}`);
}
});
// Send success response
// Send success response after Python script completes
res.json({
success: true,
message: 'Form data saved successfully'
message: 'Form data saved and Excel file created successfully'
});
console.log('Success response sent');
} catch (execError) {
console.error(`Error executing Python script: ${execError.message}`);
if (execError.stderr) {
console.error(`stderr: ${execError.stderr}`);
}
// Send error response for Python script failure
res.status(500).json({
success: false,
message: 'Error creating Excel file',
error: execError.message
});
console.error('Error response sent for Python script failure');
}
} catch (error) {
console.error('Error processing form data:', error);
console.error('Error stack:', error.stack);

View File

@@ -101,11 +101,17 @@ def update_excel_variables(excel_path):
# Update the cells
for cell_ref, value in cell_mappings.items():
try:
sheet[cell_ref] = value
# Force the value to be set, even if the cell is protected or has data validation
cell = sheet[cell_ref]
cell.value = value
print(f"Updated {cell_ref} with value: {value}")
except Exception as e:
print(f"Error updating cell {cell_ref}: {e}")
# Save the workbook immediately after updating the cells
print("Saving workbook after updating variables...")
wb.save(excel_path)
# Update sheet names - replace {store_name} with actual store name
store_name = user_data.get('store_name', '')
if store_name:
@@ -140,6 +146,23 @@ def update_excel_variables(excel_path):
# Reload the workbook to get the changes
wb = openpyxl.load_workbook(excel_path)
# Re-apply the variables to make sure they weren't overwritten
print("Re-applying variables after XML modification...")
try:
sheet = wb['Variables']
for cell_ref, value in cell_mappings.items():
try:
cell = sheet[cell_ref]
cell.value = value
print(f"Re-applied {cell_ref} with value: {value}")
except Exception as e:
print(f"Error re-applying cell {cell_ref}: {e}")
# Save the workbook again
wb.save(excel_path)
except Exception as e:
print(f"Error re-applying variables: {e}")
# Save the workbook
wb.save(excel_path)
print(f"Excel file updated successfully: {excel_path}")