import fs from "node:fs/promises"; import { FileBlob, SpreadsheetFile } from "@oai/artifact-tool"; const inputPath = "/Users/lingniu/Downloads/氢气计算公式.xlsx"; const outputDir = "/Users/lingniu/project/ai-coding/lingniu-vehicle-ingest/tmp/hydrogen-formula-analysis-20260826/preview"; await fs.mkdir(outputDir, { recursive: true }); const workbook = await SpreadsheetFile.importXlsx(await FileBlob.load(inputPath)); const overview = await workbook.inspect({ kind: "workbook,sheet,table", include: "id,name,values,formulas", maxChars: 12000, tableMaxRows: 30, tableMaxCols: 20, tableMaxCellChars: 300, }); console.log("OVERVIEW"); console.log(overview.ndjson); const sheets = (await workbook.inspect({ kind: "sheet", include: "id,name", maxChars: 6000 })).ndjson .split("\n") .filter(Boolean) .map((line) => JSON.parse(line)) .filter((item) => item.kind === "sheet"); for (let index = 0; index < sheets.length; index += 1) { const sheet = sheets[index]; const region = await workbook.inspect({ kind: "region,formula,computedStyle", sheetId: sheet.name, range: "A1:Z80", include: "values,formulas", maxChars: 30000, tableMaxRows: 80, tableMaxCols: 26, tableMaxCellChars: 500, options: { maxResults: 300 }, }); console.log(`SHEET ${sheet.name}`); console.log(region.ndjson); const rendered = await workbook.render({ sheetName: sheet.name, autoCrop: "all", scale: 2, format: "png" }); const safeName = String(sheet.name).replace(/[\\/:*?"<>|]/g, "_"); await fs.writeFile(`${outputDir}/${String(index + 1).padStart(2, "0")}-${safeName}.png`, new Uint8Array(await rendered.arrayBuffer())); } console.log(JSON.stringify({ inputPath, outputDir, sheetCount: sheets.length }));