ooxml_excel 0.0.3.1 → 0.0.3.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: 06862da115a90c58b1290475d668f10ad6b1f768
4
- data.tar.gz: 5bb0cc5ac07ca9eae03d916cc9d73e9bce33ecf4
3
+ metadata.gz: 650ed5f65217c586c53de12b5d1eafdaadb0d02a
4
+ data.tar.gz: 72fab1922ac5014a570af54842613c33b244fcb1
5
5
  SHA512:
6
- metadata.gz: e4567ab39c4f21a8c4db08d98f251e5ed507829044edb204f535b6060fe4e37b6a688cba9f8fd2c03e1f852d0b751e06d16948a26c259c66cca2fd9cb9966c14
7
- data.tar.gz: 85283efc68d4fed5aa3a36cd796e3201744d633997f083534d26e9aba7ec410abb77e570ee7343967e9a58b25cb8542fd7a0b9438bc11664152ff2cbe9c9c068
6
+ metadata.gz: 4a62e204782cf8ae3346c504ee9b9df8afbed809c771d3f104ca8106c6be71100874a7d4b8a8ecdecf3cfb92eef8831ab30b2ebef0f320ec20e62044e4e7b87c
7
+ data.tar.gz: 756f8984d194dbf7fc63f0abb12a31e039dfff964debe8a40243b8d6f8ff854668aa6e52208f3db3dbb3007f2d41448e4dfb22112fc58a203fa0bc715cee3841
@@ -52,7 +52,7 @@ module OOXML
52
52
  # TODO: get the value of merged cells
53
53
  # merged_cells = @xml.xpath('//mergeCells/mergeCell').map { |merged_cell| merged_cell.attributes["ref"].try(:value) }
54
54
  @xml.xpath('//sheetData/row').map do |row_node|
55
- Excel::Sheet::Row.load_from_node(row_node, shared_strings)
55
+ Excel::Sheet::Row.load_from_node(row_node, shared_strings, styles)
56
56
  end
57
57
  end
58
58
  end
@@ -69,6 +69,11 @@ module OOXML
69
69
  end
70
70
  end
71
71
 
72
+ # def styles(cell_reference)
73
+ # style_id = fetch_style_style_id(cell_reference)
74
+ # style = @styles.by_id(style_id.to_i) if style_id.present?
75
+ # end
76
+
72
77
  def font(cell_reference)
73
78
  style_id = fetch_style_style_id(cell_reference)
74
79
  if style_id.present?
@@ -154,10 +159,10 @@ module OOXML
154
159
  (cell.present?) ? cell : BlankCell.new(id)
155
160
  end
156
161
 
157
- def self.load_from_node(row_node, shared_strings)
162
+ def self.load_from_node(row_node, shared_strings, styles)
158
163
  new(id: row_node.attributes["r"].try(:value),
159
164
  spans: row_node.attributes["spans"].try(:value),
160
- cells: row_node.xpath('c').map { |cell_node| Row::Cell.load_from_node(cell_node, shared_strings) } )
165
+ cells: row_node.xpath('c').map { |cell_node| Row::Cell.load_from_node(cell_node, shared_strings, styles) } )
161
166
  end
162
167
  end
163
168
  end
@@ -168,7 +173,6 @@ module OOXML
168
173
  class Excel
169
174
  class Sheet
170
175
  class Row
171
-
172
176
  class BlankCell
173
177
  attr_reader :id
174
178
 
@@ -181,24 +185,65 @@ module OOXML
181
185
  end
182
186
 
183
187
  class Cell
184
- attr_accessor :id, :t, :s, :v, :shared_strings
188
+ attr_accessor :id, :t, :s, :v, :shared_strings, :styles
185
189
  # t = type
186
190
  # v = value
187
- # s = ??
191
+ # s = style
188
192
  def initialize(**attrs)
189
193
  attrs.each { |property, value| send("#{property}=", value)}
190
194
  end
191
195
 
196
+ def type
197
+ if t == 's'
198
+ "string"
199
+ elsif t == 'd'
200
+ "date"
201
+ elsif t == 'n'
202
+ "number"
203
+ else
204
+ # TODO: git all types
205
+ "string"
206
+ end
207
+ end
208
+
209
+ def style
210
+ @style ||= begin
211
+ if s.present?
212
+ style = styles.by_id(s.to_i)
213
+ end
214
+ end
215
+ end
216
+
217
+ def number_format
218
+ if (style.present?)
219
+ nf = style[:number_format]
220
+ (nf.present?) ? nf.gsub("\\", "") : nil
221
+ end
222
+ end
223
+
224
+ def font
225
+ (style.present?) ? style[:font] : nil
226
+ end
227
+
228
+ def fill
229
+ (style.present?) ? style[:fill]: nil
230
+ end
231
+
192
232
  def value
193
- (v.present?) ? shared_strings[v.to_i] : nil
233
+ if type == "number"
234
+ v
235
+ else
236
+ (v.present?) ? shared_strings[v.to_i] : nil
237
+ end
194
238
  end
195
239
 
196
- def self.load_from_node(cell_node, shared_strings)
240
+ def self.load_from_node(cell_node, shared_strings, styles)
197
241
  new(id: cell_node.attributes["r"].try(:value),
198
242
  t: cell_node.attributes["t"].try(:value),
199
243
  s: cell_node.attributes["s"].try(:value),
200
244
  v: cell_node.at('v').try(:text),
201
- shared_strings: shared_strings )
245
+ shared_strings: shared_strings,
246
+ styles: styles )
202
247
  end
203
248
  end
204
249
  end
@@ -1,7 +1,7 @@
1
1
  module OOXML
2
2
  class Excel
3
3
  class Styles
4
- attr_accessor :fonts, :fills, :cell_style_xfs
4
+ attr_accessor :fonts, :fills, :number_formats, :cell_style_xfs
5
5
  def initialize(**attrs)
6
6
  attrs.each { |property, value| send("#{property}=", value)}
7
7
  end
@@ -10,7 +10,8 @@ module OOXML
10
10
  cell_style = cell_style_xfs.fetch(id)
11
11
  {
12
12
  font: fonts_by_index(cell_style.font_id),
13
- fill: fills_by_index(cell_style.fill_id)
13
+ fill: fills_by_index(cell_style.fill_id),
14
+ number_format: number_formats_by_index(cell_style.number_formatting_id),
14
15
  }
15
16
  end
16
17
 
@@ -22,11 +23,15 @@ module OOXML
22
23
  @fills[fill_index]
23
24
  end
24
25
 
26
+ def number_formats_by_index(number_format_index)
27
+ @number_formats.find { |number_format| number_format.id == number_format_index.to_s}.try(:code)
28
+ end
29
+
25
30
  def self.load_from_stream(xml_stream)
26
31
  style_doc = Nokogiri.XML(xml_stream).remove_namespaces!
27
32
  fonts = style_doc.xpath('//fonts/font')
28
33
  fills = style_doc.xpath('//fills/fill')
29
-
34
+ number_formats = style_doc.xpath('//numFmts/numFmt')
30
35
  # This element contains the master formatting records (xf) which
31
36
  # define the formatting applied to cells in this workbook.
32
37
  # link: https://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.cellformats(v=office.14).aspx
@@ -35,6 +40,7 @@ module OOXML
35
40
  self.new(
36
41
  fonts: fonts.map { |font_node| Excel::Styles::Font.load_from_node(font_node)},
37
42
  fills: fills.map { |fill_node| Excel::Styles::Fill.load_from_node(fill_node)},
43
+ number_formats: number_formats.map { |num_fmt_node| Excel::Styles::NumFmt.load_from_node(num_fmt_node) },
38
44
  cell_style_xfs: cell_style_xfs.map { |cell_style_xfs_node| Excel::Styles::CellStyleXfs.load_from_node(cell_style_xfs_node)}
39
45
  )
40
46
  end
@@ -68,7 +74,21 @@ module OOXML
68
74
  end
69
75
  end
70
76
  end
71
-
77
+ module OOXML
78
+ class Excel
79
+ class Styles
80
+ class NumFmt
81
+ attr_accessor :id, :code
82
+ def self.load_from_node(num_fmt_node)
83
+ new_format = self.new.tap do |number_format|
84
+ number_format.id = num_fmt_node.attributes["numFmtId"].try(:value)
85
+ number_format.code = num_fmt_node.attributes["formatCode"].try(:value)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
72
92
  # <xf numFmtId="0" borderId="0" fillId="0" fontId="0" applyAlignment="1" applyFont="1" xfId="0"/>
73
93
  module OOXML
74
94
  class Excel
@@ -1,5 +1,5 @@
1
1
  module OOXML
2
2
  class Excel
3
- VERSION = "0.0.3.1"
3
+ VERSION = "0.0.3.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ooxml_excel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3.1
4
+ version: 0.0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Mones
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-29 00:00:00.000000000 Z
11
+ date: 2016-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport