rrxcell 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6046fab2b9b6c239f6a76716b1a3a5159532eb9
4
- data.tar.gz: 95614643bd2a05dc25200344745f6719e8a986b8
3
+ metadata.gz: 35618ff57d747cacd603dd2d4771f08c71a2bba4
4
+ data.tar.gz: e7bd1240b834f139594b606046729ed5feb8c430
5
5
  SHA512:
6
- metadata.gz: c1bd9daa5fcded11c6c7c16eadc1b3aa44ce2e2c4e03e3edac1a551d9e0376cc367e3c62110d34a2476c858909ad1551d35baa8ff874aaa60478029b616322c8
7
- data.tar.gz: d4926d02b35e2e25e10a4de29032e1ecdf2444e4d158bc36bf76ba2447743c5a8359951313bd15621a9e54a1d6ed049597f02c6f060cc19310b716f9075863be
6
+ metadata.gz: 060bbc3ee2ba1bf40907683b7a82fcf545c81c77c3fa70ca6e69ccb6b3bffde2bbd8d1317e3ed37336f5af41dd7337c810bc1ffa8e1459432d7b05c2632945e7
7
+ data.tar.gz: cfc5c9021e196c8ecd46d8fcb24280200aa4bd3e6632a3454546c8b01fae35c144fea127433a7032757ac351f9e506544efdf7624ffbae5555df092dfa713592
data/.gitignore CHANGED
@@ -103,3 +103,6 @@ Icon
103
103
  Network Trash Folder
104
104
  Temporary Items
105
105
  .apdisk
106
+
107
+ # excel temporary file
108
+ ~$*
data/README.md CHANGED
@@ -25,7 +25,8 @@ require "rrxcell"
25
25
 
26
26
  #--------------------#
27
27
  # Excelx file with '.xlsx'
28
- path = File.expand_path("PATH/TO/FILE.xlsx")
28
+ # Excel file with '.xls'
29
+ path = File.expand_path("PATH/TO/FILE.xlsx") # or "PATH/TO/FILE.xls"
29
30
  book = Rrxcell.load(path)
30
31
 
31
32
  puts book.sheet(0).row(0).column(0).value
@@ -34,16 +35,18 @@ puts book.sheet(0).row(0).column(0).value
34
35
  puts book[0][1][2].value
35
36
  # => "Sheet1!C2"
36
37
 
37
- #--------------------#
38
- # Excelx file with '.xls'
39
- path = File.expand_path("PATH/TO/FILE.xls")
40
- book = Rrxcell.load(path)
38
+ puts book.address("Sheet1!B2").value
39
+ # => "Sheet1!B2"
41
40
 
42
- puts book.sheet(0).row(0).column(0).value
43
- # => "Sheet1!A1"
41
+ puts book.sheet(0).address("B2").value
42
+ # => "Sheet1!B2"
43
+
44
+ puts book.sheet_names
45
+ # => ["Sheet1"]
46
+
47
+ puts book.sheet("Sheet1").address("AB3").value
48
+ # => "Sheet1!AB3"
44
49
 
45
- puts book[0][1][2].value
46
- # => "Sheet1!C2"
47
50
 
48
51
  ```
49
52
 
data/example/book.xls CHANGED
Binary file
data/example/book.xlsx CHANGED
Binary file
@@ -0,0 +1,73 @@
1
+ module Rrxcell::Addressing
2
+ ALPHABET_CHARCTORS = 26
3
+
4
+ class << self
5
+ def cell_address_to_column_position(cell_address)
6
+ v = 0
7
+ factor = 1
8
+
9
+ column_string(cell_address).reverse.each_char do |char|
10
+ _v = char.downcase.ord - "a".ord + 1
11
+ v += ((_v * factor) - 1)
12
+ factor += ALPHABET_CHARCTORS
13
+ end
14
+
15
+ v
16
+ end
17
+
18
+ def cell_address_to_row_column_position(cell_address)
19
+ [
20
+ cell_address_to_row_position(cell_address),
21
+ cell_address_to_column_position(cell_address)
22
+ ]
23
+ end
24
+
25
+ def cell_address_to_row_position(cell_address)
26
+ row_string(cell_address).to_i - 1
27
+ end
28
+
29
+ def cell_address_to_sheet_position(cell_address, book = nil)
30
+ validate_book!(book)
31
+ book.sheet_names.index(sheet_string(cell_address))
32
+ end
33
+
34
+ def cell_address_to_sheet_row_column_position(cell_address, book = nil)
35
+ validate_book!(book)
36
+ [
37
+ cell_address_to_sheet_position(cell_address, book),
38
+ cell_address_to_row_column_position(cell_address)
39
+ ].flatten
40
+ end
41
+
42
+ private
43
+
44
+ def column_string(cell_address)
45
+ validate_cell_address!(cell_address)
46
+ cell_address.scan(/\!*([[:alpha:]]+)/).flatten.last
47
+ end
48
+
49
+ def row_string(cell_address)
50
+ validate_cell_address!(cell_address)
51
+ cell_address.match(/[0-9]+\z/).to_s
52
+ end
53
+
54
+ def sheet_string(cell_address)
55
+ validate_cell_address!(cell_address)
56
+ cell_address.scan(/\A(.+)\!/).flatten.first
57
+ end
58
+
59
+ def validate_cell_address!(cell_address)
60
+ return true if cell_address.match(/\A[[:alpha:]]+[0-9]+\z/)
61
+
62
+ unless cell_address.match(/\A.+\![[:alpha:]]+[0-9]+\z/)
63
+ raise "invalid address"
64
+ end
65
+ true
66
+ end
67
+
68
+ def validate_book!(book)
69
+ raise "invalid Rrxcell::Book" unless book.is_a?(Rrxcell::Book)
70
+ true
71
+ end
72
+ end
73
+ end
data/lib/rrxcell/book.rb CHANGED
@@ -6,16 +6,49 @@ class Rrxcell::Book
6
6
  @path = path
7
7
  end
8
8
 
9
- def [](position)
10
- sheet(position)
9
+ def [](position_or_sheet_name)
10
+ case position_or_sheet_name
11
+ when Numeric
12
+ sheet(position_or_sheet_name)
13
+ when String
14
+ sheet(sheet_position_by_name!(position_or_sheet_name))
15
+ else
16
+ raise "invalid sheet position"
17
+ end
18
+ end
19
+
20
+ def address(cell_address)
21
+ sheet_position, row_position, column_position =
22
+ Rrxcell::Addressing.cell_address_to_sheet_row_column_position(cell_address, self)
23
+
24
+ sheet(sheet_position).row(row_position).column(column_position)
11
25
  end
12
26
 
13
27
  def sheet(position)
14
28
  raise ShouldBeImplementedError
15
29
  end
16
30
 
31
+ def sheets
32
+ sheet_names.map { |s| sheet(sheet_names.index(s)) }
33
+ end
34
+
35
+ def sheet_names
36
+ object.sheets
37
+ end
38
+
17
39
  protected
18
40
 
19
41
  attr_reader :book
20
42
 
43
+ private
44
+
45
+ def cell_address_to_sheet_row_column_position(cell_address)
46
+ validate_cell_address!(cell_address)
47
+ end
48
+
49
+ def sheet_position_by_name!(name)
50
+ pos = sheet_names.index(name)
51
+ raise if pos.nil?
52
+ pos
53
+ end
21
54
  end
data/lib/rrxcell/cell.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class Rrxcell::Cell
2
- attr_reader :row
2
+ attr_reader :row, :position
3
3
 
4
4
  def initialize(row, position)
5
5
  super()
@@ -7,6 +7,18 @@ class Rrxcell::Cell
7
7
  @position = position
8
8
  end
9
9
 
10
+ def book
11
+ row.book
12
+ end
13
+
14
+ def object
15
+ raise ShouldBeImplementedError
16
+ end
17
+
18
+ def sheet
19
+ row.sheet
20
+ end
21
+
10
22
  def value
11
23
  raise ShouldBeImplementedError
12
24
  end
@@ -3,10 +3,10 @@ require "rrxcell/book"
3
3
 
4
4
  class Rrxcell::Excel::Book < Rrxcell::Book
5
5
  def object
6
- @object ||= Roo::Excel.new(@path)
6
+ @object ||= Roo::Excel.new(path)
7
7
  end
8
8
 
9
- def sheet(position)
10
- Rrxcell::Excel::Sheet.new(self, position)
9
+ def sheet(sheet_position)
10
+ Rrxcell::Excel::Sheet.new(self, sheet_position)
11
11
  end
12
12
  end
@@ -2,7 +2,30 @@ require "rrxcell/excel"
2
2
  require "rrxcell/cell"
3
3
 
4
4
  class Rrxcell::Excel::Cell < Rrxcell::Cell
5
+ def type
6
+ book.object.celltype(row.position + 1 , position + 1, sheet.position)
7
+ end
8
+
5
9
  def value
6
- @row.sheet.book.object.cell(@row.position + 1, @position + 1, @row.sheet.position)
10
+ case type
11
+ when :time
12
+ value_as_time
13
+ else
14
+ raw_value
15
+ end
7
16
  end
17
+
18
+ private
19
+
20
+ def raw_value
21
+ book.object.cell(row.position + 1, position + 1, sheet.position)
22
+ end
23
+
24
+ def value_as_time
25
+ h = (raw_value / 3600).to_i
26
+ m = (((raw_value / 60).to_i) % 60).to_i
27
+ s = (raw_value % 60).to_i
28
+ today = Date.today
29
+ Time.new(today.year, today.month, today.day, h, m, s)
30
+ end
8
31
  end
@@ -2,11 +2,11 @@ require "rrxcell/excel"
2
2
  require "rrxcell/row"
3
3
 
4
4
  class Rrxcell::Excel::Row < Rrxcell::Row
5
- def column(position)
6
- Rrxcell::Excel::Cell.new(self, position)
5
+ def column(column_position)
6
+ Rrxcell::Excel::Cell.new(self, column_position)
7
7
  end
8
8
 
9
9
  def object
10
- @sheet.object.row(@position)
10
+ sheet.object.row(position)
11
11
  end
12
12
  end
@@ -3,10 +3,10 @@ require "rrxcell/sheet"
3
3
 
4
4
  class Rrxcell::Excel::Sheet < Rrxcell::Sheet
5
5
  def object
6
- @object ||= @book.object.worksheets[@position]
6
+ @object ||= book.object.worksheets[position]
7
7
  end
8
8
 
9
- def row(position)
10
- Rrxcell::Excel::Row.new(self, position)
9
+ def row(row_position)
10
+ Rrxcell::Excel::Row.new(self, row_position)
11
11
  end
12
12
  end
@@ -3,7 +3,7 @@ require "rrxcell/book"
3
3
 
4
4
  class Rrxcell::Excelx::Book < Rrxcell::Book
5
5
  def object
6
- @object ||= Roo::Excelx.new(@path)
6
+ @object ||= Roo::Excelx.new(path)
7
7
  end
8
8
 
9
9
  def sheet(position)
@@ -2,7 +2,34 @@ require "rrxcell/excelx"
2
2
  require "rrxcell/cell"
3
3
 
4
4
  class Rrxcell::Excelx::Cell < Rrxcell::Cell
5
+ def type
6
+ sheet.object.celltype(row.position + 1 , position + 1)
7
+ end
8
+
9
+ def object
10
+ sheet.object.cell(row.position + 1, position + 1)
11
+ end
12
+
5
13
  def value
6
- @row.object.at(@position)
14
+ case type
15
+ when :time
16
+ value_as_time
17
+ else
18
+ raw_value
19
+ end
7
20
  end
21
+
22
+ private
23
+
24
+ def raw_value
25
+ row.object.at(position)
26
+ end
27
+
28
+ def value_as_time
29
+ h = (raw_value / 3600).to_i
30
+ m = (((raw_value / 60).to_i) % 60).to_i
31
+ s = (raw_value % 60).to_i
32
+ today = Date.today
33
+ Time.new(today.year, today.month, today.day, h, m, s)
34
+ end
8
35
  end
@@ -7,6 +7,6 @@ class Rrxcell::Excelx::Row < Rrxcell::Row
7
7
  end
8
8
 
9
9
  def object
10
- @sheet.object.row(@position + 1)
10
+ sheet.object.row(position + 1)
11
11
  end
12
12
  end
@@ -3,10 +3,10 @@ require "rrxcell/sheet"
3
3
 
4
4
  class Rrxcell::Excelx::Sheet < Rrxcell::Sheet
5
5
  def object
6
- @book.object.sheet(@position)
6
+ book.object.sheet(position)
7
7
  end
8
8
 
9
- def row(position)
10
- Rrxcell::Excelx::Row.new(self, position)
9
+ def row(row_position)
10
+ Rrxcell::Excelx::Row.new(self, row_position)
11
11
  end
12
12
  end
data/lib/rrxcell/row.rb CHANGED
@@ -11,6 +11,10 @@ class Rrxcell::Row
11
11
  column(position)
12
12
  end
13
13
 
14
+ def book
15
+ sheet.book
16
+ end
17
+
14
18
  def column(position)
15
19
  raise ShouldBeImplementedError
16
20
  end
data/lib/rrxcell/sheet.rb CHANGED
@@ -7,11 +7,28 @@ class Rrxcell::Sheet
7
7
  @position = position
8
8
  end
9
9
 
10
- def [](position)
11
- row(position)
10
+ def [](row_position)
11
+ row(row_position)
12
12
  end
13
13
 
14
- def row(position)
14
+ def address(cell_address)
15
+ row_position, column_position = Rrxcell::Addressing.cell_address_to_row_column_position(cell_address)
16
+
17
+ row(row_position).column(column_position)
18
+ end
19
+
20
+ def name
21
+ book.sheet_names[position]
22
+ end
23
+
24
+ def object
15
25
  raise ShouldBeImplementedError
16
26
  end
27
+
28
+ def row(row_position)
29
+ raise ShouldBeImplementedError
30
+ end
31
+
32
+ private
33
+
17
34
  end
@@ -1,3 +1,3 @@
1
1
  module Rrxcell
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rrxcell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sekizo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-11 00:00:00.000000000 Z
11
+ date: 2016-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -142,6 +142,7 @@ files:
142
142
  - example/book.xls
143
143
  - example/book.xlsx
144
144
  - lib/rrxcell.rb
145
+ - lib/rrxcell/addressing.rb
145
146
  - lib/rrxcell/book.rb
146
147
  - lib/rrxcell/cell.rb
147
148
  - lib/rrxcell/errors/should_be_implemented_error.rb