Skip to content

Commit

Permalink
Range reference to easily use some feature (#504)
Browse files Browse the repository at this point in the history
This patch introduce following methods to easily use auto_filter,
table, etc.

- Worksheet#top_left_reference
- Worksheet#bottom_right_reference
- Worksheet#range_reference
  • Loading branch information
sato-s committed Feb 12, 2017
1 parent d41258e commit 4539ffd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
5 changes: 2 additions & 3 deletions examples/auto_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
require 'axlsx'
Axlsx::Package.new do |p|
p.workbook.add_worksheet(:name => "Table") do |sheet|
sheet.add_row ["Build Matrix"]
sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
sheet.auto_filter = 'A2:D5'
sheet.auto_filter = sheet.range_reference # => "A1:D4"
sheet.auto_filter.add_column 3, :filters, :filter_items => ['1.9.2']
end
p.workbook.add_worksheet(:name => "Table 2") do |sheet|
Expand All @@ -19,7 +18,7 @@
sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
sheet.auto_filter = 'A2:D5'
sheet.auto_filter = "A2:#{sheet.bottom_right_reference}" # => 'A2:D5'
sheet.auto_filter.add_column 3, :filters, :filter_items => ['1.9.2']
end
end.serialize('auto_filter.xlsx')
47 changes: 47 additions & 0 deletions lib/axlsx/workbook/worksheet/worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,53 @@ def outline_level_columns(start_index, end_index, level = 1, collapsed = true)
outline column_info, (start_index..end_index), level, collapsed
end

# The excel-style reference of the top-left cell of this worksheet, e.g., A1
# @return String
def top_left_reference
raise "worksheet has no data" if rows.empty? or rows.first.empty?
rows.first.first.r
end

# The excel-style reference of the bottom-right cell of this worksheet, e.g., D5
# This method is useful when you want to apply auto_filter or table to dynamic length data.
#
# @example
# Axlsx::Package.new do |p|
# p.workbook.add_worksheet(:name => "Table 2") do |sheet|
# sheet.add_row ["Build Matrix"]
# sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
# sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
# sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
# sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
# sheet.auto_filter = "A2:#{sheet.bottom_right_reference}" # => 'A2:D5'
# sheet.auto_filter.add_column 3, :filters, :filter_items => ['1.9.2']
# end
# end
# @return String
def bottom_right_reference
raise "worksheet has no data" if rows.empty? or rows.last.empty?
rows.last.last.r
end

# The excel-style reference of the range of this worksheet, e.g., A1:D5
# This method is useful when you just want to apply auto_filter or table to whole worksheet.
#
# @example
# Axlsx::Package.new do |p|
# p.workbook.add_worksheet(:name => "Table") do |sheet|
# sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
# sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
# sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
# sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
# sheet.auto_filter = sheet.range_reference # => "A1:D4"
# sheet.auto_filter.add_column 3, :filters, :filter_items => ['1.9.2']
# end
# end
# @return String
def range_reference
"#{top_left_reference}:#{bottom_right_reference}"
end

private

def xml_space
Expand Down
22 changes: 22 additions & 0 deletions test/workbook/worksheet/tc_worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,28 @@ def test_referencing
assert_equal(range.last, @ws.rows.last.cells.last)
end

def test_corner_reference
@ws.add_row [1, 2, 3]
@ws.add_row [4, 5, 6]
assert_equal(@ws.top_left_reference, "A1")
assert_equal(@ws.bottom_right_reference, "C2")
end

def test_corner_reference_raise_exception_if_no_row
assert_raise(RuntimeError, "worksheet has no data"){ @ws.top_left_reference }
assert_raise(RuntimeError, "worksheet has no data"){ @ws.bottom_right_reference }
end

def test_range_reference
@ws.add_row [1, 2, 3]
@ws.add_row [4, 5, 6]
assert_equal(@ws.range_reference, 'A1:C2')
end

def test_range_reference_raise_exception_if_no_row
assert_raise(RuntimeError, "worksheet has no data"){ @ws.range_reference }
end

def test_add_row
assert(@ws.rows.empty?, "sheet has no rows by default")
r = @ws.add_row([1,2,3])
Expand Down

0 comments on commit 4539ffd

Please sign in to comment.