How do I hide columns using rubyXL 3.4.0 or later?
The example here (https://github.com/weshatheleopard/rubyXL/issues/145) appears to be out-of-date. (sheet.cols.find
now returns an Enumerator
, so doesn't have a hidden
method.)
Code from rubyXL issue #145:
# Assuming that the cells/rows/cols are respectively locked in the test file:
doc = RubyXL::Parser.parse('test.xlsx')
sheet = doc.worksheets[0]
sheet.sheet_data.rows[0].hidden
=> nil
sheet.sheet_data.rows[1].hidden
=> true
(c = sheet.cols.find(0)) && c.hidden
=> nil
(c = sheet.cols.find(1)) && c.hidden
=> true
xf = doc.workbook.cell_xfs[c.style_index || 0]
xf.apply_protection && xf.protection.locked
=> true
xf.apply_protection && xf.protection.hidden
=> true
I figured it out:
The following code does generate a new workbook with column B hidden:
(I also put a copy of this code here: https://github.com/kurmasz/rubyXL-recipes)