Skip to content

Build your personal knowledge base with Trilium Notes

fix(spreadsheet): type exceljs data validations instead of casting

Elian Doran0381119158a5
diff --git a/packages/commons/src/lib/spreadsheet/exceljs_augmentation.ts b/packages/commons/src/lib/spreadsheet/exceljs_augmentation.ts
new file mode 100644
index 0000000000..b99bc3b808
--- /dev/null
+++ b/packages/commons/src/lib/spreadsheet/exceljs_augmentation.ts
@@ -0,0 +1,25 @@
+/**
+ * exceljs exposes `Worksheet#dataValidations` at runtime — an instance of its `DataValidations`
+ * class (`lib/doc/data-validations.js`) — but omits it from its published typings, where the field
+ * is commented out of `WorksheetModel`. Declare it so the XLSX importer/exporter can read and write
+ * validation rules without casting through `any`.
+ *
+ * This is a module rather than a free-floating `.d.ts` so that importing it pulls the augmentation
+ * into the program; an unreferenced declaration file is only picked up by projects whose `include`
+ * globs it, which is not every project that compiles these sources.
+ */
+import "exceljs";
+
+declare module "exceljs" {
+ interface DataValidations {
+ /** Validation config keyed by cell address; exceljs expands a range's `sqref` per cell. */
+ model: Record<string, DataValidation | undefined>;
+ add(address: string, validation: DataValidation): DataValidation;
+ find(address: string): DataValidation | undefined;
+ remove(address: string): void;
+ }
+
+ interface Worksheet {
+ readonly dataValidations: DataValidations;
+ }
+}
diff --git a/packages/commons/src/lib/spreadsheet/parse_from_xlsx.ts b/packages/commons/src/lib/spreadsheet/parse_from_xlsx.ts
index a08bee9e43..caaf6e0a98 100644
--- a/packages/commons/src/lib/spreadsheet/parse_from_xlsx.ts
+++ b/packages/commons/src/lib/spreadsheet/parse_from_xlsx.ts
@@ -19,6 +19,8 @@
import ExcelJS from "exceljs";
+import "./exceljs_augmentation.js";
+
import {
BorderStyle,
CellValueType,
@@ -339,9 +341,7 @@ interface CellRef {
* sheet has none.
*/
function readDataValidations(ws: ExcelJS.Worksheet, sheetId: string): DataValidationRule[] {
- const model = (ws.dataValidations as { model?: Record<string, ExcelJS.DataValidation | undefined> } | undefined)?.model;
- /* v8 ignore next -- defensive: exceljs always exposes a (possibly empty) dataValidations model */
- if (!model) return [];
+ const model = ws.dataValidations.model;
// Group cells by an identical validation config; the key is order-stable across the sheet.
const groups = new Map<string, { config: ExcelJS.DataValidation; cells: CellRef[] }>();
diff --git a/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts b/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts
index 4a3a14a7bf..d2ad59d619 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts
@@ -13,6 +13,8 @@
import ExcelJS from "exceljs";
+import "./exceljs_augmentation.js";
+
import {
BorderStyle,
type DataValidationRule,