/* ============================================================================= * spreadsheet_format_code.ts (2단계 · 주인 sub_laptop_1) * 셀 서식 대화상자(표시 형식 탭)의 범주 → 형식 코드 짓기 — DOM · CSS 없음(시험이 바로 부름). * 실무 자주 쓰는 모양만(일반 · 숫자 · 통화 · 회계 · 백분율 · 텍스트 · 사용자 지정). * ========================================================================== */ export const CATEGORIES = [ "일반", "숫자", "통화", "회계", "백분율", "텍스트", "사용자 지정", ] as const; export type Category = (typeof CATEGORIES)[number]; export type NegStyle = "minus" | "paren" | "red-minus" | "red-paren"; /** 범주 · 소수 자릿수 · 천단위 · 음수 표시로 형식 코드를 지음. */ export function buildCode( cat: Category, decimals: number, thousands: boolean, neg: NegStyle, ): string { if (cat === "일반") return ""; if (cat === "텍스트") return "@"; const digits = decimals > 0 ? `0.${"0".repeat(decimals)}` : "0"; const grouped = thousands ? `#,##${digits}` : digits; if (cat === "백분율") return `${grouped}%`; const prefix = cat === "통화" ? '"₩"' : ""; const positive = `${prefix}${grouped}`; if (cat === "회계") { const acc = decimals > 0 ? "0.00" : "0"; return `_-* #,##${acc}_-;-* #,##${acc}_-;_-* "-"_-;_-@_-`; } switch (neg) { case "minus": return positive; case "paren": return `${positive};(${positive})`; case "red-minus": return `${positive};[Red]-${positive}`; case "red-paren": return `${positive};[Red](${positive})`; } } /** 있는 형식 코드를 보고 범주를 어림(대화상자를 열 때 초기 범주 고르기). */ export function guessCategory(code: string | undefined): Category { if (!code) return "일반"; if (code === "@") return "텍스트"; if (code.includes("%")) return "백분율"; if (code.includes("_-*")) return "회계"; if (code.includes("₩")) return "통화"; if (/[0#]/.test(code)) return "숫자"; return "사용자 지정"; }