Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: keep hidden spreadsheet rows in chat attachments (#17314)

Nilay457795e08de7
diff --git a/studio/frontend/src/components/file-viewer/office.ts b/studio/frontend/src/components/file-viewer/office.ts
index c24f5a6259b..53c0e4ac354 100644
--- a/studio/frontend/src/components/file-viewer/office.ts
+++ b/studio/frontend/src/components/file-viewer/office.ts
@@ -879,6 +879,7 @@ function readSheet(
date1904: boolean,
budget: { cells: number },
limits: SheetLimits,
+ keepHidden: boolean,
): Sheet {
const rows: (SheetCell | undefined)[][] = [];
const widths: (number | undefined)[] = [];
@@ -890,7 +891,7 @@ function readSheet(
const min = Number(attribute(attrs, "min") ?? 1);
const max = Math.min(Number(attribute(attrs, "max") ?? min), limits.columns);
const width = Number(attribute(attrs, "width"));
- const hide = isTrue(attribute(attrs, "hidden"));
+ const hide = !keepHidden && isTrue(attribute(attrs, "hidden"));
for (let i = min; i <= max; i++) {
if (hide) hidden.columns.add(i - 1);
else if (width) widths[i - 1] = Math.round(width * 7 + 5);
@@ -910,7 +911,7 @@ function readSheet(
break;
}
budget.cells--;
- const rowHidden = isTrue(attribute(rowAttrs, "hidden"));
+ const rowHidden = !keepHidden && isTrue(attribute(rowAttrs, "hidden"));
if (rowHidden) hidden.rows.add(r);
if (rowHidden && !rowBody.includes("shared")) continue;
const cells: (SheetCell | undefined)[] = [];
@@ -1119,7 +1120,11 @@ function styleSections(bytes: Uint8Array | undefined): { doc: Document | null; c
return { doc: parseXml(`${open}${parts.join("")}</${prefix}styleSheet>`), cut };
}
-export function readXlsx(bytes: Uint8Array, limits: SheetLimits = FULL_SHEET_LIMITS): Sheet[] {
+export function readXlsx(
+ bytes: Uint8Array,
+ limits: SheetLimits = FULL_SHEET_LIMITS,
+ { keepHidden = false } = {},
+): Sheet[] {
const read = archive(bytes);
const main = mainPart(read, "xl/workbook.xml");
if ((read.size(main) ?? 0) > MAX_XML_PART_BYTES) throw new Error("File is too large to preview.");
@@ -1153,8 +1158,11 @@ export function readXlsx(bytes: Uint8Array, limits: SheetLimits = FULL_SHEET_LIM
const paths = new Map(rels.map((rel) => [rel.id, rel.path]));
const sheets: Sheet[] = [];
const budget = { cells: MAX_WORKBOOK_CELLS };
- for (const sheet of all(workbook, "sheet")) {
- if (sheet.getAttribute("state") === "hidden" || sheet.getAttribute("state") === "veryHidden") continue;
+ const listed = all(workbook, "sheet");
+ const isHidden = (sheet: Element) => ["hidden", "veryHidden"].includes(sheet.getAttribute("state") ?? "");
+ const visible = listed.filter((sheet) => !isHidden(sheet));
+ // Hidden sheets last, so they never take the sheet or cell budget from visible ones.
+ for (const sheet of keepHidden ? [...visible, ...listed.filter(isHidden)] : visible) {
if (limits.extraNames !== undefined && sheets.length >= limits.sheets) {
if (sheets.length >= limits.sheets + limits.extraNames) break;
sheets.push({ name: sheet.getAttribute("name") ?? "Sheet", rows: [], widths: [], truncated: false });
@@ -1171,7 +1179,7 @@ export function readXlsx(bytes: Uint8Array, limits: SheetLimits = FULL_SHEET_LIM
const whole = part.length <= MAX_SHEET_XML_BYTES;
const { text, cut } = sheetText(part, Math.min(limits.rows, budget.cells), whole);
missed = false;
- const parsed = readSheet(text, sheet.getAttribute("name") ?? "Sheet", string, style, date1904, budget, limits);
+ const parsed = readSheet(text, sheet.getAttribute("name") ?? "Sheet", string, style, date1904, budget, limits, keepHidden);
if (cut || missed) parsed.truncated = true;
sheets.push(parsed);
}
diff --git a/studio/frontend/src/features/chat/attachment-content.ts b/studio/frontend/src/features/chat/attachment-content.ts
index 4f86c8c3640..0941bf3c57c 100644
--- a/studio/frontend/src/features/chat/attachment-content.ts
+++ b/studio/frontend/src/features/chat/attachment-content.ts
@@ -1805,15 +1805,12 @@ export async function extractOfficeAttachmentText(
parts.push(`[Truncated: only the first ${deck.slides.length} slides are included.]`);
}
} else {
- for (const sheet of readXlsx(bytes)) {
+ for (const sheet of readXlsx(bytes, undefined, { keepHidden: true })) {
if (budget.cut) break;
const lines = [budget.take(`Sheet: ${sheet.name}`)];
for (const row of sheet.rows) {
if (!row || budget.cut) continue;
- const line = Array.from(row, (cell) => budget.take(cell?.text ?? ""))
- .filter((_, index) => !sheet.hidden?.columns.has(index))
- .join("\t")
- .trimEnd();
+ const line = Array.from(row, (cell) => budget.take(cell?.text ?? "")).join("\t").trimEnd();
if (line) lines.push(line);
}
if (sheet.truncated) {
diff --git a/studio/frontend/tests/file-viewer-office.test.ts b/studio/frontend/tests/file-viewer-office.test.ts
index 10920366d94..8fc2d4e25af 100644
--- a/studio/frontend/tests/file-viewer-office.test.ts
+++ b/studio/frontend/tests/file-viewer-office.test.ts
@@ -251,6 +251,59 @@ test("xlsx: out-of-range date values do not prevent reading the workbook", () =>
assert.deepEqual(sheet?.rows[0]?.map((cell) => cell?.text), ["1700000000", "Mar-23", "42"]);
});
+test("xlsx: a chat attachment keeps the rows, columns and sheets the viewer hides", async () => {
+ const { extractOfficeAttachmentText } = await import("../src/features/chat/attachment-content.ts");
+ const zip = zipSync({
+ "xl/workbook.xml": strToU8(
+ `<workbook xmlns="${MAIN}" xmlns:r="${REL}"><sheets><sheet name="Sales" sheetId="1" r:id="rId1"/><sheet name="Notes" sheetId="2" state="hidden" r:id="rId2"/></sheets></workbook>`,
+ ),
+ "xl/_rels/workbook.xml.rels": rels(
+ `<Relationship Id="rId1" Type="${REL}/worksheet" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="${REL}/worksheet" Target="worksheets/sheet2.xml"/>`,
+ ),
+ "xl/worksheets/sheet1.xml": strToU8(`<worksheet xmlns="${MAIN}"><cols><col min="3" max="3" hidden="1"/></cols><sheetData>
+<row r="1"><c r="A1" t="inlineStr"><is><t>Region</t></is></c><c r="B1" t="inlineStr"><is><t>Q3</t></is></c><c r="C1" t="inlineStr"><is><t>Owner</t></is></c></row>
+<row r="2"><c r="A2" t="inlineStr"><is><t>North</t></is></c><c r="B2"><v>100</v></c><c r="C2" t="inlineStr"><is><t>Ann</t></is></c></row>
+<row r="3" hidden="1"><c r="A3" t="inlineStr"><is><t>West</t></is></c><c r="B3"><v>300</v></c><c r="C3" t="inlineStr"><is><t>Bo</t></is></c></row>
+</sheetData></worksheet>`),
+ "xl/worksheets/sheet2.xml": strToU8(
+ `<worksheet xmlns="${MAIN}"><sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>adjust</t></is></c></row></sheetData></worksheet>`,
+ ),
+ });
+
+ const viewer = readXlsx(zip);
+ assert.deepEqual(viewer.map((sheet) => sheet.name), ["Sales"]);
+ assert.deepEqual(viewer[0]!.rows.filter(Boolean).map((row) => row.map((cell) => cell?.text)), [
+ ["Region", "Q3"],
+ ["North", "100"],
+ ]);
+
+ const file = new File([zip], "q3.xlsx", { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
+ assert.equal(
+ await extractOfficeAttachmentText(file, "XLSX"),
+ "Sheet: Sales\nRegion\tQ3\tOwner\nNorth\t100\tAnn\nWest\t300\tBo\n\nSheet: Notes\nadjust",
+ );
+});
+
+test("xlsx: kept hidden sheets come after visible ones so they cannot use up the sheet budget", () => {
+ const zip = zipSync({
+ "xl/workbook.xml": strToU8(
+ `<workbook xmlns="${MAIN}" xmlns:r="${REL}"><sheets><sheet name="Lookup" sheetId="1" state="veryHidden" r:id="rId1"/><sheet name="Sales" sheetId="2" r:id="rId2"/></sheets></workbook>`,
+ ),
+ "xl/_rels/workbook.xml.rels": rels(
+ `<Relationship Id="rId1" Type="${REL}/worksheet" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="${REL}/worksheet" Target="worksheets/sheet2.xml"/>`,
+ ),
+ "xl/worksheets/sheet1.xml": strToU8(
+ `<worksheet xmlns="${MAIN}"><sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>code</t></is></c></row></sheetData></worksheet>`,
+ ),
+ "xl/worksheets/sheet2.xml": strToU8(
+ `<worksheet xmlns="${MAIN}"><sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>North</t></is></c></row></sheetData></worksheet>`,
+ ),
+ });
+ const limits = { sheets: 1, rows: 10, columns: 10 };
+ assert.deepEqual(readXlsx(zip, limits, { keepHidden: true }).map((sheet) => sheet.name), ["Sales"]);
+ assert.deepEqual(readXlsx(zip, undefined, { keepHidden: true }).map((sheet) => sheet.name), ["Sales", "Lookup"]);
+});
+
test("pptx: maxSlides stops reading after that many slides", () => {
const P = "http://schemas.openxmlformats.org/presentationml/2006/main";
const A = "http://schemas.openxmlformats.org/drawingml/2006/main";