office: preserve absolute whole-column and whole-row refs
- Commit
c5b40ba6fc22cb4533af95729c5bf0ead7e93b06- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 19:46 UTC (4 days ago)
- Parents
43ccbe2
ExcelFormula translated whole-row references such as $1:$3 into the ODF endpoints .$A1:.$XFD3, putting the absolute anchor on the synthetic A/XFD columns instead of the rows. The reverse translator also dropped the second column anchor in $A:$C, dropped row anchors entirely, and refused to collapse sheet-qualified whole ranges because the expanded second endpoint carries no sheet prefix. A filled-down or saved-and-reloaded formula therefore changed meaning: a row-absolute whole-row range became column-absolute and row-relative, so its rows could move, and $A:$C came back as $A:C. Put the $ on the row in wrapWholeRow, preserve both endpoints anchors and row anchors in wholeColOf/wholeRowOf, and accept the sheet-less second endpoint those wrappers produce. Add a regression test covering absolute whole columns, absolute whole rows, and a sheet-qualified whole column. Verified: the new test fails before the fix (expected =SUM($A:$C), was =SUM($A:C)) and passes after; the full non-native :office:testDevUnitTest batch passes (553 tests). The native quarantine task and cargo builds were excluded because the in-flight Rust rollout leaves Cargo.lock inconsistent with --locked; no Rust was touched.
Diff
office/src/main/java/com/vayunmathur/office/data/odf/ExcelFormula.kt
diff --git a/office/src/main/java/com/vayunmathur/office/data/odf/ExcelFormula.kt b/office/src/main/java/com/vayunmathur/office/data/odf/ExcelFormula.ktindex a62f8274c6..fb06cc89b7 100644--- a/office/src/main/java/com/vayunmathur/office/data/odf/ExcelFormula.kt+++ b/office/src/main/java/com/vayunmathur/office/data/odf/ExcelFormula.kt@@ -170,13 +170,13 @@ internal object ExcelFormula { return "[$pre$colA" + "1:.$colB$MAX_ROWS]" } - /** `2:5` -> `[.A2:.XFD5]`. */+ /** `2:5` -> `[.A2:.XFD5]`; `$2:$5` -> `[.A$2:.XFD$5]` (the $ anchors the row). */ private fun wrapWholeRow(sheet: String, a: String, b: String): String { val (ra, absA) = splitAbs(a) val (rb, absB) = splitAbs(b) val pre = prefixSheet(sheet)- val r1 = "$pre${if (absA) "$" else ""}A${ra.trimStart('$')}"- val r2 = ".${if (absB) "$" else ""}$MAX_COL_LETTERS${rb.trimStart('$')}"+ val r1 = "${pre}A${if (absA) "$" else ""}${ra.trimStart('$')}"+ val r2 = ".$MAX_COL_LETTERS${if (absB) "$" else ""}${rb.trimStart('$')}" return "[$r1:$r2]" } @@ -233,8 +233,9 @@ internal object ExcelFormula { val b = splitEndpoint(parts[1]) ?: return null if (a.row != "1" || b.row != MAX_ROWS.toString()) return null if (a.rowAbs || b.rowAbs) return null- if (a.sheetNorm != b.sheetNorm) return null- return "${a.sheetPrefix}${a.colAbs}${a.col}:${b.col}"+ // The expanded second endpoint carries no sheet prefix (see [wrapWholeCol]).+ if (b.sheetNorm.isNotEmpty() && a.sheetNorm != b.sheetNorm) return null+ return "${a.sheetPrefix}${a.colAbs}${a.col}:${b.colAbs}${b.col}" } private fun wholeRowOf(parts: List<String>): String? {@@ -242,8 +243,11 @@ internal object ExcelFormula { val b = splitEndpoint(parts[1]) ?: return null if (a.col.uppercase() != "A" || b.col.uppercase() != MAX_COL_LETTERS) return null if (a.colAbs.isNotEmpty() || b.colAbs.isNotEmpty()) return null- if (a.sheetNorm != b.sheetNorm) return null- return if (a.sheet.isEmpty()) "${a.row}:${b.row}" else "${a.sheetPrefix}${a.row}:${b.row}"+ // The expanded second endpoint carries no sheet prefix (see [wrapWholeRow]).+ if (b.sheetNorm.isNotEmpty() && a.sheetNorm != b.sheetNorm) return null+ val rowA = "${if (a.rowAbs) "$" else ""}${a.row}"+ val rowB = "${if (b.rowAbs) "$" else ""}${b.row}"+ return "${a.sheetPrefix}$rowA:$rowB" } private data class Endpoint(
office/src/test/java/com/vayunmathur/office/data/odf/W4FormulaNumFmtTest.kt
diff --git a/office/src/test/java/com/vayunmathur/office/data/odf/W4FormulaNumFmtTest.kt b/office/src/test/java/com/vayunmathur/office/data/odf/W4FormulaNumFmtTest.ktindex 42f9460484..354eed92bd 100644--- a/office/src/test/java/com/vayunmathur/office/data/odf/W4FormulaNumFmtTest.kt+++ b/office/src/test/java/com/vayunmathur/office/data/odf/W4FormulaNumFmtTest.kt@@ -109,6 +109,23 @@ class W4FormulaNumFmtTest { assertEquals("=SUM(A:A)", ExcelFormula.fromOdf(col)) } + @Test+ fun `formula absolute whole column and row refs round trip`() {+ // The $ in a whole-column ref anchors the column; both endpoints keep it.+ val absCol = ExcelFormula.toOdf("SUM(\$A:\$C)")+ assertEquals("of:=SUM([.\$A1:.\$C1048576])", absCol)+ assertEquals("=SUM(\$A:\$C)", ExcelFormula.fromOdf(absCol))+ // The $ in a whole-row ref anchors the row, not the synthetic A/XFD columns:+ // $1 must become the row-absolute endpoint .A$1, not the column-absolute .$A1.+ val absRow = ExcelFormula.toOdf("SUM(\$1:\$3)")+ assertEquals("of:=SUM([.A\$1:.XFD\$3])", absRow)+ assertEquals("=SUM(\$1:\$3)", ExcelFormula.fromOdf(absRow))+ // A sheet-qualified whole-column ref still collapses back to whole-column form+ // (the expanded second endpoint carries no sheet prefix).+ val sheetCol = ExcelFormula.toOdf("SUM('My Sheet'!A:A)")+ assertEquals("=SUM('My Sheet'!A:A)", ExcelFormula.fromOdf(sheetCol))+ }+ @Test fun `formula strings and function names are left alone`() { val withString = ExcelFormula.toOdf("""CONCAT("A1", B2)""")