@@ -450,6 +450,18 @@ def clear_content(self):
450
450
for e in self .xpath ("./*[not(self::w:tcPr)]" ):
451
451
self .remove (e )
452
452
453
+ @property
454
+ def grid_offset (self ) -> int :
455
+ """Starting offset of `tc` in the layout-grid columns of its table.
456
+
457
+ A cell in the leftmost grid-column has offset 0.
458
+ """
459
+ grid_before = self ._tr .grid_before
460
+ preceding_tc_grid_spans = sum (
461
+ tc .grid_span for tc in self .xpath ("./preceding-sibling::w:tc" )
462
+ )
463
+ return grid_before + preceding_tc_grid_spans
464
+
453
465
@property
454
466
def grid_span (self ) -> int :
455
467
"""The integer number of columns this cell spans.
@@ -484,7 +496,7 @@ def iter_block_items(self):
484
496
@property
485
497
def left (self ) -> int :
486
498
"""The grid column index at which this ``<w:tc>`` element appears."""
487
- return self ._grid_col
499
+ return self .grid_offset
488
500
489
501
def merge (self , other_tc : CT_Tc ) -> CT_Tc :
490
502
"""Return top-left `w:tc` element of a new span.
@@ -510,7 +522,7 @@ def right(self) -> int:
510
522
This is one greater than the index of the right-most column of the span, similar
511
523
to how a slice of the cell's columns would be specified.
512
524
"""
513
- return self ._grid_col + self .grid_span
525
+ return self .grid_offset + self .grid_span
514
526
515
527
@property
516
528
def top (self ) -> int :
@@ -553,14 +565,6 @@ def _add_width_of(self, other_tc: CT_Tc):
553
565
if self .width and other_tc .width :
554
566
self .width = Length (self .width + other_tc .width )
555
567
556
- @property
557
- def _grid_col (self ) -> int :
558
- """The grid column at which this cell begins."""
559
- tr = self ._tr
560
- idx = tr .tc_lst .index (self )
561
- preceding_tcs = tr .tc_lst [:idx ]
562
- return sum (tc .grid_span for tc in preceding_tcs )
563
-
564
568
def _grow_to (self , width : int , height : int , top_tc : CT_Tc | None = None ):
565
569
"""Grow this cell to `width` grid columns and `height` rows.
566
570
@@ -727,15 +731,15 @@ def _tbl(self) -> CT_Tbl:
727
731
@property
728
732
def _tc_above (self ) -> CT_Tc :
729
733
"""The `w:tc` element immediately above this one in its grid column."""
730
- return self ._tr_above .tc_at_grid_col (self ._grid_col )
734
+ return self ._tr_above .tc_at_grid_col (self .grid_offset )
731
735
732
736
@property
733
737
def _tc_below (self ) -> CT_Tc | None :
734
738
"""The tc element immediately below this one in its grid column."""
735
739
tr_below = self ._tr_below
736
740
if tr_below is None :
737
741
return None
738
- return tr_below .tc_at_grid_col (self ._grid_col )
742
+ return tr_below .tc_at_grid_col (self .grid_offset )
739
743
740
744
@property
741
745
def _tr (self ) -> CT_Row :
0 commit comments