workbook 0.4.12 → 0.4.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c05c947e639810533b5d77b7f66cca14bf2880b3
4
- data.tar.gz: 623d1d9ca5e9bceac0cb0a18bec19d79a869a193
3
+ metadata.gz: 6f84a8f4041814e019f83cb05d05de9f91ad003d
4
+ data.tar.gz: 385307619e4061a0c44966bf73665b612b203ae3
5
5
  SHA512:
6
- metadata.gz: e972dc3fbcc4d1c29860b5966c4c3d54407b6a24344450279410ee029f163555c00d8b1cec240d224c5d47ffc4d8b4f0a96e23f2b13d2cb0a5a575cb8631380e
7
- data.tar.gz: 74538baa3d9d1ce6644a4a35e2aab5aed98b213abd4d6cc89392fcf29a34a77524f9dbdc75d9156377965a39056e06cfc36a5e3738b2320a8a796b4ad596e11e
6
+ metadata.gz: 4e3403db816bde3fa82f590081a0d29064d6d797ee8d6eea27419263e257f19c4c12af542cbfc7dbc453252138c43e29462d7cdb21f7de04d15708ee0e64ce6f
7
+ data.tar.gz: 896a09df2b1ed2e26562c70116caa0158d6e4519e034e6034581edd51dd9400523727135fd0c660089ca328d56fe2c7b1cf2c46ad33866b9504dccc52dad8994
data/.travis.yml CHANGED
@@ -1,6 +1,9 @@
1
+ sudo: false
1
2
  language: ruby
2
3
  rvm:
3
4
  - 2.0.0
4
5
  - 2.1.0
5
6
  - 2.2.0
6
- # - jruby-19mode
7
+ - 2.3.0
8
+ before_install:
9
+ - gem install bundler
data/lib/workbook/book.rb CHANGED
@@ -115,12 +115,12 @@ module Workbook
115
115
  # @param [String] filename a string with a reference to the file to be opened
116
116
  # @param [String] extension an optional string enforcing a certain parser (based on the file extension, e.g. 'txt', 'csv' or 'xls')
117
117
  # @return [Workbook::Book] A new instance, based on the filename
118
- def open filename, extension=nil
118
+ def open filename, extension=nil, options={}
119
119
  extension = file_extension(filename) unless extension
120
120
  if ['txt','csv','xml'].include?(extension)
121
- open_text filename, extension
121
+ open_text filename, extension, options
122
122
  else
123
- open_binary filename, extension
123
+ open_binary filename, extension, options
124
124
  end
125
125
  end
126
126
 
@@ -129,22 +129,22 @@ module Workbook
129
129
  # @param [String] filename a string with a reference to the file to be opened
130
130
  # @param [String] extension an optional string enforcing a certain parser (based on the file extension, e.g. 'txt', 'csv' or 'xls')
131
131
  # @return [Workbook::Book] A new instance, based on the filename
132
- def open_binary filename, extension=nil
132
+ def open_binary filename, extension=nil, options={}
133
133
  extension = file_extension(filename) unless extension
134
134
  f = File.open(filename,'rb')
135
- send("load_#{extension}".to_sym,f)
135
+ send("load_#{extension}".to_sym, f, options)
136
136
  end
137
137
 
138
138
  # Open the file in non-binary, read-only mode, read it and parse it to UTF-8
139
139
  #
140
140
  # @param [String] filename a string with a reference to the file to be opened
141
141
  # @param [String] extension an optional string enforcing a certain parser (based on the file extension, e.g. 'txt', 'csv' or 'xls')
142
- def open_text filename, extension=nil
142
+ def open_text filename, extension=nil, options={}
143
143
  extension = file_extension(filename) unless extension
144
144
  f = File.open(filename,'r')
145
145
  t = f.read
146
146
  t = text_to_utf8(t)
147
- send("load_#{extension}".to_sym,t)
147
+ send("load_#{extension}".to_sym, t, options)
148
148
  end
149
149
 
150
150
  # Writes the book to a file. Filetype is based on the extension, but can be overridden
@@ -190,11 +190,11 @@ module Workbook
190
190
  #
191
191
  # @param [StringIO] stringio_or_string StringIO stream or String object, with data in CSV format
192
192
  # @param [Symbol] filetype (currently only :csv or :txt), indicating the format of the first parameter
193
- def read(stringio_or_string, filetype)
193
+ def read(stringio_or_string, filetype, options={})
194
194
  raise ArgumentError.new("The filetype parameter should be either :csv or :txt") unless [:csv, :txt].include?(filetype)
195
195
  t = stringio_or_string.respond_to?(:read) ? stringio_or_string.read : stringio_or_string.to_s
196
196
  t = text_to_utf8(t)
197
- send(:"parse_#{filetype}", t)
197
+ send(:"parse_#{filetype}", t, options)
198
198
  end
199
199
 
200
200
  # Create or open the existing sheet at an index value
@@ -224,9 +224,9 @@ module Workbook
224
224
  # @param [StringIO] stringio_or_string StringIO stream or String object, with data in CSV or TXT format
225
225
  # @param [Symbol] filetype (currently only :csv or :txt), indicating the format of the first parameter
226
226
  # @return [Workbook::Book] A new instance
227
- def read(stringio_or_string, filetype)
227
+ def read stringio_or_string, filetype, options={}
228
228
  wb = self.new
229
- wb.read(stringio_or_string, filetype)
229
+ wb.read(stringio_or_string, filetype, options)
230
230
  wb
231
231
  end
232
232
 
@@ -3,9 +3,9 @@
3
3
  module Workbook
4
4
  module Readers
5
5
  module CsvReader
6
- def load_csv text
6
+ def load_csv text, options={}
7
7
  csv = text
8
- parse_csv csv
8
+ parse_csv csv, options
9
9
  end
10
10
 
11
11
  def csv_lib
@@ -17,19 +17,22 @@ module Workbook
17
17
  end
18
18
  end
19
19
 
20
- def parse_csv csv_raw
20
+ def parse_csv csv_raw, options={}
21
21
  custom_date_converter = Workbook::Cell.new.string_optimistic_date_converter
22
- converters = [:float,:integer,:date,:date_time,custom_date_converter]
22
+ options = {
23
+ converters: [:float,:integer,:date,:date_time,custom_date_converter]
24
+ }.merge(options)
25
+
23
26
  csv=nil
24
27
  #begin
25
- csv = csv_lib.parse(csv_raw,{:converters=>converters})
28
+ csv = csv_lib.parse(csv_raw,options)
26
29
 
27
30
  #rescue
28
31
  # we're going to have another shot at it...
29
32
  #end
30
33
 
31
34
  if csv==nil or csv[0].count == 1
32
- csv_excel = csv_lib.parse(csv_raw,{:converters=>converters,:col_sep=>';'})
35
+ csv_excel = csv_lib.parse(csv_raw,options.merge({:col_sep=>';'}))
33
36
  csv = csv_excel if csv_excel[0].count > 1
34
37
  end
35
38
 
@@ -6,7 +6,7 @@ module Workbook
6
6
  # reads self with and ods-type content.xml
7
7
  # @param [String,File] file_obj a file or file reference
8
8
  # @return [Workbook::Book] self
9
- def load_ods file_obj
9
+ def load_ods file_obj, options={}
10
10
  file_obj = file_obj.path if file_obj.is_a? File
11
11
  content = ""
12
12
  styles = ""
@@ -20,7 +20,7 @@ module Workbook
20
20
  styles = Nokogiri.XML(styles)
21
21
  template.add_raw content
22
22
  parse_ods_style styles
23
- parse_ods content
23
+ parse_ods content, options
24
24
  return self
25
25
  end
26
26
 
@@ -2,12 +2,12 @@
2
2
  module Workbook
3
3
  module Readers
4
4
  module TxtReader
5
- def load_txt text
5
+ def load_txt text, options={}
6
6
  csv = text
7
- parse_txt csv
7
+ parse_txt csv, options
8
8
  end
9
9
 
10
- def parse_txt csv_raw
10
+ def parse_txt csv_raw, options={}
11
11
  csv = []
12
12
  csv_raw.split("\n").each {|l| csv << csv_lib.parse_line(l,{:col_sep=>"\t"});nil}
13
13
  self[0]=Workbook::Sheet.new(csv,self,{:parse_cells_on_batch_creation=>true, :cell_parse_options=>{:detect_date=>true}}) unless sheet.has_contents?
@@ -8,11 +8,11 @@ module Workbook
8
8
  module XlsReader
9
9
  include Workbook::Readers::XlsShared
10
10
 
11
- def load_xls file_obj
11
+ def load_xls file_obj, options
12
12
  begin
13
13
  sp = Spreadsheet.open(file_obj, 'rb')
14
14
  template.add_raw sp
15
- parse_xls sp
15
+ parse_xls sp, options
16
16
  rescue Ole::Storage::FormatError => e
17
17
  begin
18
18
  # Assuming it is a tab separated txt inside .xls
@@ -11,10 +11,10 @@ module Workbook
11
11
  # Load method for .xlsm files, an office open file format, hence compatible with .xlsx (it emphasizes that it contains macros)
12
12
  #
13
13
  # @param [String, File] file_obj a string with a reference to the file to be written to
14
- def load_xlsm file_obj
15
- self.load_xlsx file_obj
14
+ def load_xlsm file_obj, options={}
15
+ self.load_xlsx file_obj, options
16
16
  end
17
- def load_xlsx file_obj
17
+ def load_xlsx file_obj, options={}
18
18
  file_obj = file_obj.path if file_obj.is_a? File
19
19
  # file_obj = file_obj.match(/^\/(.*)/) ? file_obj : "./#{file_obj}"
20
20
  # p "opening #{file_obj}"
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Workbook
3
- VERSION = '0.4.12'
3
+ VERSION = '0.4.13'
4
4
  end
@@ -55,7 +55,7 @@ module Workbook
55
55
  if header
56
56
  doc.tr do
57
57
  header.each do |cell|
58
- th_options = build_cell_options cell, options.merge(classnames: [cell.to_sym])
58
+ th_options = build_cell_options cell, options.merge(classnames: [cell.to_sym], data: {key: cell.to_sym})
59
59
  unless cell.value.class == Workbook::NilValue
60
60
  doc.th(th_options) do
61
61
  doc.text cell.value
@@ -85,12 +85,17 @@ module Workbook
85
85
  end
86
86
  return builder.doc.to_xhtml
87
87
  end
88
- private
88
+
89
89
  def build_cell_options cell, options={}
90
90
  classnames = cell.format.all_names
91
91
  classnames = classnames + options[:classnames] if options[:classnames]
92
92
  classnames = classnames.join(" ").strip
93
93
  td_options = classnames != "" ? {:class=>classnames} : {}
94
+ if options[:data]
95
+ options[:data].each do |key, value|
96
+ td_options = td_options.merge({("data-#{key}".to_sym) => value})
97
+ end
98
+ end
94
99
  td_options = td_options.merge({:style=>cell.format.to_css}) if options[:style_with_inline_css] and cell.format.to_css != ""
95
100
  td_options = td_options.merge({:colspan=>cell.colspan}) if cell.colspan
96
101
  td_options = td_options.merge({:rowspan=>cell.rowspan}) if cell.rowspan
Binary file
@@ -95,5 +95,9 @@ module Readers
95
95
  assert_equal("asdf",w.sheet.table[3][:a].value)
96
96
  assert_equal(Date.new(2001,2,2),w.sheet.table[3][:d].value)
97
97
  end
98
+ def test_from_string
99
+ w = Workbook::Book.read("2013-03-19,JV,211,032,1,DBG A,,13,0147,\n", :csv, {converters: []})
100
+ assert_equal("2013-03-19,JV,211,032,1,DBG A,,13,0147,\n",w.sheet.table.to_csv)
101
+ end
98
102
  end
99
103
  end
@@ -65,5 +65,10 @@ module Readers
65
65
  assert_equal(["Diff_10", "Diff_9", "Diff_8", "Diff_7", "Diff_6", "Diff_5", "Diff_4", "Diff_3", "Diff_2", "Diff_1"], b.collect{|a| a.name})
66
66
  assert_equal([10, 9, 8, 7, 6, 5, 4, 3, 2, 1], b.collect{|a| a.table[1][0].value})
67
67
  end
68
+ def test_float_problem
69
+ w = Workbook::Book.new
70
+ w.open File.join(File.dirname(__FILE__), 'artifacts/floats_problem.xls')
71
+ # puts w.first.first.to_csv
72
+ end
68
73
  end
69
74
  end
@@ -14,7 +14,7 @@ module Writers
14
14
  assert_equal(false, match)
15
15
  match = html.match(/<td>1<\/td>/) ? true : false
16
16
  assert_equal(true, match)
17
- match = html.match(/<th class=\"a\">a<\/th>/) ? true : false
17
+ match = html.match(/<th class=\"a\" data-key=\"a\">a<\/th>/) ? true : false
18
18
  assert_equal(true, match)
19
19
  end
20
20
  def test_to_html_format_names
@@ -24,11 +24,16 @@ module Writers
24
24
  c = b[0][0][1][0]
25
25
  c.format.name="testname"
26
26
  html = b.to_html
27
- match = html.match(/<th class=\"testname a\">a<\/th>/) ? true : false
27
+ match = html.match(/<th class=\"testname a\" data-key=\"a\">a<\/th>/) ? true : false
28
28
  assert_equal(true, match)
29
29
  match = html.match(/<td class=\"testname\">1<\/td>/) ? true : false
30
30
  assert_equal(true, match)
31
31
  end
32
+ def test_build_cell_options
33
+ b = Workbook::Book.new([['a','b'],[1,2],[3,4]])
34
+ result = b.sheet.table.build_cell_options(b.sheet.table.first.first,{data: {a:"a"}})
35
+ assert("a",result["data-a"])
36
+ end
32
37
  def test_to_html_css
33
38
  b = Workbook::Book.new([['a','b'],[1,2],[3,4]])
34
39
  c = b[0][0][0][0]
@@ -36,13 +41,14 @@ module Writers
36
41
  c = b[0][0][1][0]
37
42
  c.format[:background]="#ff0"
38
43
  html = b.to_html
39
- match = html.match(/<th class=\"a\">a<\/th>/) ? true : false
44
+ match = html.match(/<th class=\"a\" data-key=\"a\">a<\/th>/) ? true : false
45
+ assert_equal(true, match)
40
46
  match = html.match(/<td>1<\/td>/) ? true : false
41
47
  assert_equal(true, match)
42
48
  html = b.to_html({:style_with_inline_css=>true})
43
- match = html.match(/<th style="background: #f00">a<\/th>/) ? true : false
49
+ match = html.match(/<th class=\"a\" data-key=\"a\" style=\"background: #f00\">a<\/th>/) ? true : false
50
+ assert_equal(true, match)
44
51
  match = html.match(/<td style="background: #ff0">1<\/td>/) ? true : false
45
-
46
52
  assert_equal(true, match)
47
53
  end
48
54
  def test_sheet_and_table_names
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: workbook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.12
4
+ version: 0.4.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maarten Brouwers
@@ -238,7 +238,6 @@ files:
238
238
  - lib/workbook/writers/xls_writer.rb
239
239
  - lib/workbook/writers/xlsx_writer.rb
240
240
  - rbeautify.rb
241
- - test/artifacts/.~lock.complex_types.xls#
242
241
  - test/artifacts/bigtable.xls
243
242
  - test/artifacts/bigtable.xlsx
244
243
  - test/artifacts/book_with_colspans.ods
@@ -253,6 +252,7 @@ files:
253
252
  - test/artifacts/excel_different_types.xls
254
253
  - test/artifacts/excel_different_types.xlsx
255
254
  - test/artifacts/failing_import1.xls
255
+ - test/artifacts/floats_problem.xls
256
256
  - test/artifacts/heavy.xlsx
257
257
  - test/artifacts/native_xlsx.xlsx
258
258
  - test/artifacts/sheet_with_combined_cells.ods
@@ -310,13 +310,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
310
310
  version: '0'
311
311
  requirements: []
312
312
  rubyforge_project: workbook
313
- rubygems_version: 2.4.5
313
+ rubygems_version: 2.5.1
314
314
  signing_key:
315
315
  specification_version: 4
316
316
  summary: Workbook is a datastructure to contain books of tables (an anlogy used in
317
317
  e.g. Excel)
318
318
  test_files:
319
- - test/artifacts/.~lock.complex_types.xls#
320
319
  - test/artifacts/bigtable.xls
321
320
  - test/artifacts/bigtable.xlsx
322
321
  - test/artifacts/book_with_colspans.ods
@@ -331,6 +330,7 @@ test_files:
331
330
  - test/artifacts/excel_different_types.xls
332
331
  - test/artifacts/excel_different_types.xlsx
333
332
  - test/artifacts/failing_import1.xls
333
+ - test/artifacts/floats_problem.xls
334
334
  - test/artifacts/heavy.xlsx
335
335
  - test/artifacts/native_xlsx.xlsx
336
336
  - test/artifacts/sheet_with_combined_cells.ods
@@ -1 +0,0 @@
1
- ,murb,murb-top.local,03.02.2016 16:38,file:///Users/murb/Library/Application%20Support/LibreOffice/4;