261021_01_ENH: 1차 마이그레이션 완료
This commit is contained in:
121
src/lib/report-excel.ts
Normal file
121
src/lib/report-excel.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import type { ReportCell, ReportResult, ReportRow } from "@/types/report";
|
||||
import type { QuotaCell, QuotaResult } from "@/types/quota";
|
||||
import { isTransposed, legacyCellText } from "@/lib/report-ui";
|
||||
|
||||
/**
|
||||
* Excel export input (`AggregateExcelTableDto`). The server writes the legacy MakeExcel layout
|
||||
* from the legacy report body: one object per row with the same keys, header and merged cells as
|
||||
* `"text|rowSpan|colSpan"` (empty span = 1), cells covered by a merge as `""`, and the remaining
|
||||
* cells as their value: N as count text (written "(n)"), percentages and statistics as unrounded
|
||||
* numbers with a dot (numeric cells formatted 0.0, Mean 0.00), labels as text.
|
||||
*/
|
||||
export type ExcelTable = {
|
||||
head: string;
|
||||
vname: string;
|
||||
qtype: string;
|
||||
body: Record<string, string>[];
|
||||
imagePng?: string;
|
||||
};
|
||||
export type ExcelRow = Record<string, string>;
|
||||
|
||||
/** Legacy Report.cs span text: 1 is written as an empty span. */
|
||||
const span = (value: number | undefined) => (value && value > 1 ? String(value) : "");
|
||||
const anchor = (text: string, rowSpan?: number, colSpan?: number) =>
|
||||
text + "|" + span(rowSpan) + "|" + span(colSpan);
|
||||
|
||||
const columnKey = (index: number) => "c" + index;
|
||||
|
||||
/** Places cells on a full grid so every row has the same keys; merged-over cells stay "". */
|
||||
function grid(rows: { cells: { columnIndex: number }[] }[], width: number, text: (row: number, cell: number) => string): ExcelRow[] {
|
||||
return rows.map((row, rowIndex) => {
|
||||
const values: ExcelRow = {};
|
||||
for (let column = 0; column < width; column++) values[columnKey(column)] = "";
|
||||
row.cells.forEach((cell, cellIndex) => {
|
||||
values[columnKey(cell.columnIndex)] = text(rowIndex, cellIndex);
|
||||
});
|
||||
return values;
|
||||
});
|
||||
}
|
||||
|
||||
const tableWidth = (rows: { cells: { columnIndex: number; colSpan?: number }[] }[]) =>
|
||||
Math.max(
|
||||
0,
|
||||
...rows.flatMap((row) => row.cells.map((cell) => cell.columnIndex + (cell.colSpan ?? 1))),
|
||||
);
|
||||
|
||||
/**
|
||||
* Number text that always has a decimal point, so the server writes a numeric cell (formatted
|
||||
* 0.0, Mean 0.00) instead of the "(n)" count text; legacy sent "100.0" for whole percentages.
|
||||
*/
|
||||
export function pointedNumber(value: number): string {
|
||||
let text = String(value);
|
||||
if (/e/i.test(text)) text = value.toFixed(14).replace(/0+$/, "");
|
||||
if (text.endsWith(".")) text += "0";
|
||||
return text.includes(".") ? text : text + ".0";
|
||||
}
|
||||
|
||||
/**
|
||||
* Excel value of one result cell. The base (N) line keeps the count text; percentages and
|
||||
* statistics use the unrounded server values (legacy kept full precision and only formatted the
|
||||
* cell), falling back to the screen text when an older server omits them.
|
||||
*/
|
||||
function excelText(cell: ReportCell, row: ReportRow, result: ReportResult): string {
|
||||
if (cell.kind === "TEXT") return cell.textValue ?? "";
|
||||
const columnKind = result.columns[cell.columnIndex]?.kind;
|
||||
const transposed = isTransposed(result);
|
||||
const shown = legacyCellText(cell, row.kind, columnKind, transposed);
|
||||
if (cell.kind === "EMPTY") return shown;
|
||||
if (cell.kind === "STATISTIC" || cell.kind === "MISSING")
|
||||
return cell.statisticExact == null ? shown : pointedNumber(cell.statisticExact);
|
||||
if ((transposed ? columnKind : row.kind) === "TOTAL") return shown;
|
||||
return cell.percentExact == null ? shown : pointedNumber(cell.percentExact);
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy table body for an ordinary result. Header rows are all anchors (like setHeader); body
|
||||
* cells are anchors only when they merge, otherwise the plain displayed text (labels included).
|
||||
* Null when the result has no rows to export.
|
||||
*/
|
||||
export function reportExcelBody(result: ReportResult): ExcelRow[] | null {
|
||||
if (result.type === "TEXT_ANALYSIS") return wordExcelBody(result);
|
||||
const rows = [...result.headerRows, ...result.rows];
|
||||
const width = tableWidth(rows);
|
||||
if (rows.length < 2 || width < 2) return null;
|
||||
const headerCount = result.headerRows.length;
|
||||
return grid(rows, width, (rowIndex, cellIndex) => {
|
||||
const row = rows[rowIndex];
|
||||
const cell = row.cells[cellIndex];
|
||||
const text = excelText(cell, row, result);
|
||||
return rowIndex < headerCount || (cell.rowSpan ?? 1) > 1 || (cell.colSpan ?? 1) > 1
|
||||
? anchor(text, cell.rowSpan, cell.colSpan)
|
||||
: text;
|
||||
});
|
||||
}
|
||||
|
||||
/** Legacy NLP getWordTag: a "Text / Count" row, then the top 100 words. */
|
||||
export function wordExcelBody(result: ReportResult): ExcelRow[] | null {
|
||||
if (!result.words.length) return null;
|
||||
return [
|
||||
{ Text: "Text", Count: "Count" },
|
||||
...result.words.slice(0, 100).map((word) => ({ Text: word.text, Count: String(word.count) })),
|
||||
];
|
||||
}
|
||||
|
||||
/** Legacy quota cell "complete/target/percent"; the server keeps "complete/target". */
|
||||
const quotaText = (cell: QuotaCell) =>
|
||||
(cell.complete ?? 0) + "/" + (cell.target ?? 0) + "/" + (cell.achievementDisplayText ?? "-");
|
||||
|
||||
export function quotaExcelBody(result: QuotaResult): ExcelRow[] | null {
|
||||
const rows = [...result.headerRows, ...result.rows];
|
||||
const width = tableWidth(rows);
|
||||
if (rows.length < 2 || width < 2) return null;
|
||||
const headerCount = result.headerRows.length;
|
||||
return grid(rows, width, (rowIndex, cellIndex) => {
|
||||
if (rowIndex < headerCount) {
|
||||
const cell = result.headerRows[rowIndex].cells[cellIndex];
|
||||
return anchor(cell.textValue ?? "", cell.rowSpan, cell.colSpan);
|
||||
}
|
||||
const cell = result.rows[rowIndex - headerCount].cells[cellIndex];
|
||||
return cell.kind === "QUOTA" ? quotaText(cell) : (cell.textValue ?? "");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user