workbook 0.3.1 → 0.4

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.
Files changed (62) hide show
  1. data/.yardoc/checksums +21 -18
  2. data/.yardoc/object_types +0 -0
  3. data/.yardoc/objects/root.dat +0 -0
  4. data/Gemfile.lock +4 -4
  5. data/README.md +8 -5
  6. data/doc/RubyXL/Cell.html +18 -19
  7. data/doc/RubyXL/Workbook.html +116 -114
  8. data/doc/RubyXL.html +3 -3
  9. data/doc/Workbook/Book.html +513 -145
  10. data/doc/Workbook/Cell.html +181 -170
  11. data/doc/Workbook/Format.html +591 -77
  12. data/doc/Workbook/Modules/RawObjectsStorage.html +39 -45
  13. data/doc/Workbook/Modules/TableDiffSort.html +225 -87
  14. data/doc/Workbook/Modules/TypeParser.html +182 -131
  15. data/doc/Workbook/Modules.html +3 -3
  16. data/doc/Workbook/Readers/CsvReader.html +101 -39
  17. data/doc/Workbook/Readers/OdsReader.html +564 -0
  18. data/doc/Workbook/Readers/TxtReader.html +12 -14
  19. data/doc/Workbook/Readers/XlsReader.html +154 -138
  20. data/doc/Workbook/Readers/XlsShared.html +71 -72
  21. data/doc/Workbook/Readers/XlsxReader.html +89 -82
  22. data/doc/Workbook/Readers.html +6 -6
  23. data/doc/Workbook/Row.html +421 -206
  24. data/doc/Workbook/Sheet.html +379 -32
  25. data/doc/Workbook/Table.html +328 -90
  26. data/doc/Workbook/Template.html +55 -60
  27. data/doc/Workbook/Writers/CsvTableWriter.html +33 -8
  28. data/doc/Workbook/Writers/HtmlWriter.html +393 -0
  29. data/doc/Workbook/Writers/XlsWriter.html +132 -92
  30. data/doc/Workbook/Writers.html +5 -5
  31. data/doc/Workbook.html +16 -4
  32. data/doc/_index.html +45 -15
  33. data/doc/class_list.html +1 -1
  34. data/doc/css/style.css +10 -0
  35. data/doc/file.README.html +53 -48
  36. data/doc/frames.html +1 -1
  37. data/doc/index.html +53 -48
  38. data/doc/method_list.html +232 -56
  39. data/doc/top-level-namespace.html +3 -3
  40. data/lib/workbook/book.rb +27 -1
  41. data/lib/workbook/format.rb +46 -7
  42. data/lib/workbook/modules/type_parser.rb +8 -1
  43. data/lib/workbook/readers/ods_reader.rb +93 -0
  44. data/lib/workbook/row.rb +7 -0
  45. data/lib/workbook/sheet.rb +10 -0
  46. data/lib/workbook/version.rb +1 -1
  47. data/lib/workbook/writers/html_writer.rb +56 -0
  48. data/test/artifacts/book_with_tabs_and_colours.ods +0 -0
  49. data/test/artifacts/complex_types.ods +0 -0
  50. data/test/artifacts/excel_different_types.ods +0 -0
  51. data/test/artifacts/simple_sheet.ods +0 -0
  52. data/test/test_book.rb +6 -0
  53. data/test/test_format.rb +39 -0
  54. data/test/test_modules_type_parser.rb +2 -0
  55. data/test/test_readers_csv_reader.rb +44 -0
  56. data/test/test_readers_ods_reader.rb +51 -0
  57. data/test/test_readers_txt_reader.rb +53 -0
  58. data/test/test_row.rb +12 -0
  59. data/test/test_sheet.rb +12 -0
  60. data/test/test_writers_html_writer.rb +37 -0
  61. data/workbook.gemspec +2 -2
  62. metadata +21 -4
@@ -6,7 +6,7 @@
6
6
  <title>
7
7
  Top Level Namespace
8
8
 
9
- &mdash; Documentation by YARD 0.8.3
9
+ &mdash; Documentation by YARD 0.8.5.2
10
10
 
11
11
  </title>
12
12
 
@@ -103,9 +103,9 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Thu Jan 17 13:15:56 2013 by
106
+ Generated on Sun May 5 14:58:04 2013 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
- 0.8.3 (ruby-1.8.7).
108
+ 0.8.5.2 (ruby-1.9.3).
109
109
  </div>
110
110
 
111
111
  </body>
data/lib/workbook/book.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  require 'workbook/writers/xls_writer'
3
+ require 'workbook/writers/html_writer'
3
4
  require 'workbook/readers/xls_reader'
4
5
  require 'workbook/readers/xls_shared'
5
6
  require 'workbook/readers/xlsx_reader'
7
+ require 'workbook/readers/ods_reader'
6
8
  require 'workbook/readers/csv_reader'
7
9
  require 'workbook/readers/txt_reader'
8
10
 
@@ -13,10 +15,12 @@ module Workbook
13
15
  class Book < Array
14
16
  include Workbook::Readers::XlsShared
15
17
  include Workbook::Writers::XlsWriter
18
+ include Workbook::Writers::HtmlWriter
16
19
  include Workbook::Readers::XlsReader
20
+ include Workbook::Readers::OdsReader
17
21
  include Workbook::Readers::XlsxReader
18
22
  include Workbook::Readers::CsvReader
19
- include Workbook::Readers::TxtReader
23
+ include Workbook::Readers::TxtReader
20
24
 
21
25
  attr_accessor :title
22
26
  attr_accessor :template
@@ -149,6 +153,28 @@ module Workbook
149
153
  return wb
150
154
  end
151
155
 
156
+ # Create an instance from the given stream or string, which should be in CSV or TXT format
157
+ #
158
+ # @param [StringIO] stringio_or_string StringIO stream or String object, with data in CSV or TXT format
159
+ # @param [Symbol] filetype (currently only :csv or :txt), indicating the format of the first parameter
160
+ # @return [Workbook::Book] A new instance
161
+ def self.read(stringio_or_string, filetype)
162
+ wb = self.new
163
+ wb.read(stringio_or_string, filetype)
164
+ wb
165
+ end
166
+
167
+ # Load the CSV data contained in the given StringIO or String object
168
+ #
169
+ # @param [StringIO] stringio_or_string StringIO stream or String object, with data in CSV format
170
+ # @param [Symbol] filetype (currently only :csv or :txt), indicating the format of the first parameter
171
+ def read(stringio_or_string, filetype)
172
+ raise ArgumentError.new("The filetype parameter should be either :csv or :txt") unless [:csv, :txt].include?(filetype)
173
+ t = stringio_or_string.respond_to?(:read) ? stringio_or_string.read : stringio_or_string.to_s
174
+ t = text_to_utf8(t)
175
+ send(:"parse_#{filetype}", t)
176
+ end
177
+
152
178
  # Create or open the existing sheet at an index value
153
179
  #
154
180
  # @param [Integer] index the index of the sheet
@@ -6,38 +6,77 @@ module Workbook
6
6
  class Format < Hash
7
7
  include Workbook::Modules::RawObjectsStorage
8
8
  alias_method :merge_hash, :merge
9
+ alias_method :merge_hash!, :merge!
9
10
 
10
- attr_accessor :name
11
+ attr_accessor :name, :parent
11
12
 
12
13
  # Initialize
13
14
  # @param [Workbook::Format, Hash] options (e.g. :background, :color
14
- def initialize options={}
15
+ def initialize options={}, name=nil
15
16
  options.each {|k,v| self[k]=v}
17
+ self.name = name
16
18
  end
17
19
 
18
20
  # Does the current format feature a background *color*? (not black or white or transparant).
19
21
  def has_background_color? color=:any
20
- if self[:background_color]
21
- return (self[:background_color].downcase==color.to_s.downcase or (!(self[:background_color]==nil or (self[:background_color].is_a? String and (self[:background_color].downcase=='#ffffff' or self[:background_color]=='#000000'))) and color==:any))
22
+ if flattened[:background_color]
23
+ return (flattened[:background_color].downcase==color.to_s.downcase or (!(flattened[:background_color]==nil or (flattened[:background_color].is_a? String and (flattened[:background_color].downcase=='#ffffff' or flattened[:background_color]=='#000000'))) and color==:any))
22
24
  else
23
25
  return false
24
26
  end
25
27
  end
26
28
 
27
29
  # Returns a string that can be used as inline cell styling (e.g. `<td style="<%=cell.format.to_css%>"><%=cell%></td>`)
28
- # @return [String] very basic CSS styling string
30
+ # @return String very basic CSS styling string
29
31
  def to_css
30
32
  css_parts = []
31
- css_parts.push("background: #{self[:background_color].to_s} #{self[:background].to_s}".strip) if self[:background] or self[:background_color]
32
- css_parts.push("color: #{self[:color].to_s}") if self[:color]
33
+ background = [flattened[:background_color].to_s,flattened[:background].to_s].join(" ").strip
34
+ css_parts.push("background: #{background}") if background and background != ""
35
+ css_parts.push("color: #{flattened[:color].to_s}") if flattened[:color]
33
36
  css_parts.join("; ")
34
37
  end
35
38
 
36
39
  # Combines the formatting options of one with another, removes as a consequence the reference to the raw object's equivalent.
37
40
  # @param [Workbook::Format] other_format
41
+ # @return [Workbook::Format] a new resulting Workbook::Format
38
42
  def merge(other_format)
39
43
  self.remove_all_raws!
40
44
  self.merge_hash(other_format)
41
45
  end
46
+
47
+ # Applies the formatting options of self with another, removes as a consequence the reference to the raw object's equivalent.
48
+ # @param [Workbook::Format] other_format
49
+ # @return [Workbook::Format] self
50
+ def merge!(other_format)
51
+ self.remove_all_raws!
52
+ self.merge_hash!(other_format)
53
+ end
54
+
55
+ # returns an array of all formats this style is inheriting from (including itself)
56
+ # @return [Array<Workbook::Format>] an array of Workbook::Formats
57
+ def formats
58
+ formats=[]
59
+ f = self
60
+ formats << f
61
+ while f.parent
62
+ formats << f.parent
63
+ f = f.parent
64
+ end
65
+ formats.reverse
66
+ end
67
+
68
+ # returns an array of all format-names this style is inheriting from (and this style)
69
+ # @return [Array<String>] an array of Workbook::Formats
70
+ def all_names
71
+ formats.collect{|a| a.name}
72
+ end
73
+
74
+ # Applies the formatting options of self with its parents until no parent can be found
75
+ # @return [Workbook::Format] new Workbook::Format that is the result of merging current style with all its parent's styles.
76
+ def flattened
77
+ ff=Workbook::Format.new()
78
+ formats.each{|a| ff.merge!(a) }
79
+ return ff
80
+ end
42
81
  end
43
82
  end
@@ -90,9 +90,16 @@ module Workbook
90
90
  rv = v
91
91
  end
92
92
  begin
93
- rv = Date.parse(v.to_i.to_s) == rv ? v : rv # disqualify is it is only based on the first number
93
+ rv = Date.parse(v.to_i.to_s) == rv ? v : rv # disqualify if it is only based on the first number
94
94
  rescue ArgumentError
95
95
  end
96
+ # try strptime with format 'mm/dd/yyyy'
97
+ if rv == v && /^\d{1,2}[\/-]\d{1,2}[\/-]\d{4}/ =~ v
98
+ begin
99
+ rv = Date.strptime(v, "%m/%d/%Y")
100
+ rescue ArgumentError
101
+ end
102
+ end
96
103
  end
97
104
  rv
98
105
  end
@@ -0,0 +1,93 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module Workbook
4
+ module Readers
5
+ module OdsReader
6
+
7
+ # reads self with and ods-type content.xml
8
+ # @param [String,File] file_obj a file or file reference
9
+ # @return [Workbook::Book] self
10
+ def load_ods file_obj
11
+ file_obj = file_obj.path if file_obj.is_a? File
12
+ content = ""
13
+ styles = ""
14
+ Zip::ZipFile.open(file_obj) do |zipfile|
15
+ zipfile.entries.each do |file|
16
+ styles = zipfile.read(file.name) if file.name == "styles.xml"
17
+
18
+ content = zipfile.read(file.name) if file.name == "content.xml"
19
+ end
20
+ end
21
+ content = Nokogiri.XML(content)
22
+ styles = Nokogiri.XML(styles)
23
+ template.add_raw content
24
+ parse_ods_style styles
25
+ parse_ods content
26
+ return self
27
+ end
28
+
29
+ def set_format_property format, property, value
30
+ value.strip!
31
+ format[property] = value if value and value != ""
32
+ end
33
+
34
+ def parse_ods_style parse_ods_style
35
+ parse_ods_style.xpath("//style:style").each do |style|
36
+ style_family = style.xpath("@style:family").to_s
37
+ if style_family == "table-cell"
38
+ format = Workbook::Format.new
39
+ format.name = style.xpath("@style:name").to_s
40
+ format.parent = self.template.formats[style.xpath("@style:parent-style-name").to_s]
41
+ set_format_property format, :border, style.xpath("style:table-cell-properties/@fo:border").to_s
42
+ set_format_property format, :vertical_align, style.xpath("style:table-cell-properties/@style:vertical-align").to_s.gsub("automatic","auto")
43
+ set_format_property format, :padding, style.xpath("style:table-cell-properties/@fo:padding").to_s.gsub("automatic","auto")
44
+ set_format_property format, :font, style.xpath("style:text-properties/style:font-name").to_s + " " + style.xpath("style:text-properties/fo:font-size").to_s + " " + style.xpath("style:text-properties/fo:font-weight").to_s
45
+ set_format_property format, :color, style.xpath("style:text-properties/@fo:color").to_s
46
+ set_format_property format, :background_color, style.xpath("style:table-cell-properties/@fo:background-color").to_s
47
+ self.template.add_format(format)
48
+ end
49
+ end
50
+ end
51
+
52
+ # updates self with and ods-type content.xml
53
+ # @param [Nokogiri::XML::Document] ods_spreadsheet nokogirified content.xml
54
+ # @return [Workbook::Book] self
55
+ def parse_ods ods_spreadsheet=template.raws[Nokogiri::XML::Document], options={}
56
+ options = {:additional_type_parsing=>false}.merge options
57
+ # styles
58
+ #puts ods_spreadsheet
59
+ parse_ods_style ods_spreadsheet
60
+
61
+ # data
62
+ ods_spreadsheet.xpath("//office:body/office:spreadsheet").each_with_index do |sheet,sheetindex|
63
+ s = self.create_or_open_sheet_at(sheetindex)
64
+ sheet.xpath("table:table").each_with_index do |table,tableindex|
65
+ t = s.create_or_open_table_at(tableindex)
66
+ table.xpath("table:table-row").each_with_index do |row,rowindex|
67
+ row = row.xpath("table:table-cell").collect do |cell|
68
+ c = Workbook::Cell.new()
69
+ cell_style_name = cell.xpath('@table:style-name').to_s
70
+ c.format = self.template.formats[cell_style_name]
71
+ valuetype = cell.xpath('@office:value-type').to_s
72
+ value = cell.xpath("text:p//text()").to_s
73
+ value = value == "" ? nil : value
74
+ case valuetype
75
+ when 'float'
76
+ value = cell.xpath("@office:value").to_s.to_f
77
+ when 'integer'
78
+ value = cell.xpath("@office:value").to_s.to_i
79
+ when 'date'
80
+ value = DateTime.parse(cell.xpath("@office:date-value").to_s)
81
+ end
82
+ c.value = value
83
+ c
84
+ end
85
+ t << Workbook::Row.new(row)
86
+ end
87
+ end
88
+ end
89
+ return self
90
+ end
91
+ end
92
+ end
93
+ end
data/lib/workbook/row.rb CHANGED
@@ -134,6 +134,13 @@ module Workbook
134
134
  table != nil and self.object_id == table.first.object_id
135
135
  end
136
136
 
137
+ # Returns true when all the cells in the row have values whose to_s value equals an empty string
138
+ #
139
+ # @return [Boolean]
140
+ def no_values?
141
+ all? {|c| c.value.to_s == ''}
142
+ end
143
+
137
144
  # Converts a row to an array of symbol representations of the row content, see also: Workbook::Cell#to_sym
138
145
  # @return [Array<Symbol>] returns row as an array of symbols
139
146
  def to_symbols
@@ -64,5 +64,15 @@ module Workbook
64
64
  s.each{|t| c << t.clone}
65
65
  return c
66
66
  end
67
+
68
+ # Create or open the existing table at an index value
69
+ #
70
+ # @param [Integer] index the index of the table
71
+ def create_or_open_table_at index
72
+ t = self[index]
73
+ t = self[index] = Workbook::Table.new if t == nil
74
+ t.sheet = self
75
+ t
76
+ end
67
77
  end
68
78
  end
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Workbook
3
- VERSION = '0.3.1'
3
+ VERSION = '0.4'
4
4
  end
@@ -0,0 +1,56 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spreadsheet'
3
+
4
+ module Workbook
5
+ module Writers
6
+ module HtmlWriter
7
+
8
+ # Generates an Spreadsheet (from the spreadsheet gem) in order to build an XlS
9
+ #
10
+ # @param [Hash] options A hash with options
11
+ # @return [Spreadsheet] A Spreadsheet object, ready for writing or more lower level operations
12
+ def to_html options={}
13
+ options = {:style_with_inline_css=>false}.merge(options)
14
+ builder = Nokogiri::HTML::Builder.new do |doc|
15
+ doc.html {
16
+ doc.body {
17
+ self.each{|sheet|
18
+ doc.h1 {
19
+ doc.text sheet.name
20
+ }
21
+ sheet.each{|table|
22
+ doc.table {
23
+ table.each{|row|
24
+ doc.tr {
25
+ row.each {|cell|
26
+ classnames = cell.format.all_names.join(" ").strip
27
+ td_options = classnames != "" ? {:class=>classnames} : {}
28
+ td_options = td_options.merge({:style=>cell.format.to_css}) if options[:style_with_inline_css] and cell.format.to_css != ""
29
+ doc.td(td_options) {
30
+ doc.text cell.value
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
36
+ }
37
+ }
38
+ }
39
+ }
40
+ end
41
+ return builder.to_html
42
+ end
43
+
44
+
45
+
46
+ # Write the current workbook to Microsoft Excel format (using the spreadsheet gem)
47
+ #
48
+ # @param [String] filename
49
+ # @param [Hash] options see #to_xls
50
+ def write_to_html filename="#{title}.html", options={}
51
+ File.open(filename, 'w') {|f| f.write(to_html(options)) }
52
+ return filename
53
+ end
54
+ end
55
+ end
56
+ end
Binary file
Binary file
data/test/test_book.rb CHANGED
@@ -61,4 +61,10 @@ class TestWorkbook < Test::Unit::TestCase
61
61
  t = w.text_to_utf8(t)
62
62
  assert_equal("a\tb\tc\td", t.split(/(\n|\r)/).first)
63
63
  end
64
+
65
+ def test_read_bad_filetype
66
+ assert_raises(ArgumentError) { Workbook::Book.read("test string here", :xls) }
67
+ assert_raises(ArgumentError) { Workbook::Book.read("test string here", :ods) }
68
+ assert_raises(ArgumentError) { Workbook::Book.read("test string here", :xlsx) }
69
+ end
64
70
  end
data/test/test_format.rb CHANGED
@@ -57,4 +57,43 @@ class TestFormat < Test::Unit::TestCase
57
57
 
58
58
  end
59
59
 
60
+ def test_parent_style
61
+ a = Workbook::Format.new({:background_color=>'#ffffff'})
62
+ b = Workbook::Format.new({:color=>'#000'})
63
+ a.parent = b
64
+ assert_equal(b, a.parent)
65
+ end
66
+ def test_parent_style_flattened_properties
67
+ a = Workbook::Format.new({:background_color=>'#ffffff'})
68
+ b = Workbook::Format.new({:color=>'#000'})
69
+ a.parent = b
70
+ assert_equal(Workbook::Format.new({:color=>'#000',:background_color=>'#ffffff'}), a.flattened)
71
+ c = Workbook::Format.new({:color=>'#f00'})
72
+ c.parent = a
73
+ assert_equal(Workbook::Format.new({:color=>'#f00',:background_color=>'#ffffff'}), c.flattened)
74
+ end
75
+ def test_formats
76
+ a = Workbook::Format.new({:background_color=>'#ffffff'})
77
+ b = Workbook::Format.new({:color=>'#000'})
78
+ c = Workbook::Format.new({:color=>'#f00'})
79
+ a.parent = b
80
+ c.parent = a
81
+ assert_equal([b,a,c], c.formats)
82
+ end
83
+ def test_parent_style_to_css
84
+ a = Workbook::Format.new({:background_color=>'#ffffff'})
85
+ b = Workbook::Format.new({:color=>'#000'})
86
+ c = Workbook::Format.new({:color=>'#f00'})
87
+ a.parent = b
88
+ c.parent = a
89
+ assert_equal("background: #ffffff; color: #f00", c.to_css)
90
+ end
91
+ def test_all_names
92
+ a = Workbook::Format.new({:background_color=>'#ffffff'}, "a")
93
+ b = Workbook::Format.new({:color=>'#000'},"b")
94
+ c = Workbook::Format.new({:color=>'#f00'},"c")
95
+ a.parent = b
96
+ c.parent = a
97
+ assert_equal(["b","a","c"],c.all_names)
98
+ end
60
99
  end
@@ -10,6 +10,8 @@ module Modules
10
10
  "2011-05-19T15_37_49 - 52349.xml"=>"2011-05-19T15_37_49 - 52349.xml",
11
11
  "20-2-2012 20:52"=>DateTime.new(2012,2,20,20,52),
12
12
  "1-11-2011"=>Date.new(2011,11,1),
13
+ "12/12/2012"=>Date.new(2012,12,12),
14
+ "12/23/1980"=>Date.new(1980,12,23), #TODO: should probably depend on locale, see: http://bugs.ruby-lang.org/issues/634#note-10
13
15
  "jA"=>"jA",
14
16
  "n"=>"n",
15
17
  "12 bomen"=>"12 bomen",
@@ -51,5 +51,49 @@ module Readers
51
51
  assert_equal(42000,w.sheet.table[3][:b].value)
52
52
  assert_equal(nil,w.sheet.table[2][:c].value)
53
53
  end
54
+ def test_class_read_string
55
+ s = File.read 'test/artifacts/simple_csv.csv'
56
+ w = Workbook::Book.read( s, :csv )
57
+ # reads
58
+ # a,b,c,d
59
+ # 1,2,3,4
60
+ # 5,3,2,1
61
+ # "asdf",123,12,2001-02-02
62
+ #
63
+ assert_equal([:a,:b,:c,:d],w.sheet.table.header.to_symbols)
64
+ assert_equal(3,w.sheet.table[2][:b].value)
65
+ assert_equal("asdf",w.sheet.table[3][:a].value)
66
+ assert_equal(Date.new(2001,2,2),w.sheet.table[3][:d].value)
67
+ end
68
+ def test_instance_read_string
69
+ w = Workbook::Book.new
70
+ s = File.read 'test/artifacts/simple_csv.csv'
71
+ w.read( s, :csv )
72
+ # reads
73
+ # a,b,c,d
74
+ # 1,2,3,4
75
+ # 5,3,2,1
76
+ # "asdf",123,12,2001-02-02
77
+ #
78
+ assert_equal([:a,:b,:c,:d],w.sheet.table.header.to_symbols)
79
+ assert_equal(3,w.sheet.table[2][:b].value)
80
+ assert_equal("asdf",w.sheet.table[3][:a].value)
81
+ assert_equal(Date.new(2001,2,2),w.sheet.table[3][:d].value)
82
+ end
83
+ def test_instance_read_stringio
84
+ w = Workbook::Book.new
85
+ sio = StringIO.new(File.read 'test/artifacts/simple_csv.csv')
86
+ w.read( sio, :csv )
87
+ # reads
88
+ # a,b,c,d
89
+ # 1,2,3,4
90
+ # 5,3,2,1
91
+ # "asdf",123,12,2001-02-02
92
+ #
93
+ assert_equal([:a,:b,:c,:d],w.sheet.table.header.to_symbols)
94
+ assert_equal(3,w.sheet.table[2][:b].value)
95
+ assert_equal("asdf",w.sheet.table[3][:a].value)
96
+ assert_equal(Date.new(2001,2,2),w.sheet.table[3][:d].value)
97
+ end
54
98
  end
55
99
  end
@@ -0,0 +1,51 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), 'helper')
3
+ module Readers
4
+ class TestXlsWriter < Test::Unit::TestCase
5
+ def test_open
6
+
7
+ w = Workbook::Book.new
8
+ w.open 'test/artifacts/book_with_tabs_and_colours.ods'
9
+ assert_equal([:a, :b, :c, :d, :e],w.sheet.table.header.to_symbols[0..4])
10
+ assert_equal(90588,w.sheet.table[2][:b].value)
11
+ end
12
+
13
+ def test_styling
14
+ w = Workbook::Book.new
15
+ w.open 'test/artifacts/book_with_tabs_and_colours.ods'
16
+ assert_equal("#ffff99",w.sheet.table[3][:c].format[:background_color])
17
+ assert_equal(true,w.sheet.table[0][:e].format.all_names.include?("Heading1"))
18
+ # TODO: column styles
19
+ # assert_equal(8.13671875,w.sheet.table.first[:b].format[:width])
20
+ # assert_equal(3.85546875,w.sheet.table.first[:a].format[:width])
21
+ # assert_equal(25.14453125,w.sheet.table.first[:c].format[:width])
22
+ end
23
+
24
+ def test_complex_types
25
+ w = Workbook::Book.new
26
+ w.open 'test/artifacts/complex_types.ods'
27
+ assert_equal(Date.new(2011,11,15), w.sheet.table[2][3].value)
28
+ assert_equal("http://murb.nl", w.sheet.table[3][2].value)
29
+ assert_equal("sadfasdfsd", w.sheet.table[4][2].value)
30
+ assert_equal(1.2, w.sheet.table[3][1].value)
31
+ end
32
+
33
+ def test_excel_standardized_open
34
+ w = Workbook::Book.new
35
+ w.open("test/artifacts/excel_different_types.ods")
36
+ # reads
37
+ # a,b,c,d
38
+ # 2012-02-22,2014-12-27,2012-11-23,2012-11-12T04:20:00+00:00
39
+ # c,222.0,,0027-12-14T05:21:00+00:00
40
+ # 2012-01-22T11:00:00+00:00,42000.0,"goh, idd",ls
41
+ #
42
+ assert_equal([:a,:b,:c, :d],w.sheet.table.header.to_symbols[0..3])
43
+ assert_equal(Date.new(2012,2,22),w.sheet.table[1][:a].value)
44
+ assert_equal("c",w.sheet.table[2][:a].value)
45
+ assert_equal(DateTime.new(2012,1,22,11),w.sheet.table[3][:a].value)
46
+ assert_equal(42000,w.sheet.table[3][:b].value)
47
+ assert_equal(nil,w.sheet.table[2][:c].value)
48
+ end
49
+
50
+ end
51
+ end
@@ -26,5 +26,58 @@ module Readers
26
26
  assert_equal(nil,w.sheet.table[2][:c].value)
27
27
  end
28
28
 
29
+ def test_excel_class_read_string
30
+ s = File.read("test/artifacts/excel_different_types.txt")
31
+ w = Workbook::Book.read(s, :txt)
32
+ # reads
33
+ # a,b,c,d
34
+ # 2012-02-22,2014-12-27,2012-11-23,2012-11-12T04:20:00+00:00
35
+ # c,222.0,,0027-12-14T05:21:00+00:00
36
+ # 2012-01-22T11:00:00+00:00,42000.0,"goh, idd",ls
37
+
38
+ assert_equal([:a,:b,:c, :d],w.sheet.table.header.to_symbols)
39
+ assert_equal(Date.new(2012,2,22),w.sheet.table[1][:a].value)
40
+ assert_equal("c",w.sheet.table[2][:a].value)
41
+ assert_equal(DateTime.new(2012,1,22,11),w.sheet.table[3][:a].value)
42
+ assert_equal(42000,w.sheet.table[3][:b].value)
43
+ assert_equal(nil,w.sheet.table[2][:c].value)
44
+ end
45
+
46
+ def test_excel_instance_read_string
47
+ s = File.read("test/artifacts/excel_different_types.txt")
48
+ w = Workbook::Book.new
49
+ w.read(s, :txt)
50
+ # reads
51
+ # a,b,c,d
52
+ # 2012-02-22,2014-12-27,2012-11-23,2012-11-12T04:20:00+00:00
53
+ # c,222.0,,0027-12-14T05:21:00+00:00
54
+ # 2012-01-22T11:00:00+00:00,42000.0,"goh, idd",ls
55
+
56
+ assert_equal([:a,:b,:c, :d],w.sheet.table.header.to_symbols)
57
+ assert_equal(Date.new(2012,2,22),w.sheet.table[1][:a].value)
58
+ assert_equal("c",w.sheet.table[2][:a].value)
59
+ assert_equal(DateTime.new(2012,1,22,11),w.sheet.table[3][:a].value)
60
+ assert_equal(42000,w.sheet.table[3][:b].value)
61
+ assert_equal(nil,w.sheet.table[2][:c].value)
62
+ end
63
+
64
+ def test_excel_instance_read_stringio
65
+ sio = StringIO.new(File.read("test/artifacts/excel_different_types.txt"))
66
+ w = Workbook::Book.new
67
+ w.read(sio, :txt)
68
+ # reads
69
+ # a,b,c,d
70
+ # 2012-02-22,2014-12-27,2012-11-23,2012-11-12T04:20:00+00:00
71
+ # c,222.0,,0027-12-14T05:21:00+00:00
72
+ # 2012-01-22T11:00:00+00:00,42000.0,"goh, idd",ls
73
+
74
+ assert_equal([:a,:b,:c, :d],w.sheet.table.header.to_symbols)
75
+ assert_equal(Date.new(2012,2,22),w.sheet.table[1][:a].value)
76
+ assert_equal("c",w.sheet.table[2][:a].value)
77
+ assert_equal(DateTime.new(2012,1,22,11),w.sheet.table[3][:a].value)
78
+ assert_equal(42000,w.sheet.table[3][:b].value)
79
+ assert_equal(nil,w.sheet.table[2][:c].value)
80
+ end
81
+
29
82
  end
30
83
  end
data/test/test_row.rb CHANGED
@@ -67,6 +67,18 @@ class TestRow < Test::Unit::TestCase
67
67
  assert_equal(r1, t.first)
68
68
  end
69
69
 
70
+ def test_no_values?
71
+ t = Workbook::Table.new
72
+ r1 = Workbook::Row.new
73
+ r1.table = t
74
+ assert_equal(true, r1.no_values?)
75
+ r1 << Workbook::Cell.new('abcd')
76
+ assert_equal(false, r1.no_values?)
77
+ r2 = Workbook::Row.new [nil, '', nil, '', '']
78
+ r2.table = t
79
+ assert_equal(true, r2.no_values?)
80
+ end
81
+
70
82
  def test_to_symbols
71
83
  r1 = Workbook::Row.new ["test", "asdf-asd", "asdf - asdf", "asdf2"]
72
84
  assert_equal([:test, :asdfasd, :asdf_asdf, :asdf2], r1.to_symbols)
data/test/test_sheet.rb CHANGED
@@ -42,4 +42,16 @@ class TestWorkbook < Test::Unit::TestCase
42
42
  assert_equal(5,s2.table[2][:a])
43
43
  end
44
44
 
45
+ def test_create_or_open_table_at
46
+ s = Workbook::Sheet.new
47
+ table0=s.create_or_open_table_at(0)
48
+ assert_equal(Workbook::Table, table0.class)
49
+ assert_equal(s, table0.sheet)
50
+ table1=s.create_or_open_table_at(1)
51
+ assert_equal(Workbook::Table, table1.class)
52
+ assert_equal(s, table1.sheet)
53
+ table1<<Workbook::Row.new([1,2,3,4])
54
+ assert_equal(false, table1 == table0)
55
+
56
+ end
45
57
  end