import fs from 'node:fs/promises'; import { Workbook } from '@oai/artifact-tool'; import { fileURLToPath } from 'node:url'; import path from 'node:path'; const dir=path.dirname(fileURLToPath(import.meta.url)); const plan=JSON.parse(await fs.readFile(path.join(dir,process.argv[2] ?? '20261013_manual_auth_p3-plan.json'),'utf8')); for(const p of plan){ const wb=await Workbook.fromCSV(await fs.readFile(path.join(dir,p.source_file),'utf8'),{sheetName:'ExistingRows'}); const sheet=wb.worksheets.getItem('ExistingRows'); const range=sheet.getRangeByIndexes(0,0,p.source_rows.length+1,p.fields.length); const original=range.values; if(JSON.stringify(original)!==JSON.stringify([p.fields,...p.source_rows]))throw Error('CSV source preservation failed'); for(const c of p.changes){const cell=sheet.getCell(c.row,c.column);if(cell.values[0][0]!==c.old)throw Error('Value conflict');cell.values=[[c.value]];} wb.recalculate(); const result=range.values; for(const c of p.changes)if(result[c.row][c.column]!==c.value)throw Error('Cell readback mismatch'); await fs.writeFile(path.join(dir,p.output_json),JSON.stringify(result)); console.log(JSON.stringify({kind:p.kind,editedRows:p.source_rows.length,editedCells:p.changes.length,newIds:0,format:'CSV preserving unrelated records'})); }