Skip to content

Commit

Permalink
Option to skip allocation for empty cell.
Browse files Browse the repository at this point in the history
This option skips creation of empty cell and assignment of key, value pair for such empty cell in hash.
  • Loading branch information
chopraanmol1 committed Nov 13, 2018
1 parent 5fc4378 commit d68f002
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ def initialize(filename_or_stream, options = {})
sheet_options = {}
sheet_options[:expand_merged_ranges] = (options[:expand_merged_ranges] || false)
sheet_options[:no_hyperlinks] = (options[:no_hyperlinks] || false)
sheet_options[:empty_cells] = (options[:empty_cells] || false)
shared_options = {}

shared_options[:disable_html_wrapper] = (options[:disable_html_wrapper] || false)
unless is_stream?(filename_or_stream)
file_type_check(filename_or_stream, %w[.xlsx .xlsm], 'an Excel 2007', file_warning, packed)
Expand Down
17 changes: 13 additions & 4 deletions lib/roo/excelx/sheet_doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def cell_value_type(type, format)
# # => <Excelx::Cell::String>
#
# Returns a type of <Excelx::Cell>.
def cell_from_xml(cell_xml, hyperlink, coordinate = nil)
def cell_from_xml(cell_xml, hyperlink, coordinate, empty_cell=true)
coordinate ||= ::Roo::Utils.extract_coordinate(cell_xml[COMMON_STRINGS[:r]])
cell_xml_children = cell_xml.children
return Excelx::Cell::Empty.new(coordinate) if cell_xml_children.empty?
return create_empty_cell(coordinate, empty_cell) if cell_xml_children.empty?

# NOTE: This is error prone, to_i will silently turn a nil into a 0.
# This works by coincidence because Format[0] is General.
Expand Down Expand Up @@ -117,7 +117,13 @@ def cell_from_xml(cell_xml, hyperlink, coordinate = nil)
end
end

Excelx::Cell::Empty.new(coordinate)
create_empty_cell(coordinate)
end

def create_empty_cell(coordinate, empty_cell)
if empty_cell
Excelx::Cell::Empty.new(coordinate)
end
end

def create_cell_from_value(value_type, cell, formula, format, style, hyperlink, coordinate)
Expand Down Expand Up @@ -199,9 +205,12 @@ def expand_merged_ranges(cells)

def extract_cells(relationships)
extracted_cells = {}
empty_cell = @options[:empty_cell]

doc.xpath('/worksheet/sheetData/row/c').each do |cell_xml|
coordinate = ::Roo::Utils.extract_coordinate(cell_xml[COMMON_STRINGS[:r]])
extracted_cells[coordinate] = cell_from_xml(cell_xml, hyperlinks(relationships)[coordinate], coordinate)
cell = cell_from_xml(cell_xml, hyperlinks(relationships)[coordinate], coordinate, empty_cell)
extracted_cells[coordinate] = cell if cell
end

expand_merged_ranges(extracted_cells) if @options[:expand_merged_ranges]
Expand Down

0 comments on commit d68f002

Please sign in to comment.