tealeg / xlsx

Go library for reading and writing XLSX files.
Other
5.85k stars 819 forks source link

Columns seem to be numbered from 1 but documentation gives example using 0 #805

Closed bradleypeabody closed 3 months ago

bradleypeabody commented 4 months ago

Using the latest v3 as of this writing, when trying to set column widths following the example from here: https://github.com/tealeg/xlsx/blob/master/tutorial/tutorial.adoc#working-with-columns

The example reads as follows:

// creating a column that relates to worksheet columns A thru E (index 0 to 4)
newColumn := NewColForRange(0,4)
...

I tried this using xlsx.NewColForRange(0,0) and when I open the resulting file with LibreOffice I get an error saying The data could not be loaded completely because the maximum number of columns per sheet was exceeded. and the column width is not applied.

After unzipping and looking at the raw output and comparing a working sheet saved by LibreOffice vs what the above was outputting, it seems like column index 0 was put literally into the XML, when it looks like the first column is supposed to be numbered 1 (based on dissecting a working file created with LibreOffice manually). Changing the code to xlsx.NewColForRange(1,1) produces the desired result.

I suggest updating the example and mentioning both in the tutorial and in the relevant function comments (e.g. for NewColForRange(), Col() and any others) that column indexes start from 1 (i.e. col A=1, B=2, etc; not zero as is the case with rows).

tealeg commented 3 months ago

This is definitely a bug - it's causing the output files to be considered corrupt when opening in Excel too. (See #788 )

bradleypeabody commented 3 months ago

@tealeg Although I haven't thoroughly tested it, it seems like just documenting that column indexes start from 1 (not 0) would solve it, no? That would be easy and would reflect the functionality as it exists right now.

tealeg commented 3 months ago

Yup. Fixed in this commit