Skip to content

Build your personal knowledge base with Trilium Notes

feat(spreadsheet): basic search support

Elian Doran860af33710af
diff --git a/apps/server/src/services/search/expressions/note_content_fulltext.ts b/apps/server/src/services/search/expressions/note_content_fulltext.ts
index f3e0a39333..827f8cc173 100644
--- a/apps/server/src/services/search/expressions/note_content_fulltext.ts
+++ b/apps/server/src/services/search/expressions/note_content_fulltext.ts
@@ -83,7 +83,7 @@ class NoteContentFulltextExp extends Expression {
for (const row of sql.iterateRows<SearchRow>(`
SELECT noteId, type, mime, content, isProtected
FROM notes JOIN blobs USING (blobId)
- WHERE type IN ('text', 'code', 'mermaid', 'canvas', 'mindMap')
+ WHERE type IN ('text', 'code', 'mermaid', 'canvas', 'mindMap', 'spreadsheet')
AND isDeleted = 0
AND LENGTH(content) < ${MAX_SEARCH_CONTENT_SIZE}`)) {
this.findInText(row, inputNoteSet, resultNoteSet);
diff --git a/apps/server/src/services/search/expressions/note_content_fulltext_preprocessor.ts b/apps/server/src/services/search/expressions/note_content_fulltext_preprocessor.ts
index 795c0346de..927c72e4fb 100644
--- a/apps/server/src/services/search/expressions/note_content_fulltext_preprocessor.ts
+++ b/apps/server/src/services/search/expressions/note_content_fulltext_preprocessor.ts
@@ -1,3 +1,4 @@
+import { extractSpreadsheetText } from "@triliumnext/commons";
import striptags from "striptags";
import { normalize } from "../../utils.js";
@@ -16,6 +17,8 @@ export default function preprocessContent(content: string | Buffer, type: string
content = processMindmapContent(content);
} else if (type === "canvas" && mime === "application/json") {
content = processCanvasContent(content);
+ } else if (type === "spreadsheet" && mime === "application/json") {
+ content = extractSpreadsheetText(content);
}
return content.trim();
diff --git a/packages/commons/src/index.ts b/packages/commons/src/index.ts
index 8c2cfc595c..ebcf3ac56b 100644
--- a/packages/commons/src/index.ts
+++ b/packages/commons/src/index.ts
@@ -16,6 +16,7 @@ export * from "./lib/dayjs.js";
export * from "./lib/notes.js";
export * from "./lib/week_utils.js";
export { default as BUILTIN_ATTRIBUTES } from "./lib/builtin_attributes.js";
+export * from "./lib/spreadsheet/extract_text.js";
export * from "./lib/spreadsheet/render_to_html.js";
export * from "./lib/llm_api.js";
export * from "./lib/marked_extensions.js";
diff --git a/packages/commons/src/lib/spreadsheet/extract_text.spec.ts b/packages/commons/src/lib/spreadsheet/extract_text.spec.ts
new file mode 100644
index 0000000000..714dca28f3
--- /dev/null
+++ b/packages/commons/src/lib/spreadsheet/extract_text.spec.ts
@@ -0,0 +1,349 @@
+import { describe, expect, it } from "vitest";
+import { extractSpreadsheetText } from "./extract_text.js";
+
+describe("extractSpreadsheetText", () => {
+ it("extracts cell values from a basic spreadsheet", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ cellData: {
+ "1": { "1": { v: "The", t: 1 } },
+ "2": { "2": { v: "quick", t: 1 } },
+ "3": { "3": { v: "brown", t: 1 } },
+ "4": { "1": { v: "fox", t: 1 } }
+ },
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ });
+
+ const text = extractSpreadsheetText(input);
+ expect(text).toBe("The quick brown fox");
+ });
+
+ it("skips hidden sheets", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1", "s2"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Visible",
+ hidden: 0,
+ cellData: { "0": { "0": { v: "shown" } } },
+ rowData: {},
+ columnData: {}
+ },
+ s2: {
+ id: "s2",
+ name: "Hidden",
+ hidden: 1,
+ cellData: { "0": { "0": { v: "secret" } } },
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ });
+
+ const text = extractSpreadsheetText(input);
+ expect(text).toContain("shown");
+ expect(text).not.toContain("secret");
+ });
+
+ it("skips hidden rows", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ cellData: {
+ "0": { "0": { v: "visible row" } },
+ "1": { "0": { v: "hidden row" } }
+ },
+ rowData: { "1": { hd: 1 } },
+ columnData: {}
+ }
+ }
+ }
+ });
+
+ const text = extractSpreadsheetText(input);
+ expect(text).toContain("visible row");
+ expect(text).not.toContain("hidden row");
+ });
+
+ it("skips hidden columns", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ cellData: {
+ "0": {
+ "0": { v: "visible col" },
+ "1": { v: "hidden col" }
+ }
+ },
+ rowData: {},
+ columnData: { "1": { hd: 1 } }
+ }
+ }
+ }
+ });
+
+ const text = extractSpreadsheetText(input);
+ expect(text).toContain("visible col");
+ expect(text).not.toContain("hidden col");
+ });
+
+ it("extracts text from multiple visible sheets", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1", "s2"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Data",
+ hidden: 0,
+ cellData: { "0": { "0": { v: "alpha" } } },
+ rowData: {},
+ columnData: {}
+ },
+ s2: {
+ id: "s2",
+ name: "Summary",
+ hidden: 0,
+ cellData: { "0": { "0": { v: "beta" } } },
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ });
+
+ const text = extractSpreadsheetText(input);
+ expect(text).toContain("alpha");
+ expect(text).toContain("beta");
+ });
+
+ it("handles boolean values", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ cellData: {
+ "0": {
+ "0": { v: true, t: 3 },
+ "1": { v: false, t: 3 }
+ }
+ },
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ });
+
+ const text = extractSpreadsheetText(input);
+ expect(text).toContain("TRUE");
+ expect(text).toContain("FALSE");
+ });
+
+ it("handles numeric values", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ cellData: {
+ "0": { "0": { v: 42 }, "1": { v: 3.14 } }
+ },
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ });
+
+ const text = extractSpreadsheetText(input);
+ expect(text).toContain("42");
+ expect(text).toContain("3.14");
+ });
+
+ it("skips cells with null or undefined values", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ cellData: {
+ "0": {
+ "0": { v: "data" },
+ "1": { v: null },
+ "2": {},
+ "3": { s: "someStyle" }
+ }
+ },
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ });
+
+ const text = extractSpreadsheetText(input);
+ expect(text).toBe("data");
+ });
+
+ it("returns empty string for invalid JSON", () => {
+ expect(extractSpreadsheetText("not json")).toBe("");
+ });
+
+ it("returns empty string for empty workbook", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ cellData: {},
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ });
+
+ expect(extractSpreadsheetText(input)).toBe("");
+ });
+
+ it("returns empty string for missing workbook", () => {
+ expect(extractSpreadsheetText(JSON.stringify({ version: 1 }))).toBe("");
+ });
+
+ it("handles sheet with missing cellData", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0
+ }
+ }
+ }
+ });
+
+ expect(extractSpreadsheetText(input)).toBe("");
+ });
+
+ it("handles lowercased JSON from normalize()", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "sheet1",
+ hidden: 0,
+ cellData: {
+ "0": { "0": { v: "hello" } }
+ },
+ rowData: { "1": { hd: 1 } },
+ columnData: { "2": { hd: 1 } }
+ }
+ }
+ }
+ }).toLowerCase();
+
+ expect(extractSpreadsheetText(input)).toBe("hello");
+ });
+
+ it("extracts text from the sample spreadsheet", () => {
+ const input = JSON.stringify({
+ version: 1,
+ workbook: {
+ id: "vlyRSv",
+ sheetOrder: ["YdJwCRPZnEn4twLYtcdrE"],
+ name: "",
+ appVersion: "0.20.1",
+ locale: "zhCN",
+ styles: {
+ yIwJoC: { bl: 1 },
+ bTNHmx: { it: 1 },
+ "6ZCsr1": { ff: "Arial" }
+ },
+ sheets: {
+ YdJwCRPZnEn4twLYtcdrE: {
+ id: "YdJwCRPZnEn4twLYtcdrE",
+ name: "Sheet1",
+ hidden: 0,
+ rowCount: 1000,
+ columnCount: 20,
+ defaultColumnWidth: 88,
+ defaultRowHeight: 24,
+ mergeData: [],
+ cellData: {
+ "1": { "1": { v: "The", t: 1 } },
+ "2": { "2": { v: "quick", t: 1 } },
+ "3": { "3": { s: "yIwJoC", v: "brown", t: 1 } },
+ "4": { "1": { s: "bTNHmx", v: "fox", t: 1 } },
+ "8": {
+ "0": { s: "6ZCsr1" },
+ "3": { v: "This text is available only in the spreadsheet", t: 1 }
+ }
+ },
+ rowData: {},
+ columnData: {},
+ showGridlines: 1
+ }
+ }
+ }
+ });
+
+ const text = extractSpreadsheetText(input);
+ expect(text).toContain("The");
+ expect(text).toContain("quick");
+ expect(text).toContain("brown");
+ expect(text).toContain("fox");
+ expect(text).toContain("This text is available only in the spreadsheet");
+ });
+});
diff --git a/packages/commons/src/lib/spreadsheet/extract_text.ts b/packages/commons/src/lib/spreadsheet/extract_text.ts
new file mode 100644
index 0000000000..ff38abdb2a
--- /dev/null
+++ b/packages/commons/src/lib/spreadsheet/extract_text.ts
@@ -0,0 +1,86 @@
+/**
+ * Extracts plain text from a UniversJS workbook JSON structure for full-text search indexing.
+ * Mirrors the traversal logic of {@link renderSpreadsheetToHtml}: only visible sheets,
+ * skipping hidden rows and columns.
+ *
+ * Handles both original camelCase keys and lowercased keys (the search preprocessor
+ * runs `normalize()` on content before passing it here, which lowercases the entire
+ * JSON string).
+ */
+
+/* eslint-disable @typescript-eslint/no-explicit-any */
+
+/** Read a property by trying the camelCase name first, then the lowercased form. */
+function prop(obj: any, camel: string): any {
+ if (obj == null) return undefined;
+ return obj[camel] ?? obj[camel.toLowerCase()];
+}
+
+/**
+ * Parses the raw JSON content of a spreadsheet note and extracts all visible
+ * cell values as a single plain-text string (space-separated).
+ *
+ * Returns an empty string for invalid or empty spreadsheets.
+ */
+export function extractSpreadsheetText(jsonContent: string): string {
+ let data: any;
+ try {
+ data = JSON.parse(jsonContent);
+ } catch {
+ return "";
+ }
+
+ const workbook = prop(data, "workbook");
+ const sheets = workbook && prop(workbook, "sheets");
+ if (!sheets) {
+ return "";
+ }
+
+ const sheetOrder: string[] | undefined = prop(workbook, "sheetOrder");
+ const sheetIds = sheetOrder ?? Object.keys(sheets);
+ const visibleSheets = sheetIds
+ .map((id: string) => sheets[id])
+ .filter((s: any) => s && !prop(s, "hidden"));
+
+ if (visibleSheets.length === 0) {
+ return "";
+ }
+
+ const parts: string[] = [];
+ for (const sheet of visibleSheets) {
+ extractSheetText(sheet, parts);
+ }
+
+ return parts.join(" ");
+}
+
+function extractSheetText(sheet: any, parts: string[]): void {
+ const cellData = prop(sheet, "cellData");
+ if (!cellData) return;
+
+ const rowData = prop(sheet, "rowData") ?? {};
+ const columnData = prop(sheet, "columnData") ?? {};
+
+ for (const rowStr of Object.keys(cellData)) {
+ const row = Number(rowStr);
+ if (prop(rowData[row], "hd")) continue;
+
+ const cols = cellData[row];
+ for (const colStr of Object.keys(cols)) {
+ const col = Number(colStr);
+ if (prop(columnData[col], "hd")) continue;
+
+ const cell = cols[col];
+ if (cell?.v == null) continue;
+
+ if (typeof cell.v === "boolean") {
+ parts.push(cell.v ? "TRUE" : "FALSE");
+ } else {
+ const text = String(cell.v).trim();
+ if (text) {
+ parts.push(text);
+ }
+ }
+ }
+ }
+}