Exemplo n.º 1
0
 def _getRowByIndex(self, idx):
     assert idx > 0, f"Wrong row index: {idx}"
     rown = 0
     for row in self.__sheet.getElementsByType(TableRow):
         n = int(row.getAttribute('numberrowsrepeated') or 1)
         if rown + n > idx:
             if idx - rown == 1:
                 # we are in the target row, just remove the repeated attribute
                 # and insert a new row with repeated decreased by 1
                 row.removeAttribute('numberrowsrepeated')
                 newrow = TableRow(stylename=row.getAttribute('stylename'), numberrowsrepeated=n - 1)
                 self.__sheet.insertBefore(newrow, row.nextSibling)
                 return Row(idx, row)
             # we are in a previous row, just remove the repeated, and add two new rows,
             # one being the target row and another with repeated decreased by two
             row.removeAttribute('numberrowsrepeated')
             nextSibling = row.nextSibling
             newrow = TableRow(stylename=row.getAttribute('stylename'))
             self.__sheet.insertBefore(newrow, nextSibling)
             if n > 2:
                 newnewrow = TableRow(stylename=row.getAttribute('stylename'))
                 if n > 3:
                     newnewrow.setAttribute('numberrowsrepeated', n - 2)
                 self.__sheet.insertBefore(newnewrow, nextSibling)
             return Row(idx, newrow)
         if rown + n == idx:
             return Row(idx, row)
         rown += n
     raise ValueError(f"Error retrieving row {idx}")