(args: {
binary: Buffer
workspaceId: string
})
| 30 | * sandbox/infra failure. |
| 31 | */ |
| 32 | export async function recalcXlsx(args: { |
| 33 | binary: Buffer |
| 34 | workspaceId: string |
| 35 | }): Promise<XlsxRecalcResult> { |
| 36 | const script = ` |
| 37 | import json, openpyxl |
| 38 | |
| 39 | ERR = {"#REF!", "#DIV/0!", "#VALUE!", "#NAME?", "#N/A", "#NULL!", "#NUM!"} |
| 40 | |
| 41 | # Input is already recalculated by compileDoc, so cached values are present. |
| 42 | wb = openpyxl.load_workbook("/home/user/input.xlsx", data_only=True) |
| 43 | errors = [] |
| 44 | for ws in wb.worksheets: |
| 45 | for row in ws.iter_rows(): |
| 46 | for c in row: |
| 47 | if isinstance(c.value, str) and c.value.strip() in ERR: |
| 48 | errors.append({"sheet": ws.title, "cell": c.coordinate, "error": c.value.strip()}) |
| 49 | |
| 50 | print("__SIM_RESULT__=" + json.dumps({"ok": len(errors) == 0, "errors": errors[:${MAX_REPORTED_ERRORS}]})) |
| 51 | `.trim() |
| 52 | |
| 53 | const result = await executeInE2B({ |
| 54 | code: script, |
| 55 | language: CodeLanguage.Python, |
| 56 | timeoutMs: RECALC_TIMEOUT_MS, |
| 57 | sandboxKind: 'doc', |
| 58 | sandboxFiles: [ |
| 59 | { |
| 60 | path: '/home/user/input.xlsx', |
| 61 | content: args.binary.toString('base64'), |
| 62 | encoding: 'base64', |
| 63 | }, |
| 64 | ], |
| 65 | }) |
| 66 | |
| 67 | if (result.error) { |
| 68 | throw new Error(`Spreadsheet recalc failed: ${result.error}`) |
| 69 | } |
| 70 | const payload = result.result as XlsxRecalcResult | null |
| 71 | if (!payload || typeof payload.ok !== 'boolean') { |
| 72 | logger.warn('Recalc returned no structured result', { workspaceId: args.workspaceId }) |
| 73 | return { ok: true, errors: [] } |
| 74 | } |
| 75 | return { ok: payload.ok, errors: Array.isArray(payload.errors) ? payload.errors : [] } |
| 76 | } |
| 77 | |
| 78 | /** Single-line summary of the first few formula errors, for the compiled-check result. */ |
| 79 | export function formatXlsxErrors(errors: XlsxCellError[]): string { |
no test coverage detected