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: +@pytest.mark.parametrize("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'