feat: enhance Excel table header and register styling with Navy fill, white bold font, borders, and AutoFilter
This commit is contained in:
@@ -5,7 +5,7 @@ from pathlib import Path
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import openpyxl
|
import openpyxl
|
||||||
from openpyxl.styles import Font, PatternFill
|
from openpyxl.styles import Alignment, Border, Font, PatternFill, Side
|
||||||
from openpyxl.utils import get_column_letter
|
from openpyxl.utils import get_column_letter
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
@@ -36,7 +36,13 @@ VALID_STATUSES = ("in_service", "under_repair", "out_of_service", "condemned")
|
|||||||
VALID_CRITICALITIES = ("critical", "non_critical")
|
VALID_CRITICALITIES = ("critical", "non_critical")
|
||||||
|
|
||||||
_HEADER_FILL = PatternFill(start_color="1E3A8A", end_color="1E3A8A", fill_type="solid")
|
_HEADER_FILL = PatternFill(start_color="1E3A8A", end_color="1E3A8A", fill_type="solid")
|
||||||
_HEADER_FONT = Font(color="FFFFFF", bold=True)
|
_HEADER_FONT = Font(name="Calibri", size=11, color="FFFFFF", bold=True)
|
||||||
|
_HEADER_BORDER = Border(
|
||||||
|
left=Side(style="thin", color="334155"),
|
||||||
|
right=Side(style="thin", color="334155"),
|
||||||
|
top=Side(style="thin", color="334155"),
|
||||||
|
bottom=Side(style="medium", color="0F172A"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _auto_fit_columns(ws, max_width: int = 42) -> None:
|
def _auto_fit_columns(ws, max_width: int = 42) -> None:
|
||||||
@@ -58,6 +64,11 @@ def build_import_template_xlsx(db: Session) -> BytesIO:
|
|||||||
cell = ws.cell(row=1, column=col_idx, value=col_name)
|
cell = ws.cell(row=1, column=col_idx, value=col_name)
|
||||||
cell.fill = _HEADER_FILL
|
cell.fill = _HEADER_FILL
|
||||||
cell.font = _HEADER_FONT
|
cell.font = _HEADER_FONT
|
||||||
|
cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
||||||
|
cell.border = _HEADER_BORDER
|
||||||
|
ws.row_dimensions[1].height = 26
|
||||||
|
ws.views.sheetView[0].showGridLines = True
|
||||||
|
ws.auto_filter.ref = f"A1:{get_column_letter(len(IMPORT_COLUMNS))}2"
|
||||||
ws.freeze_panes = "A2"
|
ws.freeze_panes = "A2"
|
||||||
|
|
||||||
departments = (
|
departments = (
|
||||||
@@ -119,6 +130,8 @@ def build_import_template_xlsx(db: Session) -> BytesIO:
|
|||||||
|
|
||||||
# Sheet 2: Master Reference
|
# Sheet 2: Master Reference
|
||||||
ref = wb.create_sheet(title="Master Reference")
|
ref = wb.create_sheet(title="Master Reference")
|
||||||
|
ref.row_dimensions[1].height = 26
|
||||||
|
ref.views.sheetView[0].showGridLines = True
|
||||||
headers_map = [
|
headers_map = [
|
||||||
(1, "Department Code"),
|
(1, "Department Code"),
|
||||||
(2, "Department Name"),
|
(2, "Department Name"),
|
||||||
@@ -133,6 +146,8 @@ def build_import_template_xlsx(db: Session) -> BytesIO:
|
|||||||
c = ref.cell(row=1, column=col_idx, value=label)
|
c = ref.cell(row=1, column=col_idx, value=label)
|
||||||
c.fill = _HEADER_FILL
|
c.fill = _HEADER_FILL
|
||||||
c.font = _HEADER_FONT
|
c.font = _HEADER_FONT
|
||||||
|
c.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
||||||
|
c.border = _HEADER_BORDER
|
||||||
ref.freeze_panes = "A2"
|
ref.freeze_panes = "A2"
|
||||||
|
|
||||||
dept_by_id = {d.id: d for d in departments}
|
dept_by_id = {d.id: d for d in departments}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Bulk Import Template Builders for Contracts, Calibration Plans, and Insurance Po
|
|||||||
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
import openpyxl
|
import openpyxl
|
||||||
from openpyxl.styles import Font, PatternFill
|
from openpyxl.styles import Alignment, Border, Font, PatternFill, Side
|
||||||
from openpyxl.utils import get_column_letter
|
from openpyxl.utils import get_column_letter
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
@@ -13,8 +13,14 @@ from ..models import Asset, Department, Vendor
|
|||||||
from .settings_service import get_setting_value
|
from .settings_service import get_setting_value
|
||||||
|
|
||||||
_HEADER_FILL = PatternFill(start_color="1E3A8A", end_color="1E3A8A", fill_type="solid")
|
_HEADER_FILL = PatternFill(start_color="1E3A8A", end_color="1E3A8A", fill_type="solid")
|
||||||
_HEADER_FONT = Font(color="FFFFFF", bold=True)
|
_HEADER_FONT = Font(name="Calibri", size=11, color="FFFFFF", bold=True)
|
||||||
_REF_HEADER_FILL = PatternFill(start_color="334155", end_color="334155", fill_type="solid")
|
_REF_HEADER_FILL = PatternFill(start_color="334155", end_color="334155", fill_type="solid")
|
||||||
|
_HEADER_BORDER = Border(
|
||||||
|
left=Side(style="thin", color="334155"),
|
||||||
|
right=Side(style="thin", color="334155"),
|
||||||
|
top=Side(style="thin", color="334155"),
|
||||||
|
bottom=Side(style="medium", color="0F172A"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _auto_fit_columns(ws, max_width: int = 42) -> None:
|
def _auto_fit_columns(ws, max_width: int = 42) -> None:
|
||||||
@@ -46,6 +52,11 @@ def build_contracts_template_xlsx(db: Session) -> BytesIO:
|
|||||||
c = ws.cell(row=1, column=col_idx, value=col_name)
|
c = ws.cell(row=1, column=col_idx, value=col_name)
|
||||||
c.fill = _HEADER_FILL
|
c.fill = _HEADER_FILL
|
||||||
c.font = _HEADER_FONT
|
c.font = _HEADER_FONT
|
||||||
|
c.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
||||||
|
c.border = _HEADER_BORDER
|
||||||
|
ws.row_dimensions[1].height = 26
|
||||||
|
ws.views.sheetView[0].showGridLines = True
|
||||||
|
ws.auto_filter.ref = f"A1:{get_column_letter(len(cols))}2"
|
||||||
ws.freeze_panes = "A2"
|
ws.freeze_panes = "A2"
|
||||||
|
|
||||||
assets = db.query(Asset).join(Department).filter(Asset.status != "condemned").order_by(Asset.asset_code).all()
|
assets = db.query(Asset).join(Department).filter(Asset.status != "condemned").order_by(Asset.asset_code).all()
|
||||||
@@ -69,6 +80,8 @@ def build_contracts_template_xlsx(db: Session) -> BytesIO:
|
|||||||
|
|
||||||
# Reference sheet
|
# Reference sheet
|
||||||
ws_ref = wb.create_sheet(title="Master Reference")
|
ws_ref = wb.create_sheet(title="Master Reference")
|
||||||
|
ws_ref.row_dimensions[1].height = 26
|
||||||
|
ws_ref.views.sheetView[0].showGridLines = True
|
||||||
ws_ref.cell(row=1, column=1, value="Asset Code").fill = _REF_HEADER_FILL
|
ws_ref.cell(row=1, column=1, value="Asset Code").fill = _REF_HEADER_FILL
|
||||||
ws_ref.cell(row=1, column=1).font = _HEADER_FONT
|
ws_ref.cell(row=1, column=1).font = _HEADER_FONT
|
||||||
ws_ref.cell(row=1, column=2, value="Asset Name").fill = _REF_HEADER_FILL
|
ws_ref.cell(row=1, column=2, value="Asset Name").fill = _REF_HEADER_FILL
|
||||||
@@ -117,6 +130,11 @@ def build_calibration_template_xlsx(db: Session) -> BytesIO:
|
|||||||
c = ws.cell(row=1, column=col_idx, value=col_name)
|
c = ws.cell(row=1, column=col_idx, value=col_name)
|
||||||
c.fill = _HEADER_FILL
|
c.fill = _HEADER_FILL
|
||||||
c.font = _HEADER_FONT
|
c.font = _HEADER_FONT
|
||||||
|
c.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
||||||
|
c.border = _HEADER_BORDER
|
||||||
|
ws.row_dimensions[1].height = 26
|
||||||
|
ws.views.sheetView[0].showGridLines = True
|
||||||
|
ws.auto_filter.ref = f"A1:{get_column_letter(len(cols))}2"
|
||||||
ws.freeze_panes = "A2"
|
ws.freeze_panes = "A2"
|
||||||
|
|
||||||
assets = (
|
assets = (
|
||||||
@@ -147,6 +165,8 @@ def build_calibration_template_xlsx(db: Session) -> BytesIO:
|
|||||||
|
|
||||||
# Reference sheet
|
# Reference sheet
|
||||||
ws_ref = wb.create_sheet(title="Master Reference")
|
ws_ref = wb.create_sheet(title="Master Reference")
|
||||||
|
ws_ref.row_dimensions[1].height = 26
|
||||||
|
ws_ref.views.sheetView[0].showGridLines = True
|
||||||
ws_ref.cell(row=1, column=1, value="Asset Code").fill = _REF_HEADER_FILL
|
ws_ref.cell(row=1, column=1, value="Asset Code").fill = _REF_HEADER_FILL
|
||||||
ws_ref.cell(row=1, column=1).font = _HEADER_FONT
|
ws_ref.cell(row=1, column=1).font = _HEADER_FONT
|
||||||
ws_ref.cell(row=1, column=2, value="Asset Name").fill = _REF_HEADER_FILL
|
ws_ref.cell(row=1, column=2, value="Asset Name").fill = _REF_HEADER_FILL
|
||||||
@@ -197,6 +217,11 @@ def build_insurance_template_xlsx(db: Session) -> BytesIO:
|
|||||||
c = ws.cell(row=1, column=col_idx, value=col_name)
|
c = ws.cell(row=1, column=col_idx, value=col_name)
|
||||||
c.fill = _HEADER_FILL
|
c.fill = _HEADER_FILL
|
||||||
c.font = _HEADER_FONT
|
c.font = _HEADER_FONT
|
||||||
|
c.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
||||||
|
c.border = _HEADER_BORDER
|
||||||
|
ws.row_dimensions[1].height = 26
|
||||||
|
ws.views.sheetView[0].showGridLines = True
|
||||||
|
ws.auto_filter.ref = f"A1:{get_column_letter(len(cols))}2"
|
||||||
ws.freeze_panes = "A2"
|
ws.freeze_panes = "A2"
|
||||||
|
|
||||||
assets = db.query(Asset).join(Department).filter(Asset.status != "condemned").order_by(Asset.asset_code).all()
|
assets = db.query(Asset).join(Department).filter(Asset.status != "condemned").order_by(Asset.asset_code).all()
|
||||||
@@ -218,6 +243,8 @@ def build_insurance_template_xlsx(db: Session) -> BytesIO:
|
|||||||
|
|
||||||
# Reference sheet
|
# Reference sheet
|
||||||
ws_ref = wb.create_sheet(title="Master Reference")
|
ws_ref = wb.create_sheet(title="Master Reference")
|
||||||
|
ws_ref.row_dimensions[1].height = 26
|
||||||
|
ws_ref.views.sheetView[0].showGridLines = True
|
||||||
ws_ref.cell(row=1, column=1, value="Asset Code").fill = _REF_HEADER_FILL
|
ws_ref.cell(row=1, column=1, value="Asset Code").fill = _REF_HEADER_FILL
|
||||||
ws_ref.cell(row=1, column=1).font = _HEADER_FONT
|
ws_ref.cell(row=1, column=1).font = _HEADER_FONT
|
||||||
ws_ref.cell(row=1, column=2, value="Asset Name").fill = _REF_HEADER_FILL
|
ws_ref.cell(row=1, column=2, value="Asset Name").fill = _REF_HEADER_FILL
|
||||||
|
|||||||
Generated
+96
@@ -34,6 +34,7 @@
|
|||||||
"recharts": "^2.13.3",
|
"recharts": "^2.13.3",
|
||||||
"tailwind-merge": "^2.5.4",
|
"tailwind-merge": "^2.5.4",
|
||||||
"xlsx": "^0.18.5",
|
"xlsx": "^0.18.5",
|
||||||
|
"xlsx-js-style": "^1.2.0",
|
||||||
"zod": "^3.23.8",
|
"zod": "^3.23.8",
|
||||||
"zustand": "^4.5.5"
|
"zustand": "^4.5.5"
|
||||||
},
|
},
|
||||||
@@ -4700,6 +4701,15 @@
|
|||||||
"integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==",
|
"integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/exit-on-epipe": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/exit-on-epipe/-/exit-on-epipe-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-h2z5mrROTxce56S+pnvAV890uu7ls7f1kEvVGJbw1OlFH3/mlJ5bkXu0KRyW94v37zzHPiUd55iLn3DA7TjWpw==",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/expect-type": {
|
"node_modules/expect-type": {
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz",
|
||||||
@@ -4798,6 +4808,12 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/fflate": {
|
||||||
|
"version": "0.3.11",
|
||||||
|
"resolved": "https://registry.npmjs.org/fflate/-/fflate-0.3.11.tgz",
|
||||||
|
"integrity": "sha512-Rr5QlUeGN1mbOHlaqcSYMKVpPbgLy0AWT/W0EHxA6NGI12yO1jpoui2zBBvU2G824ltM6Ut8BFgfHSBGfkmS0A==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/file-entry-cache": {
|
"node_modules/file-entry-cache": {
|
||||||
"version": "8.0.0",
|
"version": "8.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-8.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-8.0.0.tgz",
|
||||||
@@ -6070,6 +6086,18 @@
|
|||||||
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
|
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/printj": {
|
||||||
|
"version": "1.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/printj/-/printj-1.1.2.tgz",
|
||||||
|
"integrity": "sha512-zA2SmoLaxZyArQTOPj5LXecR+RagfPSU5Kw1qP+jkWeNlrq+eJZyY2oS68SU1Z/7/myXM4lo9716laOFAVStCQ==",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"bin": {
|
||||||
|
"printj": "bin/printj.njs"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/prop-types": {
|
"node_modules/prop-types": {
|
||||||
"version": "15.8.1",
|
"version": "15.8.1",
|
||||||
"resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz",
|
"resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz",
|
||||||
@@ -7419,6 +7447,74 @@
|
|||||||
"node": ">=0.8"
|
"node": ">=0.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/xlsx-js-style": {
|
||||||
|
"version": "1.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/xlsx-js-style/-/xlsx-js-style-1.2.0.tgz",
|
||||||
|
"integrity": "sha512-DDT4FXFSWfT4DXMSok/m3TvmP1gvO3dn0Eu/c+eXHW5Kzmp7IczNkxg/iEPnImbG9X0Vb8QhROda5eatSR/97Q==",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"dependencies": {
|
||||||
|
"adler-32": "~1.2.0",
|
||||||
|
"cfb": "^1.1.4",
|
||||||
|
"codepage": "~1.14.0",
|
||||||
|
"commander": "~2.17.1",
|
||||||
|
"crc-32": "~1.2.0",
|
||||||
|
"exit-on-epipe": "~1.0.1",
|
||||||
|
"fflate": "^0.3.8",
|
||||||
|
"ssf": "~0.11.2",
|
||||||
|
"wmf": "~1.0.1",
|
||||||
|
"word": "~0.3.0"
|
||||||
|
},
|
||||||
|
"bin": {
|
||||||
|
"xlsx": "bin/xlsx.njs"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/xlsx-js-style/node_modules/adler-32": {
|
||||||
|
"version": "1.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/adler-32/-/adler-32-1.2.0.tgz",
|
||||||
|
"integrity": "sha512-/vUqU/UY4MVeFsg+SsK6c+/05RZXIHZMGJA+PX5JyWI0ZRcBpupnRuPLU/NXXoFwMYCPCoxIfElM2eS+DUXCqQ==",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"dependencies": {
|
||||||
|
"exit-on-epipe": "~1.0.1",
|
||||||
|
"printj": "~1.1.0"
|
||||||
|
},
|
||||||
|
"bin": {
|
||||||
|
"adler32": "bin/adler32.njs"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/xlsx-js-style/node_modules/codepage": {
|
||||||
|
"version": "1.14.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/codepage/-/codepage-1.14.0.tgz",
|
||||||
|
"integrity": "sha512-iz3zJLhlrg37/gYRWgEPkaFTtzmnEv1h+r7NgZum2lFElYQPi0/5bnmuDfODHxfp0INEfnRqyfyeIJDbb7ahRw==",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"dependencies": {
|
||||||
|
"commander": "~2.14.1",
|
||||||
|
"exit-on-epipe": "~1.0.1"
|
||||||
|
},
|
||||||
|
"bin": {
|
||||||
|
"codepage": "bin/codepage.njs"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/xlsx-js-style/node_modules/codepage/node_modules/commander": {
|
||||||
|
"version": "2.14.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/commander/-/commander-2.14.1.tgz",
|
||||||
|
"integrity": "sha512-+YR16o3rK53SmWHU3rEM3tPAh2rwb1yPcQX5irVn7mb0gXbwuCCrnkbV5+PBfETdfg1vui07nM6PCG1zndcjQw==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
|
"node_modules/xlsx-js-style/node_modules/commander": {
|
||||||
|
"version": "2.17.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/commander/-/commander-2.17.1.tgz",
|
||||||
|
"integrity": "sha512-wPMUt6FnH2yzG95SA6mzjQOEKUU3aLaDEmzs1ti+1E9h+CsrZghRlqEM/EJ4KscsQVG8uNN4uVreUeT8+drlgg==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/xml-name-validator": {
|
"node_modules/xml-name-validator": {
|
||||||
"version": "5.0.0",
|
"version": "5.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-5.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-5.0.0.tgz",
|
||||||
|
|||||||
@@ -37,6 +37,7 @@
|
|||||||
"recharts": "^2.13.3",
|
"recharts": "^2.13.3",
|
||||||
"tailwind-merge": "^2.5.4",
|
"tailwind-merge": "^2.5.4",
|
||||||
"xlsx": "^0.18.5",
|
"xlsx": "^0.18.5",
|
||||||
|
"xlsx-js-style": "^1.2.0",
|
||||||
"zod": "^3.23.8",
|
"zod": "^3.23.8",
|
||||||
"zustand": "^4.5.5"
|
"zustand": "^4.5.5"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||||
import { render, screen, fireEvent } from "@testing-library/react";
|
import { render, screen, fireEvent } from "@testing-library/react";
|
||||||
import { ColumnDef } from "@tanstack/react-table";
|
import { ColumnDef } from "@tanstack/react-table";
|
||||||
import * as XLSX from "xlsx";
|
import XLSX from "xlsx-js-style";
|
||||||
import { DataTable } from "./DataTable";
|
import { DataTable } from "./DataTable";
|
||||||
|
|
||||||
vi.mock("xlsx", async () => {
|
vi.mock("xlsx-js-style", async (importOriginal) => {
|
||||||
const actual = await vi.importActual<typeof import("xlsx")>("xlsx");
|
const actual = await importOriginal<Record<string, unknown>>();
|
||||||
|
const base = ((actual as { default?: Record<string, unknown> }).default ?? actual) as Record<string, unknown>;
|
||||||
|
const writeFileMock = vi.fn();
|
||||||
return {
|
return {
|
||||||
...actual,
|
...base,
|
||||||
writeFile: vi.fn(),
|
writeFile: writeFileMock,
|
||||||
|
default: {
|
||||||
|
...base,
|
||||||
|
writeFile: writeFileMock,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import {
|
|||||||
type Table as TanStackTable,
|
type Table as TanStackTable,
|
||||||
} from "@tanstack/react-table";
|
} from "@tanstack/react-table";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import * as XLSX from "xlsx";
|
import XLSX from "xlsx-js-style";
|
||||||
import {
|
import {
|
||||||
ChevronUp,
|
ChevronUp,
|
||||||
ChevronDown,
|
ChevronDown,
|
||||||
@@ -69,6 +69,75 @@ function exportToExcel<TData>(table: TanStackTable<TData>, filename: string) {
|
|||||||
|
|
||||||
const ws = XLSX.utils.json_to_sheet(exportRows);
|
const ws = XLSX.utils.json_to_sheet(exportRows);
|
||||||
|
|
||||||
|
// Apply rich executive styling to table header row (Navy fill, bold white font, borders)
|
||||||
|
for (let c = 0; c < visibleCols.length; c++) {
|
||||||
|
const headerAddr = XLSX.utils.encode_cell({ r: 0, c });
|
||||||
|
if (ws[headerAddr]) {
|
||||||
|
ws[headerAddr].s = {
|
||||||
|
fill: { fgColor: { rgb: "1E3A8A" } }, // Poona Hospital Deep Navy
|
||||||
|
font: { name: "Calibri", sz: 11, bold: true, color: { rgb: "FFFFFF" } },
|
||||||
|
alignment: { horizontal: "center", vertical: "center", wrapText: true },
|
||||||
|
border: {
|
||||||
|
top: { style: "thin", color: { rgb: "334155" } },
|
||||||
|
bottom: { style: "medium", color: { rgb: "0F172A" } },
|
||||||
|
left: { style: "thin", color: { rgb: "334155" } },
|
||||||
|
right: { style: "thin", color: { rgb: "334155" } },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply zebra striping, borders, and column-aware alignment to data rows
|
||||||
|
for (let r = 0; r < rows.length; r++) {
|
||||||
|
const isOdd = r % 2 === 1;
|
||||||
|
for (let c = 0; c < visibleCols.length; c++) {
|
||||||
|
const cellAddr = XLSX.utils.encode_cell({ r: r + 1, c });
|
||||||
|
if (!ws[cellAddr]) continue;
|
||||||
|
|
||||||
|
const col = visibleCols[c];
|
||||||
|
const headerLabel = (typeof col.columnDef.header === "string" ? col.columnDef.header : col.id).toLowerCase();
|
||||||
|
const val = rows[r].getValue(col.id);
|
||||||
|
|
||||||
|
let horizontalAlign: "left" | "center" | "right" = "left";
|
||||||
|
if (
|
||||||
|
typeof val === "number" ||
|
||||||
|
headerLabel.includes("cost") ||
|
||||||
|
headerLabel.includes("value") ||
|
||||||
|
headerLabel.includes("amount") ||
|
||||||
|
headerLabel.includes("price") ||
|
||||||
|
headerLabel.includes("minutes") ||
|
||||||
|
headerLabel.includes("downtime") ||
|
||||||
|
headerLabel.includes("total")
|
||||||
|
) {
|
||||||
|
horizontalAlign = "right";
|
||||||
|
} else if (
|
||||||
|
headerLabel.includes("code") ||
|
||||||
|
headerLabel.includes("date") ||
|
||||||
|
headerLabel.includes("status") ||
|
||||||
|
headerLabel.includes("priority") ||
|
||||||
|
headerLabel.includes("criticality") ||
|
||||||
|
headerLabel.includes("serial") ||
|
||||||
|
headerLabel.includes("mode") ||
|
||||||
|
headerLabel.includes("id") ||
|
||||||
|
typeof val === "boolean"
|
||||||
|
) {
|
||||||
|
horizontalAlign = "center";
|
||||||
|
}
|
||||||
|
|
||||||
|
ws[cellAddr].s = {
|
||||||
|
fill: { fgColor: { rgb: isOdd ? "F8FAFC" : "FFFFFF" } },
|
||||||
|
font: { name: "Calibri", sz: 10, color: { rgb: "0F172A" } },
|
||||||
|
alignment: { horizontal: horizontalAlign, vertical: "center" },
|
||||||
|
border: {
|
||||||
|
top: { style: "thin", color: { rgb: "E2E8F0" } },
|
||||||
|
bottom: { style: "thin", color: { rgb: "E2E8F0" } },
|
||||||
|
left: { style: "thin", color: { rgb: "E2E8F0" } },
|
||||||
|
right: { style: "thin", color: { rgb: "E2E8F0" } },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Auto-fit column widths with generous padding so text/codes are never truncated
|
// Auto-fit column widths with generous padding so text/codes are never truncated
|
||||||
const colWidths = visibleCols.map((col) => {
|
const colWidths = visibleCols.map((col) => {
|
||||||
const headerLabel =
|
const headerLabel =
|
||||||
@@ -85,9 +154,9 @@ function exportToExcel<TData>(table: TanStackTable<TData>, filename: string) {
|
|||||||
});
|
});
|
||||||
ws["!cols"] = colWidths;
|
ws["!cols"] = colWidths;
|
||||||
|
|
||||||
// Add comfortable row heights: header row taller (26pt), data rows spacious (20pt)
|
// Add comfortable row heights: header row taller (28pt), data rows spacious (20pt)
|
||||||
ws["!rows"] = [
|
ws["!rows"] = [
|
||||||
{ hpt: 26 },
|
{ hpt: 28 },
|
||||||
...rows.map(() => ({ hpt: 20 })),
|
...rows.map(() => ({ hpt: 20 })),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user