fix(documents): read cells beyond declared XLSX dimensions
This commit is contained in:
1 parent
24033152c7
commit
9dc0abae4f
2 files changed
+21
-1
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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'<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",
|
||||
|
||||
Reference in new issue
Block a user