Create and modify Excel workbooks with mandatory deterministic Python verification of workbook structure, required sheets, populated columns, and sample counts before reporting completion.
59
68%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
View guide
Passed
No findings from the security scan
Fix and improve this skill with Tessl
tessl review fix ./benchmarks/gdpval/skills/spreadsheet-audit-edit-validate-enhanced/SKILL.mdUse this workflow whenever you need to create or modify an Excel workbook where the output must match an expected structure. The goal is to avoid delivering a workbook that does not satisfy the user's required tabs, columns, or counts by using deterministic Python verification.
Important: This workflow has two distinct modes:
| Task Type | Pre-edit Audit | Post-write Verification |
|---|---|---|
| Modify existing workbook | Full audit required (Phases 1-2) | Full verification required (Phase 4) |
| Create new workbook from source/reference data | Skip pre-edit audit (no existing file) | Deterministic Python verification REQUIRED (Phase 4) |
This is a workflow skill, not a tool-specific recipe. Apply it with any spreadsheet-capable tooling, but Phase 4 verification uses a deterministic Python script.
Use this workflow when:
.xlsx or .xlsm files may existBefore proceeding, determine which workflow mode applies:
Mode A: Modify existing workbook - Use the full workflow (all phases) when there is an existing workbook file that must be updated in place, the user references an existing file, or the task requires preserving existing data/formulas/formatting.
Mode B: Create new workbook from source/reference data - Adapt Phase 1-2 to audit SOURCE reference files instead of targeting a non-existent workbook. Phase 1 locates and inspects source data files; Phase 2 audits their structure (sheet names, columns, data types, row counts, merged cells, formulas). Skip target workbook pre-edit audit (it doesn't exist yet), but DO audit the source/reference files thoroughly before creating. Phase 4 (deterministic Python verification) is MANDATORY for create mode.
For modify-existing mode: Never write first. Verify before editing.
For create-new mode: You must create the file, but verify thoroughly after creation using a deterministic Python script.
Always perform these phases in order:
If any check fails, stop and resolve the mismatch before continuing.
All spreadsheet operations must be anchored to the exact workspace path provided for the task.
For modify-existing mode: Discovery, inspection, editing, and verification must use the anchored path
For create-new mode: The output path must be under the anchored workspace; verify the created file from that path
Treat the provided workspace path as the authoritative root for all file operations
Resolve the selected workbook to an exact path under that workspace before editing
Do not switch to a different directory, fallback path, temp copy, or similarly named workbook unless the user explicitly authorizes it
If a delegated tool reports a workbook path, independently verify that exact path from the current workspace before trusting the result
If the workbook cannot be found at the anchored workspace path, stop rather than guessing
Create mode: In create mode, Phase 1 focuses on locating SOURCE reference data files rather than a target workbook. Audit these source files to understand their structure before creating the new workbook.
Search the filesystem broadly enough to find plausible workbooks, then narrow to the file most likely intended by the user.
Search for:
.xlsx.xlsm.xlsPrefer files that:
If several files are plausible, inspect them all before choosing.
Before deciding "this is the workbook to edit", confirm at least:
The exact path must be recorded as a workspace-anchored path, not a vague filename.
Create mode: In create mode, Phase 2 audits SOURCE reference files to understand their structure before creating the new workbook. This prevents calculation errors, missing data, or structural mismatches.
Before making any edits, inspect the workbook and compare it to the user specification.
Extract the following if present:
Turn these into explicit checks.
Modify mode: Verify the target workbook structure before editing.
Create mode: Audit source/reference data files to document their structure (sheet names, column names, data types, row counts, merged cells, formulas, data quality issues). Use this audit to plan the target workbook structure.
For each relevant workbook:
Create mode source audit checklist:
Before any write, confirm that repeated inspections agree on workbook identity and structure.
At minimum, compare across inspections:
If these differ across runs or tools in any unexplained way, treat the workbook identity as unconfirmed and do not edit until the discrepancy is resolved.
Proceed only if all of the following are true:
If not, stop and report the discrepancy.
After the workbook passes the pre-edit audit (modify mode) or when creating new (create mode):
During the edit, record what changed:
For create-new mode: Document the intended structure (sheet names, columns, expected row counts) before writing, so Phase 4 can verify against this specification. This documentation must be based on the source data audit from Phase 2.
After saving, create and execute a deterministic Python verification script that independently validates the saved workbook against the original specification.
This phase is MANDATORY for all modes and must use a direct Python script with openpyxl (or equivalent) for deterministic verification.
Write a Python script that:
#!/usr/bin/env python3
"""Deterministic workbook structure verification script."""
from openpyxl import load_workbook
import sys
from pathlib import Path
def verify_workbook(workbook_path, specification):
"""
Verify workbook structure against specification.
Args:
workbook_path: Exact path to the saved workbook
specification: Dict with required_sheets, required_columns,
expected_row_counts, sample_criteria, etc.
Returns:
dict with verification_results, passed (bool), errors (list)
"""
results = {
'passed': True,
'errors': [],
'warnings': [],
'details': {}
}
# Load workbook
try:
wb = load_workbook(workbook_path, data_only=True)
results['details']['workbook_loaded'] = True
except Exception as e:
results['passed'] = False
results['errors'].append(f"Failed to load workbook: {e}")
return results
# Verify required sheets
actual_sheets = wb.sheetnames
required_sheets = specification.get('required_sheets', [])
for sheet_name in required_sheets:
if sheet_name in actual_sheets:
results['details'][f'sheet_{sheet_name}'] = 'present'
else:
results['passed'] = False
results['errors'].append(f"Missing required sheet: {sheet_name}")
results['details'][f'sheet_{sheet_name}'] = 'missing'
# Verify each required sheet
for sheet_name in required_sheets:
if sheet_name not in actual_sheets:
continue
ws = wb[sheet_name]
# Get headers from first row
headers = []
if ws.max_row >= 1:
for col in range(1, ws.max_column + 1):
cell_value = ws.cell(row=1, column=col).value
headers.append(str(cell_value).strip() if cell_value else '')
results['details'][f'{sheet_name}_headers'] = headers
# Verify required columns for this sheet
sheet_required_cols = specification.get('required_columns', {}).get(sheet_name, [])
for col_name in sheet_required_cols:
if col_name in headers:
results['details'][f'{sheet_name}_col_{col_name}'] = 'present'
else:
results['passed'] = False
results['errors'].append(
f"Missing required column '{col_name}' in sheet '{sheet_name}'"
)
results['details'][f'{sheet_name}_col_{col_name}'] = 'missing'
# Count data rows (excluding header)
data_row_count = max(0, ws.max_row - 1) if ws.max_row > 1 else 0
results['details'][f'{sheet_name}_row_count'] = data_row_count
# Verify expected row counts
expected_count = specification.get('expected_row_counts', {}).get(sheet_name)
if expected_count is not None:
if data_row_count < expected_count:
results['passed'] = False
results['errors'].append(
f"Sheet '{sheet_name}' has {data_row_count} rows, expected at least {expected_count}"
)
# Verify populated columns (spot check)
for col_name in sheet_required_cols:
if col_name not in headers:
continue
col_idx = headers.index(col_name) + 1
# Check first few data rows are populated
populated_count = 0
for row in range(2, min(ws.max_row + 1, 11)):
cell_value = ws.cell(row=row, column=col_idx).value
if cell_value is not None and str(cell_value).strip() != '':
populated_count += 1
if populated_count == 0 and data_row_count > 0:
results['warnings'].append(
f"Column '{col_name}' in sheet '{sheet_name}' appears empty"
)
# Verify sample counts if applicable
sample_mark_col = specification.get('sample_mark_column')
sample_mark_value = specification.get('sample_mark_value', 'Yes')
expected_sample_count = specification.get('expected_sample_count')
if sample_mark_col and expected_sample_count is not None:
for sheet_name in required_sheets:
if sheet_name not in actual_sheets:
continue
ws = wb[sheet_name]
headers = []
if ws.max_row >= 1:
for col in range(1, ws.max_column + 1):
cell_value = ws.cell(row=1, column=col).value
headers.append(str(cell_value).strip() if cell_value else '')
if sample_mark_col not in headers:
continue
col_idx = headers.index(sample_mark_col) + 1
marked_count = 0
for row in range(2, ws.max_row + 1):
cell_value = ws.cell(row=row, column=col_idx).value
if cell_value is not None and str(cell_value).strip() == sample_mark_value:
marked_count += 1
results['details'][f'{sheet_name}_marked_count'] = marked_count
if marked_count != expected_sample_count:
results['passed'] = False
results['errors'].append(
f"Expected {expected_sample_count} marked rows in '{sheet_name}', found {marked_count}"
)
# Verify sampling criteria if applicable
sampling_criteria = specification.get('sampling_criteria', [])
if sampling_criteria:
# Check that each criterion is represented in marked rows
# This requires checking the actual marked row content
for criterion in sampling_criteria:
criterion_found = False
for sheet_name in required_sheets:
if sheet_name not in actual_sheets:
continue
ws = wb[sheet_name]
# Simplified check - look for criterion value in marked rows
# Full implementation would check specific columns
for row in range(2, ws.max_row + 1):
for col in range(1, ws.max_column + 1):
cell_value = ws.cell(row=row, column=col).value
if cell_value is not None and str(cell_value).strip() == criterion:
criterion_found = True
break
if criterion_found:
break
if criterion_found:
break
results['details'][f'criterion_{criterion}'] = 'found' if criterion_found else 'missing'
if not criterion_found:
# Check if fallback is permitted
if not specification.get('criterion_fallback_permitted', False):
results['passed'] = False
results['errors'].append(
f"Sampling criterion '{criterion}' not found in marked rows"
)
else:
results['warnings'].append(
f"Sampling criterion '{criterion}' not found (fallback permitted)"
)
return results
def main():
if len(sys.argv) < 3:
print("Usage: verify_workbook.py <workbook_path> <spec_json>")
sys.exit(1)
workbook_path = sys.argv[1]
spec_json = sys.argv[2]
import json
specification = json.loads(spec_json)
results = verify_workbook(workbook_path, specification)
# Print structured results
print("=" * 60)
print("WORKBOOK VERIFICATION RESULTS")
print("=" * 60)
print(f"Workbook: {workbook_path}")
print(f"Status: {'PASSED' if results['passed'] else 'FAILED'}")
print()
if results['errors']:
print("ERRORS:")
for err in results['errors']:
print(f" ✗ {err}")
print()
if results['warnings']:
print("WARNINGS:")
for warn in results['warnings']:
print(f" ⚠ {warn}")
print()
print("DETAILS:")
for key, value in results['details'].items():
print(f" {key}: {value}")
print("=" * 60)
sys.exit(0 if results['passed'] else 1)
if __name__ == '__main__':
main()After creating the output workbook:
Define the verification specification as a JSON object with:
required_sheets: List of sheet names that must existrequired_columns: Dict mapping sheet names to lists of required column headersexpected_row_counts: Dict mapping sheet names to minimum expected row countsexpected_sample_count: Number of rows that should be marked/selectedsample_mark_column: Name of the column used to mark selected rowssample_mark_value: Value indicating a row is marked (e.g., "Yes", "TRUE", 1)sampling_criteria: List of values that must appear in marked rowscriterion_fallback_permitted: Boolean - whether missing criteria are acceptableWrite the verification script to a file in the workspace
Execute the script using execute_code_sandbox or run_shell:
python3 verify_workbook.py /path/to/output.xlsx '<spec_json>'Interpret results:
Status: PASSED with no errors: Proceed to report completionStatus: FAILED with errors: Review errors, fix the workbook, re-verifyOnly report completion if:
Status: PASSEDIf verification fails:
Post-write verification must include an independent direct file read of the saved workbook from the anchored workspace path.
At minimum, the verification script must validate:
pip install openpyxl if needed)If the verification script cannot load or inspect the workbook:
A task using this workflow is complete only when:
Status: PASSED with no errorsNever report completion without successful Phase 4 verification.
c5a9c4b
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.