Skip to content

Issue with setColWidths error #538

@ABSOD

Description

@ABSOD

I am using setColWidths to set multiple column widths.
In the past I was able to use multiple setColWidths commands. e.g.:

setColWidths(wb_out,sht,cols = 1, widths = 11)
setColWidths(wb_out,sht,cols = 2, widths = 30)

In this example, "wb_out" is a workbook object and "sht" is an integer value referring to a specific sheet.

I receive the following error when the second setColWidths command is called:

Error in max(sapply(f.out_data, nrow)) :  invalid 'type' (list) of argument

"f.out_data" is a data frame that has been written to the output sheet earlier in the code (see below)

I attempted to apply the following command to change multiple columns with one command, but received the same error:

setColWidths(wb_out,sht,cols = c(1,2), widths = c(11,30))

Here is the extended code:

          title <- odata[[addr1]]$title
          f.out_data <- odata[[addr1]]$data
          f.hdr <- list_to_df(odata[[addr1]]$header)
          source <- str_replace(odata[[addr1]]$source,"\n"," ")
          
          # Row Positions
          stRow <- 1
          nRow <- nrow(f.out_data)
          nCol <- ncol(f.out_data)
          writeData(wb_out, sht,title, startRow=stRow,startCol=1,colNames=FALSE)
          addStyle(wb_out, sht, style = head_S1, rows = stRow, cols = 1, gridExpand = TRUE) # Sheet Title
          stRow <- stRow + 2
          endRow <- stRow + 1
          writeData(wb_out, sht,f.hdr, startRow=stRow,startCol=1,colNames=FALSE)
          addStyle(wb_out, sht, style = columnhead_S1, rows = stRow:endRow, cols = 1:nCol, gridExpand = TRUE) 
         # Column headings
          stRow <- endRow + 1
          endRow <- stRow + nrow(f.out_data)
          writeData(wb_out, sht,f.out_data, startRow=stRow,startCol = 1, colNames=FALSE) 
          addStyle(wb_out, sht, style = cells_S1, rows = stRow:endRow, cols = 1:2, gridExpand = TRUE)
          addStyle(wb_out, sht, style = cells_S2, rows = stRow:endRow, cols = 3:nCol, gridExpand = TRUE)
          
          stRow <- stRow + endRow
          writeData(wb_out, sht,source, startRow=stRow,startCol=1,colNames=FALSE)

            mergeCells(wb_out,sht,cols=6:7, rows=3)
            mergeCells(wb_out,sht,cols=8:9, rows=3)
            mergeCells(wb_out,sht,cols=10:11, rows=3)
            mergeCells(wb_out,sht,cols=12:13, rows=3)
            mergeCells(wb_out,sht,cols=14:15, rows=3)
            
            
            # Setting Row heights and Column Width
            setColWidths(wb_out,sht,cols = c(1,2), widths = c(11,30))

Additional context
This error is new. I am using R version 4.6.0, R-Studio 2026.05.0 Build 218, and openxlsx version 4.2.8.1

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