Skip to content

Commit

Permalink
[Excel Analyzer] Count hidden non-blank cells
Browse files Browse the repository at this point in the history
We don't care about blank cells in hidden rows and columns.
  • Loading branch information
gbp committed Jan 17, 2024
1 parent 9551bec commit f7a7af0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
18 changes: 16 additions & 2 deletions gems/excel_analyzer/lib/excel_analyzer/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,27 @@ def external_links

def hidden_columns
workbook.worksheets.sum do |sheet|
sheet.cols.compact.select(&:hidden).sum { _1.max - _1.min + 1 }
hidden_columns = []

sheet.cols.compact.select(&:hidden).each do |col_range|
hidden_columns += (col_range.min..col_range.max).to_a
end

hidden_columns.count do |c|
cells = sheet.sheet_data.rows.map { _1[c - 1] }
cells.compact.any? { !_1.value.to_s.empty? }
end
end
end

def hidden_rows
workbook.worksheets.sum do |sheet|
sheet.sheet_data.rows.compact.count(&:hidden)
hidden_rows = sheet.sheet_data.rows.compact.select(&:hidden)

hidden_rows.count do |row|
cells = row.cells
cells.compact.any? { !_1.value.to_s.empty? }
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions gems/excel_analyzer/spec/excel_analyzer/xls_analyzer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
end

it "detects hidden columns" do
expect(metadata[:excel][:hidden_columns]).to eq 1
expect(metadata[:excel][:hidden_columns]).to eq 2
end

it "detects hidden rows" do
expect(metadata[:excel][:hidden_rows]).to eq 1
expect(metadata[:excel][:hidden_rows]).to eq 2
end

it "detects hidden sheets" do
Expand Down
4 changes: 2 additions & 2 deletions gems/excel_analyzer/spec/excel_analyzer/xlsx_analyzer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
end

it "detects hidden columns" do
expect(metadata[:excel][:hidden_columns]).to eq 1
expect(metadata[:excel][:hidden_columns]).to eq 2
end

it "detects hidden rows" do
expect(metadata[:excel][:hidden_rows]).to eq 1
expect(metadata[:excel][:hidden_rows]).to eq 2
end

it "detects hidden sheets" do
Expand Down
Binary file modified gems/excel_analyzer/spec/fixtures/suspect.xls
Binary file not shown.
Binary file modified gems/excel_analyzer/spec/fixtures/suspect.xlsx
Binary file not shown.

0 comments on commit f7a7af0

Please sign in to comment.