ooxml_excel 0.0.1 → 0.0.2

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: f763147398129f0ac41335c8ddf647bb2540917e
4
- data.tar.gz: e9f93a0c9ee5ec07cdcd8269f77b49b9480e7980
3
+ metadata.gz: e5989596afa2991677b2986fc66c4aa8be2c24af
4
+ data.tar.gz: ddffed3aadfe48b0f9a7d4c5299eed9a441dbe54
5
5
  SHA512:
6
- metadata.gz: d3a06ce3169938a146da26eb347ffbc812b7b15627b04d237407fb25ed7138a2b31f298293a12e231a54e9a0756ab18092b9b24ac393e580b2bfb8c16727cbe0
7
- data.tar.gz: cb765ec3f0bb940938f7a4ba968b7f8aa54549f0f7040eb6c4e60dd1165804ec72466c91649d24558903c7914e188ec7ecee93dd2cecac33399ed624b51eb424
6
+ metadata.gz: c6add5668d0617f49da6bf79f3c0dd3083ae797cfe9b2a75b844fea1091c6a396a6479ba443973b28bd74a0a7de1a6287692529b831b2816b30d320f830030b8
7
+ data.tar.gz: 9ee8046efadb7a0fdf9b89a12f29e7e110b3819bb49f60f3ed9f06103246f4da5ae7748b5aeb0645dc99644ca3f9caa54da4f522fb39807ba83b5fc991e630c1
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ test.rb
11
+ test.xlsm
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # OOXML Excel
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ooxml_excel`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ TODO: Description
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,90 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Using `OOXML::Excel` to read spreadsheet:
24
+ ```
25
+ ooxml_excel = OOXML::Excel.new('example.xlsx')
26
+ ```
27
+
28
+ ### Fetching all sheets:
29
+ ```
30
+ ooxml_excel.sheets # ['Test Sheet 1', 'Test Sheet 2']
31
+ ```
32
+
33
+ ### Accessing Rows and Cells
34
+ ```
35
+ sheet = ooxml_excel.sheet('Test Sheet 1')
36
+
37
+ # Rows
38
+ sheet[0] # Access the first row
39
+ sheet.rows[0] # Same as above
40
+
41
+ # Cells
42
+ sheet[0].cells # Fetch all cells
43
+ sheet.rows[0].cells # longer way to do it
44
+
45
+ sheet[0][0] # Access the first cell of the row
46
+ sheet.rows[0].cells[0] # longer way to do it
47
+
48
+ sheet[0][0].value # Access cell values
49
+ sheet.rows[0].cells[0].value # longer way to do it
50
+
51
+ ```
52
+
53
+ ### Iterating to each row:
54
+ ```
55
+ # as an array of strings
56
+ ooxml_excel.sheet('Test Sheet 1').each_row do |row|
57
+ # Do something here...
58
+ end
59
+
60
+ # as an array of objects
61
+ ooxml_excel.sheet('Test Sheet 1').each_row_as_object do |row|
62
+ # Do something here...
63
+ p row
64
+ end
65
+
66
+ ```
67
+
68
+ ### Fetching Columns:
69
+ ```
70
+ # Fetch all columns
71
+ ooxml_excel.sheet('Test Sheet 1').columns
72
+
73
+ # Checking if the column is hidden
74
+ ooxml_excel.sheet('Test Sheet 1').column(1).hidden? # column index
75
+ ooxml_excel.sheet('Test Sheet 1').column('A').hidden? # column letter
76
+ ```
77
+
78
+ ### Fetching Styles:
79
+
80
+ ```
81
+ # Font
82
+ font_object = ooxml_excel.sheet('Test Sheet 1').font('A1')
83
+ font.bold? # false
84
+ font.name # Arial
85
+ font.rgb_color # FFE10000
86
+ font.size # 8
87
+
88
+ # Cell Fill
89
+ fill_object = ooxml_excel.sheet('Test Sheet 1').fill('A1')
90
+ fill_object.bg_color # FFE10000
91
+ fill_object.fg_color # FFE10000
92
+ ```
93
+
94
+ ### Data Validation
95
+ ```
96
+ # All Validations
97
+ data_validations = ooxml_excel.sheet('Test Sheet 1').data_validations
98
+
99
+ # Specific validation for cell
100
+ data_validation = ooxml.sheet('Input Sheet').data_validation('D4')
101
+
102
+ data_validation.prompt # "Sample Validation Message"
103
+ data_validation.formula # 20
104
+ data_validation.type #textLength
105
+
106
+ ```
26
107
 
27
108
  ## Development
28
109
 
@@ -32,4 +113,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
113
 
33
114
  ## Contributing
34
115
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ooxml_excel.
116
+ Bug reports and pull requests are welcome on GitHub at https://github.com/halcjames/ooxml_excel.
data/lib/ooxml_excel.rb CHANGED
@@ -3,6 +3,7 @@ require 'active_support/all'
3
3
  require 'zip'
4
4
  require "ooxml_excel/version"
5
5
  require "ooxml_excel/helper/list"
6
+ require "ooxml_excel/util"
6
7
  require "ooxml_excel/excel"
7
8
  require "ooxml_excel/styles"
8
9
  require "ooxml_excel/comments"
@@ -15,7 +15,15 @@ module OOXML
15
15
  comment_xml =Nokogiri.XML(comment_xml).remove_namespaces!
16
16
 
17
17
  comments = comment_xml.xpath("//comments/commentList/comment").map do |comment_node|
18
- value = (comment_node.xpath('./text/r/t').last || comment_node.at_xpath('./text/r/t') || comment_node.at_xpath('./text/t')).text
18
+ comment_text_node = comment_node.xpath('./text/r/t')
19
+
20
+ value = if comment_text_node.is_a?(Array)
21
+ comment_text_node.map { |comment_text_node| comment_text_node.text }.join('')
22
+ else
23
+ comment_text_node.text
24
+ end
25
+
26
+ # value = (comment_node.xpath('./text/r/t').last || comment_node.at_xpath('./text/r/t') || comment_node.at_xpath('./text/t')).text
19
27
  id = comment_node.attributes["ref"].to_s
20
28
  [id, value]
21
29
  end.to_h
@@ -2,6 +2,7 @@ module OOXML
2
2
  class Excel
3
3
  class Sheet
4
4
  include OOXML::Helper::List
5
+ include OOXML::Util
5
6
  attr_reader :columns, :data_validations, :shared_strings
6
7
  attr_accessor :comments, :styles, :defined_names, :name
7
8
 
@@ -17,13 +18,13 @@ module OOXML
17
18
  @code_name ||= @xml.xpath('//sheetPr').attribute('codeName').try(:value)
18
19
  end
19
20
 
20
- def data_validation_for_cell(cell_ref)
21
+ def data_validation(cell_ref)
21
22
  data_validations.find { |data_validation| data_validation.sqref_range.include?(cell_ref)}
22
23
  end
23
24
 
24
-
25
25
  def column(id)
26
- columns.select { |column| column.id_range.include?(id)}
26
+ uniformed_reference = uniform_reference(id)
27
+ columns.find { |column| column.id_range.include?(uniformed_reference)}
27
28
  end
28
29
 
29
30
  def columns
@@ -42,6 +43,10 @@ module OOXML
42
43
  end
43
44
  end
44
45
 
46
+ def row(index)
47
+ rows.find { |row| row.id == index.to_s}
48
+ end
49
+
45
50
  def rows
46
51
  @rows ||= begin
47
52
  # TODO: get the value of merged cells
@@ -52,6 +57,18 @@ module OOXML
52
57
  end
53
58
  end
54
59
 
60
+ def each_row
61
+ rows.each_with_index do |row, row_index|
62
+ yield row.cells.map(&:value), row_index
63
+ end
64
+ end
65
+
66
+ def each_row_as_object
67
+ 0.upto(rows.size).each do |row_index|
68
+ yield rows[row_index]
69
+ end
70
+ end
71
+
55
72
  def font(cell_reference)
56
73
  style_id = fetch_style_style_id(cell_reference)
57
74
  if style_id.present?
@@ -69,17 +86,6 @@ module OOXML
69
86
  end
70
87
  end
71
88
 
72
- def each_row
73
- rows.each_with_index do |row, row_index|
74
- yield row.cells.map(&:value), row_index
75
- end
76
- end
77
-
78
- def each_row_as_object
79
- 0.upto(rows.size).each do |row_index|
80
- yield rows[row_index]
81
- end
82
- end
83
89
 
84
90
  def data_validations
85
91
  @data_validations ||= begin
@@ -111,6 +117,7 @@ module OOXML
111
117
  class Column
112
118
  attr_accessor :id, :width, :custom_width, :id_range, :hidden
113
119
  alias_method :hidden?, :hidden
120
+
114
121
  def initialize(**attrs)
115
122
  attrs.each { |property, value| send("#{property}=", value)}
116
123
  end
@@ -0,0 +1,16 @@
1
+ module OOXML
2
+ module Util
3
+ COLUMN_LETTERS = ('A'..'ZZZZ').to_a
4
+ def letter_equivalent(index)
5
+ COLUMN_LETTERS.fetch(index)
6
+ end
7
+
8
+ def letter_index(letter)
9
+ COLUMN_LETTERS.index { |c_letter| c_letter == letter}
10
+ end
11
+
12
+ def uniform_reference(ref)
13
+ ref.to_s[/[A-Z]/] ? letter_index(ref) + 1 : ref
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  module OOXML
2
2
  class Excel
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ooxml_excel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Mones
@@ -121,6 +121,7 @@ files:
121
121
  - lib/ooxml_excel/helper/list.rb
122
122
  - lib/ooxml_excel/sheet.rb
123
123
  - lib/ooxml_excel/styles.rb
124
+ - lib/ooxml_excel/util.rb
124
125
  - lib/ooxml_excel/version.rb
125
126
  - lib/ooxml_excel/workbook.rb
126
127
  - ooxml_excel.gemspec