sehock
sehock

Reputation: 399

Hiding Columns in Excel Workbooks using openxlsx

I'm trying to hide columns in an Excel workbook I'm creating using the openxlsx package in R, but I'm not having any luck. I could make a pre-formatted Excel workbook and write data to it, but the number of columns that need to be hidden is variable. I've tried using:

 setColWidths(wb, sheet = "Sheet 1", cols = col_list, widths = 0)

where "col_list" is the list of columns that need to be hidden. When I open the saved workbook, the column widths are actually 0.38. If anybody has any ideas on how I might go about this, it would be much appreciated.

Upvotes: 8

Views: 5285

Answers (3)

Fevertree
Fevertree

Reputation: 11

I discovered an accidental backdoor by using NA

setColWidths(wb, sheet = "Sheet 1", cols = col_list, widths = NA)

Upvotes: 1

Shane D
Shane D

Reputation: 101

Documentation shows groupColumns() has an option for hidden. It worked better for me.

The man page notes that it conflicts with the parameter in setColWidths(). (I think conflicts with is too strong. It's just duplicate).

https://rdrr.io/cran/openxlsx/man/groupColumns.html

To @Sean in comments, you can hide rows with groupRows(hidden = TRUE).

groupRows(wb, sheet = "Sheet 1", rows = 6:12, hidden = TRUE)

Upvotes: 0

Christian
Christian

Reputation: 81

The documentation shows the fields for the formula as below:

setColWidths(wb, sheet, cols, widths = 8.43, hidden = rep(FALSE, length(cols)), ignoreMergedCells = FALSE)

So setting hidden = rep(TRUE, length(cols)) should hide your columns

Upvotes: 8

Related Questions