Skip to content

Your lightweight open-source personal agent.

fix(documents): read cells beyond declared XLSX dimensions

takiAA9dc0abae4f7e
diff --git a/nanobot/utils/document.py b/nanobot/utils/document.py
index 7e9727ab3..efbbbfd7c 100644
--- a/nanobot/utils/document.py
+++ b/nanobot/utils/document.py
@@ -253,6 +253,8 @@ def _open_xlsx_line_source(path: Path) -> DocumentLineSource:
try:
for sheet_name in workbook.sheetnames:
worksheet = workbook[sheet_name]
+ # Producer-supplied dimensions can omit cells that are present in the XML.
+ worksheet.reset_dimensions()
wrote_header = False
for row_index, row in enumerate(worksheet.iter_rows(values_only=True), 1):
row_text = "\t".join(
diff --git a/tests/tools/test_search_tools.py b/tests/tools/test_search_tools.py
index ab2c3af42..a5569cc2d 100644
--- a/tests/tools/test_search_tools.py
+++ b/tests/tools/test_search_tools.py
@@ -9,6 +9,7 @@ import threading
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
+from zipfile import ZipFile
import pytest
@@ -26,6 +27,7 @@ from nanobot.security.workspace_access import (
default_workspace_scope,
reset_workspace_scope,
)
+from nanobot.utils.document import extract_text
from nanobot.utils.llm_runtime import LLMRuntime
@@ -335,9 +337,13 @@ async def test_grep_defaults_to_match_context(tmp_path: Path) -> None:
@pytest.mark.asyncio
-async def test_grep_searches_xlsx_with_sheet_cell_locator(tmp_path: Path) -> None:
[email protected]("declared_dimension", ["A1:B2", "A1:A1", "A1:A2", "A1:B1"])
+async def test_grep_searches_xlsx_with_sheet_cell_locator(
+ tmp_path: Path, declared_dimension: str, monkeypatch: pytest.MonkeyPatch,
+) -> None:
from openpyxl import Workbook
+ monkeypatch.setattr("nanobot.config.loader._current_config_path", tmp_path / "config.json")
workbook_path = tmp_path / "people.xlsx"
workbook = Workbook()
sheet = workbook.active
@@ -347,6 +353,18 @@ async def test_grep_searches_xlsx_with_sheet_cell_locator(tmp_path: Path) -> Non
workbook.save(workbook_path)
workbook.close()
+ # Some XLSX producers report a smaller used range than their actual cells.
+ with ZipFile(workbook_path) as archive:
+ entries = {item.filename: archive.read(item) for item in archive.infolist()}
+ entries["xl/worksheets/sheet1.xml"] = entries["xl/worksheets/sheet1.xml"].replace(
+ b'<dimension ref="A1:B2"', f'<dimension ref="{declared_dimension}"'.encode(),
+ )
+ with ZipFile(workbook_path, "w") as archive:
+ for name, data in entries.items():
+ archive.writestr(name, data)
+
+ assert extract_text(workbook_path) == "--- Sheet: People ---\nName\tRole\nAda\tEngineer"
+
tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
result = await tool.execute(
pattern="Engineer",