Skip to content

The agent that grows with you

fix(skills): keep nested-table order and table column names intact

Teknium395e89060fe3
diff --git a/skills/productivity/microsoft-office/scripts/docx_read.py b/skills/productivity/microsoft-office/scripts/docx_read.py
index 8a56aa6efc1..eb9991be22e 100644
--- a/skills/productivity/microsoft-office/scripts/docx_read.py
+++ b/skills/productivity/microsoft-office/scripts/docx_read.py
@@ -25,11 +25,14 @@ from docx import Document
def cell_text(cell) -> str:
- """Flatten a cell's own paragraphs and any nested table text."""
- parts = [p.text for p in cell.paragraphs if p.text]
- for nested in cell.tables:
- for row in table_to_rows(nested):
- parts.extend(text for text in row if text)
+ """Flatten a cell's paragraphs and nested tables in document order."""
+ parts = []
+ for block in cell.iter_inner_content():
+ if hasattr(block, "rows"):
+ for row in table_to_rows(block):
+ parts.extend(text for text in row if text)
+ elif block.text:
+ parts.append(block.text)
return "\n".join(parts)
diff --git a/skills/productivity/microsoft-office/scripts/xlsx_restructure.py b/skills/productivity/microsoft-office/scripts/xlsx_restructure.py
index 536e6c455b2..b35fab3b7cd 100644
--- a/skills/productivity/microsoft-office/scripts/xlsx_restructure.py
+++ b/skills/productivity/microsoft-office/scripts/xlsx_restructure.py
@@ -60,7 +60,9 @@ WHOLE_ROW_RE = re.compile(
rf"(?P<sheet>{SHEET_PATTERN})?"
r"(?P<start>\$?[0-9]{1,7}):(?P<end>\$?[0-9]{1,7})"
r"(?![\w(])")
-STRING_RE = re.compile(r'"(?:[^"]|"")*"')
+# String literals and innermost [...] groups (table column names in structured
+# references such as Table1[[A:B]]) are copied through unrewritten.
+LITERAL_RE = re.compile(r'"(?:[^"]|"")*"|\[[^\[\]]*\]')
COORD_RE = re.compile(r"^(\$?)([A-Za-z]{1,3})(\$?)([0-9]+)$")
@@ -202,7 +204,7 @@ class RefRewriter:
def rewrite(self, text, home_sheet):
"""Rewrite refs outside quoted string literals. Returns new text."""
out, pos = [], 0
- for lit in STRING_RE.finditer(text):
+ for lit in LITERAL_RE.finditer(text):
out.append(self._rewrite_segment(text[pos:lit.start()], home_sheet))
out.append(lit.group(0))
pos = lit.end()
diff --git a/skills/productivity/microsoft-office/tests/test_docx_skill.py b/skills/productivity/microsoft-office/tests/test_docx_skill.py
index c72d82a96e1..abde4035af8 100644
--- a/skills/productivity/microsoft-office/tests/test_docx_skill.py
+++ b/skills/productivity/microsoft-office/tests/test_docx_skill.py
@@ -154,6 +154,7 @@ class TestCreateAndRead:
outer.cell(0, 0).text = "outer"
nested = outer.cell(0, 0).add_table(rows=1, cols=1)
nested.cell(0, 0).text = "nested-only"
+ outer.cell(0, 0).add_paragraph("after-nested")
section = doc.sections[0]
section.different_first_page_header_footer = True
section.first_page_header.paragraphs[0].text = "first-header"
@@ -161,7 +162,7 @@ class TestCreateAndRead:
doc.save(path)
text = run("docx_read.py", path, "--text")
- assert "nested-only" in text["tables"][0][0][0]
+ assert text["tables"][0][0][0].split("\n") == ["outer", "nested-only", "after-nested"]
assert "first-header" in text["headers"]
assert "even-header" in text["headers"]
diff --git a/skills/productivity/microsoft-office/tests/test_xlsx_skill.py b/skills/productivity/microsoft-office/tests/test_xlsx_skill.py
index 24f8f59e9bc..c2140519769 100644
--- a/skills/productivity/microsoft-office/tests/test_xlsx_skill.py
+++ b/skills/productivity/microsoft-office/tests/test_xlsx_skill.py
@@ -398,6 +398,7 @@ def test_restructure_whole_axis_refs(restructure_book):
wb = load_workbook(restructure_book)
wb["Summary"]["C1"] = "=SUM(Data!B:B)"
wb["Summary"]["C2"] = "=SUM(Data!2:2)"
+ wb["Data"]["A20"] = "=SUM(Table1[[B:C]])" # table column names, not columns
wb.save(restructure_book)
report = json.loads(run(
@@ -406,6 +407,7 @@ def test_restructure_whole_axis_refs(restructure_book):
wb = load_workbook(restructure_book)
assert wb["Summary"]["C1"].value == "=SUM(Data!C:C)"
assert wb["Summary"]["C2"].value == "=SUM(Data!2:2)"
+ assert wb["Data"]["A20"].value == "=SUM(Table1[[B:C]])"
assert any(item["cell"] == "C1" for item in report["formulas"])
run("xlsx_restructure.py", restructure_book, "--sheet", "Data",