Skip to content

Issue when setting col widths and saving workbook multiple times #493

Description

@jistria

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions