@@ -450,6 +450,18 @@ def clear_content(self):
450450 for e in self .xpath ("./*[not(self::w:tcPr)]" ):
451451 self .remove (e )
452452
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+
453465 @property
454466 def grid_span (self ) -> int :
455467 """The integer number of columns this cell spans.
@@ -484,7 +496,7 @@ def iter_block_items(self):
484496 @property
485497 def left (self ) -> int :
486498 """The grid column index at which this ``<w:tc>`` element appears."""
487- return self ._grid_col
499+ return self .grid_offset
488500
489501 def merge (self , other_tc : CT_Tc ) -> CT_Tc :
490502 """Return top-left `w:tc` element of a new span.
@@ -510,7 +522,7 @@ def right(self) -> int:
510522 This is one greater than the index of the right-most column of the span, similar
511523 to how a slice of the cell's columns would be specified.
512524 """
513- return self ._grid_col + self .grid_span
525+ return self .grid_offset + self .grid_span
514526
515527 @property
516528 def top (self ) -> int :
@@ -553,14 +565,6 @@ def _add_width_of(self, other_tc: CT_Tc):
553565 if self .width and other_tc .width :
554566 self .width = Length (self .width + other_tc .width )
555567
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-
564568 def _grow_to (self , width : int , height : int , top_tc : CT_Tc | None = None ):
565569 """Grow this cell to `width` grid columns and `height` rows.
566570
@@ -727,15 +731,15 @@ def _tbl(self) -> CT_Tbl:
727731 @property
728732 def _tc_above (self ) -> CT_Tc :
729733 """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 )
731735
732736 @property
733737 def _tc_below (self ) -> CT_Tc | None :
734738 """The tc element immediately below this one in its grid column."""
735739 tr_below = self ._tr_below
736740 if tr_below is None :
737741 return None
738- return tr_below .tc_at_grid_col (self ._grid_col )
742+ return tr_below .tc_at_grid_col (self .grid_offset )
739743
740744 @property
741745 def _tr (self ) -> CT_Row :
0 commit comments