Describe the bug
There's a bug in the Workbook object method setColWidths() (not the exported function by the same name) that causes saveWorkbook() to return an error when called after the user has set column widths for a range of columns (e.g. 1:2), saved the workbook, then set column widths for a new range that is inclusive of the previous one (e.g. 1:5).
The error that is returned is:
Error in sprintf("<col min=\"%s\" max=\"%s\" width=\"%s\" hidden=\"%s\" customWidth=\"1\"/>", :
arguments cannot be recycled to the same length
To Reproduce
The following code demonstrates the issue:
# Write some data to a workbook object.
wb <- createWorkbook()
addWorksheet(wb, "iris")
writeData(wb, "iris", iris)
# Set column widths and save the workbook.
setColWidths(wb, "iris", cols = 1:2, 12)
tf <- tempfile(fileext = ".xlsx")
saveWorkbook(wb, file = tf, overwrite = TRUE)
# Set column widths again for a different range inclusive of the previous one and save the workbook.
setColWidths(wb, "iris", cols = 1:5, 15)
saveWorkbook(wb, file = tf, overwrite = TRUE)
Additional context
Here cols is subset to the columns that have not previously been set, but widths and hidden are not:
|
cols <- cols[!cols %in% names(worksheets[[sheet]]$cols)] |
This means that these three objects are no longer the same length when they are passed to sprintf() here:
|
colNodes <- sprintf('<col min="%s" max="%s" width="%s" hidden="%s" customWidth="1"/>', cols, cols, widths, hidden) |
Describe the bug
There's a bug in the Workbook object method
setColWidths()(not the exported function by the same name) that causessaveWorkbook()to return an error when called after the user has set column widths for a range of columns (e.g. 1:2), saved the workbook, then set column widths for a new range that is inclusive of the previous one (e.g. 1:5).The error that is returned is:
To Reproduce
The following code demonstrates the issue:
Additional context
Here
colsis subset to the columns that have not previously been set, butwidthsandhiddenare not:openxlsx/R/workbook_column_widths.R
Line 157 in 2bf325a
This means that these three objects are no longer the same length when they are passed to
sprintf()here:openxlsx/R/workbook_column_widths.R
Line 162 in 2bf325a