Files
Aislo/tools/est-test/exchange.cjs
T

107 lines
4.8 KiB
JavaScript

const { pack, unpack } = require("./archive.cjs");
const xml = (value) =>
String(value ?? "").replace(
/[&<>"']/g,
(c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&apos;" })[c],
);
const column = (index) => {
let name = "";
for (index++; index; index = Math.floor((index - 1) / 26))
name = String.fromCharCode(65 + ((index - 1) % 26)) + name;
return name;
};
function sheet(rows) {
if (rows.length > 1048576 || rows.some((r) => r.length > 16384))
throw Error("Excel 행/열 한도 초과");
return `<?xml version="1.0" encoding="UTF-8"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetViews><sheetView workbookViewId="0"><pane ySplit="1" topLeftCell="A2" state="frozen"/></sheetView></sheetViews><sheetData>${rows
.map(
(r, i) =>
`<row r="${i + 1}">${r
.map((v, j) => {
const ref = column(j) + (i + 1);
if (v == null) return "";
if (typeof v === "number") {
if (!Number.isFinite(v)) throw Error("유효하지 않은 Excel 숫자");
return `<c r="${ref}"><v>${v}</v></c>`;
}
if (typeof v === "boolean") return `<c r="${ref}" t="b"><v>${v ? 1 : 0}</v></c>`;
const text = typeof v === "object" ? JSON.stringify(v) : String(v);
if (text.length > 32767) throw Error("Excel 셀 문자 한도 초과: " + ref);
return `<c r="${ref}" t="inlineStr"><is><t xml:space="preserve">${xml(text)}</t></is></c>`;
})
.join("")}</row>`,
)
.join("")}</sheetData><pageSetup paperSize="9" orientation="landscape"/></worksheet>`;
}
function workbook(project) {
const files = new Map(),
sheets = [];
for (const [name, rows] of Object.entries(project.tables)) {
if (!Array.isArray(rows) || name.startsWith("Cad") || name.length > 31) continue;
// Binary originals stay in JSON/ESTX; Excel edits scalar table fields only.
const keys = [
...new Set(
rows.flatMap((r) =>
Object.keys(r).filter(
(k) => !k.startsWith("_") && r[k] != null && typeof r[k] !== "object",
),
),
),
];
if (!keys.length) continue;
sheets.push({
name,
rows: [
keys,
...rows.map((r) =>
keys.map((k) => (typeof r[k] === "object" && r[k] != null ? null : r[k])),
),
],
});
}
sheets.unshift({
name: "공사정보",
rows: [
["항목", "값"],
["공사명", project.name],
["형식", "est-web-xlsx-1"],
["설명", "표의 첫 행은 필드명입니다. 바이너리는 포함하지 않습니다."],
],
});
sheets.forEach((s, i) => files.set(`xl/worksheets/sheet${i + 1}.xml`, sheet(s.rows)));
const ns = "http://schemas.openxmlformats.org/";
files.set(
"[Content_Types].xml",
`<?xml version="1.0"?><Types xmlns="${ns}package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>${sheets.map((s, i) => `<Override PartName="/xl/worksheets/sheet${i + 1}.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>`).join("")}</Types>`,
);
files.set(
"_rels/.rels",
`<?xml version="1.0"?><Relationships xmlns="${ns}package/2006/relationships"><Relationship Id="rId1" Type="${ns}officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>`,
);
files.set(
"xl/workbook.xml",
`<?xml version="1.0" encoding="UTF-8"?><workbook xmlns="${ns}spreadsheetml/2006/main" xmlns:r="${ns}officeDocument/2006/relationships"><sheets>${sheets.map((s, i) => `<sheet name="${xml(s.name)}" sheetId="${i + 1}" r:id="rId${i + 1}"/>`).join("")}</sheets><calcPr calcId="0" fullCalcOnLoad="1"/></workbook>`,
);
files.set(
"xl/_rels/workbook.xml.rels",
`<?xml version="1.0"?><Relationships xmlns="${ns}package/2006/relationships">${sheets.map((s, i) => `<Relationship Id="rId${i + 1}" Type="${ns}officeDocument/2006/relationships/worksheet" Target="worksheets/sheet${i + 1}.xml"/>`).join("")}</Relationships>`,
);
return pack(files);
}
function readWorkbook(bytes) {
const files = unpack(bytes),
selected = {};
if (!files.has("xl/workbook.xml") || !files.has("xl/_rels/workbook.xml.rels"))
throw Error("Excel 통합문서 구조 없음");
for (const [name, data] of files)
if (
name === "xl/workbook.xml" ||
name === "xl/_rels/workbook.xml.rels" ||
name === "xl/sharedStrings.xml" ||
/^xl\/worksheets\/[^/]+\.xml$/.test(name)
)
selected[name] = data.toString("utf8");
return selected;
}
module.exports = { workbook, readWorkbook, xml };