[reportlab-users] small bug in setting tablestyle ???

Robin Becker robin at reportlab.com
Wed Feb 4 13:13:38 EST 2009


James Yoo wrote:

> Hello list,

>

.........

> 2) if you happen to be setting style for a single cell in the last column

> data[0][1]

>

> eg: TableStyle.add('FONT', (1,0),(1,0), 'Times-Bold')

>

> tables._addCommand does this test to calculate the cell coords for the

> style:

>

> 873 else:

> 874 (op, (sc, sr), (ec, er)), values = cmd[:3] , cmd[3:]

> 875 if sc < 0: sc = sc + self._ncols

> 876 if ec < 0: ec = ec + self._ncols

> 877 if sr < 0: sr = sr + self._nrows

> 878 if er < 0: er = er + self._nrows

> * 879 for i in range(sr, er+1):

> * *880* * for j in range(sc, ec+1):*

> 881 _setCellStyle(self._cellStyles, i, j, op,

> values)

>

> you'll get a list index out of range error because of the the range() in

> line 880

>

> It's simply not checking that your "end column" is actually the index for

> the last column and NOT -1

>

> Of course, I can work around this in my own code, but it seems like a

> relatively minor fix to the code above or a minor adjustment to the

> documentation.


If it's a bug then it's good to know, but here's what happens when I simulate
your test scenario by hand.

>>> cmd=('FONT', (1,0),(1,0), 'Times-Bold')
>>> (op, (sc, sr), (ec, er)), values = cmd[:3] , cmd[3:]
>>> for i in range(sr, er+1):
... for j in range(sc, ec+1):
... print i,j
...
0 1
>>>

so we ought to be addressing only i=0 and j=1 which we claim is there providing
we've added one style for each cell which we attempt to do in the init method
where we set

cellrows = []
for i in xrange(nrows):
cellcols = []
for j in xrange(ncols):
cellcols.append(CellStyle(`(i,j)`))
cellrows.append(cellcols)
self._cellStyles = cellrows


For what it's worth I see your code at line 988 so our versions are out of sync;
therefore it might be that something has been fixed.

Can you illustrate your cell/style addition with a tiny example? I/we have used
essentially this technique before so I believe it ought to work.

It is of course easy to break this simplistic scheme by just adding in styles
for cells which don't exist. So logically the sr, er, sc & ec values ought to be
checked against the actual table dimensions.
--
Robin Becker


More information about the reportlab-users mailing list