j
This commit is contained in:
@@ -68,6 +68,40 @@ export type ExtraSheetCell = {
|
||||
export type ExtraSheetMatrix = Array<Array<ExtraSheetCell | undefined>>;
|
||||
type SheetPoint = { row: number; column: number };
|
||||
|
||||
export function extraSheetUsedRange(sheet: ExtraSheet): ExtraSheet | null {
|
||||
const usedPoints = sheet.rows.flatMap((row, rowIndex) =>
|
||||
sheet.columns.flatMap((column, columnIndex) => {
|
||||
const used = extraRichTextToPlainText(row.cells[column.id] ?? "").trim()
|
||||
|| row.backgrounds?.[column.id]
|
||||
|| row.alignments?.[column.id];
|
||||
return used ? [{ row: rowIndex, column: columnIndex }] : [];
|
||||
}),
|
||||
);
|
||||
if (!usedPoints.length) return null;
|
||||
|
||||
const rowIndexes = usedPoints.map((point) => point.row);
|
||||
const columnIndexes = usedPoints.map((point) => point.column);
|
||||
const firstRow = Math.min(...rowIndexes);
|
||||
const lastRow = Math.max(...rowIndexes);
|
||||
const firstColumn = Math.min(...columnIndexes);
|
||||
const lastColumn = Math.max(...columnIndexes);
|
||||
|
||||
const columns = sheet.columns.slice(firstColumn, lastColumn + 1);
|
||||
return {
|
||||
columns,
|
||||
rows: sheet.rows.slice(firstRow, lastRow + 1).map((row) => ({
|
||||
...row,
|
||||
cells: Object.fromEntries(columns.map((column) => [column.id, row.cells[column.id] ?? ""])),
|
||||
backgrounds: Object.fromEntries(columns.flatMap((column) =>
|
||||
row.backgrounds?.[column.id] ? [[column.id, row.backgrounds[column.id]]] : [],
|
||||
)),
|
||||
alignments: Object.fromEntries(columns.flatMap((column) =>
|
||||
row.alignments?.[column.id] ? [[column.id, row.alignments[column.id]]] : [],
|
||||
)),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
export function nextExtraSheetColumnLabel(columns: ExtraSheet["columns"]): string {
|
||||
const labels = new Set(columns.map((column) => column.label.toLocaleLowerCase()));
|
||||
let index = columns.length + 1;
|
||||
@@ -76,14 +110,14 @@ export function nextExtraSheetColumnLabel(columns: ExtraSheet["columns"]): strin
|
||||
}
|
||||
|
||||
export function createExtraSheet(createId: (prefix: string) => string): ExtraSheet {
|
||||
const columns = [1, 2].map((index) => ({
|
||||
const columns = Array.from({ length: 6 }, (_, index) => ({
|
||||
id: createId("extra-sheet-column"),
|
||||
label: `คอลัมน์ ${index}`,
|
||||
label: `คอลัมน์ ${index + 1}`,
|
||||
width: DEFAULT_EXTRA_SHEET_COLUMN_WIDTH,
|
||||
}));
|
||||
return {
|
||||
columns,
|
||||
rows: [1, 2].map(() => ({
|
||||
rows: Array.from({ length: 4 }, () => ({
|
||||
id: createId("extra-sheet-row"),
|
||||
cells: Object.fromEntries(columns.map((column) => [column.id, ""])),
|
||||
backgrounds: {},
|
||||
|
||||
Reference in New Issue
Block a user