Skip to content

Commit

Permalink
Merge pull request #177 from malept/hidden-sheets
Browse files Browse the repository at this point in the history
Add optional hidden sheets support
  • Loading branch information
simonoff committed Mar 23, 2015
2 parents df1823c + 1097545 commit f5d1f93
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,11 @@ def initialize(filename, options = {})
@rels_files = []
process_zipfile(@tmpdir, @filename)

@sheet_names = workbook.sheets.map { |sheet| sheet['name'] }
@sheet_names = workbook.sheets.map do |sheet|
unless options[:only_visible_sheets] && sheet['state'] == 'hidden'
sheet['name']
end
end.compact
@sheets = []
@sheets_by_name = Hash[@sheet_names.map.with_index do |sheet_name, n|
@sheets[n] = Sheet.new(sheet_name, @rels_files[n], @sheet_files[n], @comments_files[n], styles, shared_strings, workbook)
Expand Down
33 changes: 27 additions & 6 deletions lib/roo/open_office.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(filename, options={})
packed = options[:packed]
file_warning = options[:file_warning] || :error

@only_visible_sheets = options[:only_visible_sheets]
file_type_check(filename,'.ods','an Roo::OpenOffice', file_warning, packed)
@tmpdir = make_tmpdir(filename.split('/').last, options[:tmpdir_root])
@filename = local_filename(filename, @tmpdir, packed)
Expand All @@ -33,7 +34,8 @@ def initialize(filename, options={})
@formula = Hash.new
@style = Hash.new
@style_defaults = Hash.new { |h,k| h[k] = [] }
@style_definitions = Hash.new
@table_display = Hash.new { |h,k| h[k] = true }
@font_style_definitions = Hash.new
@comment = Hash.new
@comments_read = Hash.new
end
Expand Down Expand Up @@ -309,7 +311,7 @@ def font(row, col, sheet=nil)
read_cells(sheet)
row,col = normalize(row,col)
style_name = @style[sheet][[row,col]] || @style_defaults[sheet][col - 1] || 'Default'
@style_definitions[style_name]
@font_style_definitions[style_name]
end

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

def sheets
doc.xpath("//*[local-name()='table']").map do |sheet|
sheet.attributes["name"].value
unless @table_display.any?
doc.xpath("//*[local-name()='automatic-styles']").each do |style|
read_table_styles(style)
end
end
doc.xpath("//*[local-name()='table']").map do |sheet|
if !@only_visible_sheets || @table_display[attr(sheet,'style-name')]
sheet.attributes["name"].value
end
end.compact
end

# version of the Roo::OpenOffice document
Expand Down Expand Up @@ -595,7 +604,7 @@ def read_labels
end

def read_styles(style_elements)
@style_definitions['Default'] = Roo::Font.new
@font_style_definitions['Default'] = Roo::Font.new
style_elements.each do |style|
next unless style.name == 'style'
style_name = attr(style,'name')
Expand All @@ -604,7 +613,19 @@ def read_styles(style_elements)
font.bold = attr(properties,'font-weight')
font.italic = attr(properties,'font-style')
font.underline = attr(properties,'text-underline-style')
@style_definitions[style_name] = font
@font_style_definitions[style_name] = font
end
end
end

def read_table_styles(styles)
styles.children.each do |style|
next unless style.name == 'style'
style_name = attr(style,'name')
style.children.each do |properties|
display = attr(properties,'display')
next unless display
@table_display[style_name] = (display == 'true')
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/roo/excelx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@
it 'returns the expected result' do
expect(subject.sheets).to eq ["Tabelle1", "Name of Sheet 2", "Sheet3", "Sheet4", "Sheet5"]
end

describe 'only showing visible sheets' do
let(:path) { 'test/files/hidden_sheets.xlsx' }

it 'returns the expected result' do
expect(Roo::Excelx.new(path, only_visible_sheets: true).sheets).to eq ["VisibleSheet1"]
end
end
end

describe '#sheet_for' do
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/roo/libreoffice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,20 @@
expect(subject).to be_a(Roo::LibreOffice)
end
end

describe '#sheets' do
let(:path) { 'test/files/hidden_sheets.ods' }

describe 'showing all sheets' do
it 'returns the expected result' do
expect(Roo::LibreOffice.new(path).sheets).to eq ["HiddenSheet1", "VisibleSheet1", "HiddenSheet2"]
end
end

describe 'only showing visible sheets' do
it 'returns the expected result' do
expect(Roo::LibreOffice.new(path, only_visible_sheets: true).sheets).to eq ["VisibleSheet1"]
end
end
end
end
Binary file added test/files/hidden_sheets.ods
Binary file not shown.
Binary file added test/files/hidden_sheets.xlsx
Binary file not shown.

0 comments on commit f5d1f93

Please sign in to comment.