Fixed issue with variables not being updated in Excel file
This commit is contained in:
@@ -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
|
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:
|
Args:
|
||||||
excel_path: Path to the Excel file
|
excel_path: Path to the Excel file
|
||||||
store_name: The store name to replace {store_name} with
|
store_name: The store name to replace {store_name} with
|
||||||
@@ -39,26 +42,30 @@ def update_excel_with_direct_xml(excel_path, store_name):
|
|||||||
|
|
||||||
# Only modify XML files that might contain formulas or text
|
# Only modify XML files that might contain formulas or text
|
||||||
if item.filename.endswith('.xml'):
|
if item.filename.endswith('.xml'):
|
||||||
# Convert to string for text replacement
|
# Skip sheet8.xml which is the Variables sheet (based on common Excel structure)
|
||||||
try:
|
if 'sheet8.xml' in item.filename:
|
||||||
text_content = content.decode('utf-8')
|
print(f"Skipping Variables sheet: {item.filename}")
|
||||||
|
else:
|
||||||
# Check if this file contains our placeholder
|
# Convert to string for text replacement
|
||||||
if '{store_name}' in text_content:
|
try:
|
||||||
# Count occurrences before replacement
|
text_content = content.decode('utf-8')
|
||||||
occurrences = text_content.count('{store_name}')
|
|
||||||
total_replacements += occurrences
|
|
||||||
|
|
||||||
# Replace all instances of {store_name} with the actual store name
|
# Check if this file contains our placeholder
|
||||||
modified_content = text_content.replace('{store_name}', store_name)
|
if '{store_name}' in text_content:
|
||||||
|
# Count occurrences before replacement
|
||||||
# Convert back to bytes
|
occurrences = text_content.count('{store_name}')
|
||||||
content = modified_content.encode('utf-8')
|
total_replacements += occurrences
|
||||||
|
|
||||||
print(f"Replaced {occurrences} instances of '{{store_name}}' in {item.filename}")
|
# Replace all instances of {store_name} with the actual store name
|
||||||
except UnicodeDecodeError:
|
modified_content = text_content.replace('{store_name}', store_name)
|
||||||
# Not a text file, leave as is
|
|
||||||
pass
|
# Convert back to bytes
|
||||||
|
content = modified_content.encode('utf-8')
|
||||||
|
|
||||||
|
print(f"Replaced {occurrences} instances of '{{store_name}}' in {item.filename}")
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
# Not a text file, leave as is
|
||||||
|
pass
|
||||||
|
|
||||||
# Write the file (original or modified) to the new zip
|
# Write the file (original or modified) to the new zip
|
||||||
zip_out.writestr(item, content)
|
zip_out.writestr(item, content)
|
||||||
|
|||||||
41
server.js
41
server.js
@@ -35,22 +35,33 @@ app.post('/calculate', async (req, res) => {
|
|||||||
await updateConfig(formData);
|
await updateConfig(formData);
|
||||||
console.log('Config file updated successfully');
|
console.log('Config file updated successfully');
|
||||||
|
|
||||||
// Run Python script to create Excel file
|
// Run Python script to create Excel file synchronously
|
||||||
exec('python3 create_excel.py', (error, stdout, stderr) => {
|
const { execSync } = require('child_process');
|
||||||
if (error) {
|
try {
|
||||||
console.error(`Error executing Python script: ${error}`);
|
console.log('Executing Python script...');
|
||||||
console.error(`stderr: ${stderr}`);
|
const stdout = execSync('python3 create_excel.py', { encoding: 'utf8' });
|
||||||
} else {
|
console.log(`Python script output: ${stdout}`);
|
||||||
console.log(`Python script output: ${stdout}`);
|
|
||||||
|
// Send success response after Python script completes
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
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
|
||||||
// Send success response
|
res.status(500).json({
|
||||||
res.json({
|
success: false,
|
||||||
success: true,
|
message: 'Error creating Excel file',
|
||||||
message: 'Form data saved successfully'
|
error: execError.message
|
||||||
});
|
});
|
||||||
console.log('Success response sent');
|
console.error('Error response sent for Python script failure');
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing form data:', error);
|
console.error('Error processing form data:', error);
|
||||||
console.error('Error stack:', error.stack);
|
console.error('Error stack:', error.stack);
|
||||||
|
|||||||
Binary file not shown.
@@ -101,10 +101,16 @@ def update_excel_variables(excel_path):
|
|||||||
# Update the cells
|
# Update the cells
|
||||||
for cell_ref, value in cell_mappings.items():
|
for cell_ref, value in cell_mappings.items():
|
||||||
try:
|
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}")
|
print(f"Updated {cell_ref} with value: {value}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error updating cell {cell_ref}: {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
|
# Update sheet names - replace {store_name} with actual store name
|
||||||
store_name = user_data.get('store_name', '')
|
store_name = user_data.get('store_name', '')
|
||||||
@@ -139,6 +145,23 @@ def update_excel_variables(excel_path):
|
|||||||
os.remove(modified_file)
|
os.remove(modified_file)
|
||||||
# Reload the workbook to get the changes
|
# Reload the workbook to get the changes
|
||||||
wb = openpyxl.load_workbook(excel_path)
|
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
|
# Save the workbook
|
||||||
wb.save(excel_path)
|
wb.save(excel_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user