Skip to content

Commit f5d1f93

Browse files
committed
Merge pull request #177 from malept/hidden-sheets
Add optional hidden sheets support
2 parents df1823c + 1097545 commit f5d1f93

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed

lib/roo/excelx.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,11 @@ def initialize(filename, options = {})
261261
@rels_files = []
262262
process_zipfile(@tmpdir, @filename)
263263

264-
@sheet_names = workbook.sheets.map { |sheet| sheet['name'] }
264+
@sheet_names = workbook.sheets.map do |sheet|
265+
unless options[:only_visible_sheets] && sheet['state'] == 'hidden'
266+
sheet['name']
267+
end
268+
end.compact
265269
@sheets = []
266270
@sheets_by_name = Hash[@sheet_names.map.with_index do |sheet_name, n|
267271
@sheets[n] = Sheet.new(sheet_name, @rels_files[n], @sheet_files[n], @comments_files[n], styles, shared_strings, workbook)

lib/roo/open_office.rb

+27-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def initialize(filename, options={})
1111
packed = options[:packed]
1212
file_warning = options[:file_warning] || :error
1313

14+
@only_visible_sheets = options[:only_visible_sheets]
1415
file_type_check(filename,'.ods','an Roo::OpenOffice', file_warning, packed)
1516
@tmpdir = make_tmpdir(filename.split('/').last, options[:tmpdir_root])
1617
@filename = local_filename(filename, @tmpdir, packed)
@@ -33,7 +34,8 @@ def initialize(filename, options={})
3334
@formula = Hash.new
3435
@style = Hash.new
3536
@style_defaults = Hash.new { |h,k| h[k] = [] }
36-
@style_definitions = Hash.new
37+
@table_display = Hash.new { |h,k| h[k] = true }
38+
@font_style_definitions = Hash.new
3739
@comment = Hash.new
3840
@comments_read = Hash.new
3941
end
@@ -309,7 +311,7 @@ def font(row, col, sheet=nil)
309311
read_cells(sheet)
310312
row,col = normalize(row,col)
311313
style_name = @style[sheet][[row,col]] || @style_defaults[sheet][col - 1] || 'Default'
312-
@style_definitions[style_name]
314+
@font_style_definitions[style_name]
313315
end
314316

315317
# returns the type of a cell:
@@ -332,9 +334,16 @@ def celltype(row,col,sheet=nil)
332334
end
333335

334336
def sheets
335-
doc.xpath("//*[local-name()='table']").map do |sheet|
336-
sheet.attributes["name"].value
337+
unless @table_display.any?
338+
doc.xpath("//*[local-name()='automatic-styles']").each do |style|
339+
read_table_styles(style)
340+
end
337341
end
342+
doc.xpath("//*[local-name()='table']").map do |sheet|
343+
if !@only_visible_sheets || @table_display[attr(sheet,'style-name')]
344+
sheet.attributes["name"].value
345+
end
346+
end.compact
338347
end
339348

340349
# version of the Roo::OpenOffice document
@@ -595,7 +604,7 @@ def read_labels
595604
end
596605

597606
def read_styles(style_elements)
598-
@style_definitions['Default'] = Roo::Font.new
607+
@font_style_definitions['Default'] = Roo::Font.new
599608
style_elements.each do |style|
600609
next unless style.name == 'style'
601610
style_name = attr(style,'name')
@@ -604,7 +613,19 @@ def read_styles(style_elements)
604613
font.bold = attr(properties,'font-weight')
605614
font.italic = attr(properties,'font-style')
606615
font.underline = attr(properties,'text-underline-style')
607-
@style_definitions[style_name] = font
616+
@font_style_definitions[style_name] = font
617+
end
618+
end
619+
end
620+
621+
def read_table_styles(styles)
622+
styles.children.each do |style|
623+
next unless style.name == 'style'
624+
style_name = attr(style,'name')
625+
style.children.each do |properties|
626+
display = attr(properties,'display')
627+
next unless display
628+
@table_display[style_name] = (display == 'true')
608629
end
609630
end
610631
end

spec/lib/roo/excelx_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@
9191
it 'returns the expected result' do
9292
expect(subject.sheets).to eq ["Tabelle1", "Name of Sheet 2", "Sheet3", "Sheet4", "Sheet5"]
9393
end
94+
95+
describe 'only showing visible sheets' do
96+
let(:path) { 'test/files/hidden_sheets.xlsx' }
97+
98+
it 'returns the expected result' do
99+
expect(Roo::Excelx.new(path, only_visible_sheets: true).sheets).to eq ["VisibleSheet1"]
100+
end
101+
end
94102
end
95103

96104
describe '#sheet_for' do

spec/lib/roo/libreoffice_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,20 @@
1010
expect(subject).to be_a(Roo::LibreOffice)
1111
end
1212
end
13+
14+
describe '#sheets' do
15+
let(:path) { 'test/files/hidden_sheets.ods' }
16+
17+
describe 'showing all sheets' do
18+
it 'returns the expected result' do
19+
expect(Roo::LibreOffice.new(path).sheets).to eq ["HiddenSheet1", "VisibleSheet1", "HiddenSheet2"]
20+
end
21+
end
22+
23+
describe 'only showing visible sheets' do
24+
it 'returns the expected result' do
25+
expect(Roo::LibreOffice.new(path, only_visible_sheets: true).sheets).to eq ["VisibleSheet1"]
26+
end
27+
end
28+
end
1329
end

test/files/hidden_sheets.ods

10.3 KB
Binary file not shown.

test/files/hidden_sheets.xlsx

6.01 KB
Binary file not shown.

0 commit comments

Comments
 (0)