workbook 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/.yardoc/checksums +18 -0
  2. data/.yardoc/object_types +3 -0
  3. data/.yardoc/objects/root.dat +0 -0
  4. data/.yardoc/proxy_types +0 -0
  5. data/Gemfile.lock +1 -1
  6. data/{readme.markdown → README.md} +36 -10
  7. data/doc/RubyXL.html +128 -0
  8. data/doc/RubyXL/Cell.html +202 -0
  9. data/doc/RubyXL/Workbook.html +447 -0
  10. data/doc/Workbook.html +130 -0
  11. data/doc/Workbook/Book.html +1484 -0
  12. data/doc/Workbook/Cell.html +1402 -0
  13. data/doc/Workbook/Format.html +654 -0
  14. data/doc/Workbook/Modules.html +117 -0
  15. data/doc/Workbook/Modules/RawObjectsStorage.html +508 -0
  16. data/doc/Workbook/Modules/TableDiffSort.html +620 -0
  17. data/doc/Workbook/Modules/TypeParser.html +1012 -0
  18. data/doc/Workbook/Readers.html +117 -0
  19. data/doc/Workbook/Readers/CsvReader.html +262 -0
  20. data/doc/Workbook/Readers/TxtReader.html +238 -0
  21. data/doc/Workbook/Readers/XlsReader.html +362 -0
  22. data/doc/Workbook/Readers/XlsShared.html +189 -0
  23. data/doc/Workbook/Readers/XlsxReader.html +295 -0
  24. data/doc/Workbook/Row.html +1939 -0
  25. data/doc/Workbook/Sheet.html +528 -0
  26. data/doc/Workbook/Table.html +883 -0
  27. data/doc/Workbook/Template.html +639 -0
  28. data/doc/Workbook/Writers.html +117 -0
  29. data/doc/Workbook/Writers/CsvTableWriter.html +175 -0
  30. data/doc/Workbook/Writers/XlsWriter.html +865 -0
  31. data/doc/_index.html +336 -0
  32. data/doc/class_list.html +53 -0
  33. data/doc/css/common.css +1 -0
  34. data/doc/css/full_list.css +57 -0
  35. data/doc/css/style.css +328 -0
  36. data/doc/file.README.html +236 -0
  37. data/doc/file_list.html +55 -0
  38. data/doc/frames.html +28 -0
  39. data/doc/index.html +236 -0
  40. data/doc/js/app.js +214 -0
  41. data/doc/js/full_list.js +173 -0
  42. data/doc/js/jquery.js +4 -0
  43. data/doc/method_list.html +972 -0
  44. data/doc/top-level-namespace.html +112 -0
  45. data/lib/workbook/book.rb +38 -29
  46. data/lib/workbook/cell.rb +25 -0
  47. data/lib/workbook/format.rb +9 -2
  48. data/lib/workbook/modules/raw_objects_storage.rb +5 -0
  49. data/lib/workbook/modules/table_diff_sort.rb +8 -6
  50. data/lib/workbook/modules/type_parser.rb +16 -3
  51. data/lib/workbook/readers/xls_reader.rb +1 -70
  52. data/lib/workbook/readers/xls_shared.rb +75 -0
  53. data/lib/workbook/row.rb +89 -17
  54. data/lib/workbook/sheet.rb +26 -0
  55. data/lib/workbook/table.rb +20 -1
  56. data/lib/workbook/template.rb +12 -11
  57. data/lib/workbook/writers/xls_writer.rb +19 -77
  58. data/test/test_modules_table_diff_sort.rb +19 -6
  59. data/test/test_row.rb +54 -0
  60. data/test/test_sheet.rb +8 -0
  61. data/test/test_table.rb +22 -3
  62. data/test/test_writers_xls_writer.rb +3 -0
  63. data/workbook.gemspec +1 -1
  64. metadata +46 -3
@@ -1,22 +1,35 @@
1
1
  module Workbook
2
2
  module Modules
3
+ # Adds type parsing capabilities to e.g. a Cell.
3
4
  module TypeParser
5
+
6
+ # Cleans a text file from all kinds of different ways of representing new lines
7
+ # @param [String] csv_raw a raw csv string
4
8
  def strip_win_chars csv_raw
5
9
  csv_raw.gsub(/(\n\r|\r\n|\r)/,"\n")
6
10
  end
7
11
 
12
+ # Return the different active string parsers
13
+ # @return [Array<Symbol>] A list of parsers
8
14
  def string_parsers
9
15
  @string_parsers ||= [:string_cleaner,:string_nil_converter,:string_integer_converter,:string_boolean_converter]
10
16
  end
11
17
 
12
- def string_parsers= arr
13
- @string_parsers = arr
18
+ # Set the list of string parsers
19
+ # @param [Array<Symbol>] parsers A list of parsers
20
+ # @return [Array<Symbol>] A list of parsers
21
+ def string_parsers= parsers
22
+ @string_parsers = parsers
14
23
  end
15
-
24
+
25
+ # Return the different active string parsers
26
+ # @return [Array<Proc>] A list of parsers as Procs
16
27
  def string_parsers_as_procs
17
28
  string_parsers.collect{|c| c.is_a?(Proc) ? c : self.send(c)}
18
29
  end
19
30
 
31
+ # Returns the parsed value (retrieved by calling #value)
32
+ # @return [Object] The parsed object, ideally a date or integer when found to be a such...
20
33
  def parse options={}
21
34
  options = {:detect_date=>false}.merge(options)
22
35
  string_parsers.push :string_optimistic_date_converter if options[:detect_date]
@@ -3,75 +3,6 @@ require 'spreadsheet'
3
3
  module Workbook
4
4
  module Readers
5
5
  module XlsReader
6
- XLS_COLORS = {:xls_color_1=>'#000000',
7
- :xls_color_2=>'#FFFFFF',
8
- :xls_color_3=>'#FF0000',
9
- :xls_color_4=>'#00FF00',
10
- :xls_color_5=>'#0000FF',
11
- :xls_color_6=>'#FFFF00',
12
- :xls_color_7=>'#FF00FF',
13
- :xls_color_8=>'#00FFFF',
14
- :xls_color_9=>'#800000',
15
- :xls_color_10=>'#008000',
16
- :xls_color_11=>'#000080',
17
- :xls_color_12=>'#808000',
18
- :xls_color_13=>'#800080',
19
- :xls_color_14=>'#008080',
20
- :xls_color_15=>'#C0C0C0',
21
- :xls_color_16=>'#808080',
22
- :xls_color_17=>'#9999FF',
23
- :xls_color_18=>'#993366',
24
- :xls_color_19=>'#FFFFCC',
25
- :xls_color_20=>'#CCFFFF',
26
- :xls_color_21=>'#660066',
27
- :xls_color_22=>'#FF8080',
28
- :xls_color_23=>'#0066CC',
29
- :xls_color_24=>'#CCCCFF',
30
- :xls_color_25=>'#000080',
31
- :xls_color_26=>'#FF00FF',
32
- :xls_color_27=>'#FFFF00',
33
- :xls_color_28=>'#00FFFF',
34
- :xls_color_29=>'#800080',
35
- :xls_color_30=>'#800000',
36
- :xls_color_31=>'#008080',
37
- :xls_color_32=>'#0000FF',
38
- :xls_color_33=>'#00CCFF',
39
- :xls_color_34=>'#CCFFFF',
40
- :xls_color_35=>'#CCFFCC',
41
- :xls_color_36=>'#FFFF99',
42
- :xls_color_37=>'#99CCFF',
43
- :xls_color_38=>'#FF99CC',
44
- :xls_color_39=>'#CC99FF',
45
- :xls_color_40=>'#FFCC99',
46
- :xls_color_41=>'#3366FF',
47
- :xls_color_42=>'#33CCCC',
48
- :xls_color_43=>'#99CC00',
49
- :xls_color_44=>'#FFCC00',
50
- :xls_color_45=>'#FF9900',
51
- :xls_color_46=>'#FF6600',
52
- :xls_color_47=>'#666699',
53
- :xls_color_48=>'#969696',
54
- :xls_color_49=>'#003366',
55
- :xls_color_50=>'#339966',
56
- :xls_color_51=>'#003300',
57
- :xls_color_52=>'#333300',
58
- :xls_color_53=>'#993300',
59
- :xls_color_54=>'#993366',
60
- :xls_color_55=>'#333399',
61
- :xls_color_56=>'#333333',
62
- :black=>'#000000',
63
- :white=>'#FFFFFF',
64
- :red=>'#FF0000',
65
- :green=>'#00FF00',
66
- :blue=>'#0000FF',
67
- :yellow=>'#FFFF00',
68
- :magenta=>'#FF00FF',
69
- :cyan=>'#00FFFF',
70
- :border=>'#FFFFFF',
71
- :text=>'#000000',
72
- :lime=>'#00f94c'}
73
-
74
-
75
6
 
76
7
  def load_xls file_obj
77
8
  begin
@@ -158,7 +89,7 @@ module Workbook
158
89
 
159
90
  private
160
91
  def xls_color_to_html_hex color_sym
161
- XLS_COLORS[color_sym] ? XLS_COLORS[color_sym] : "#000000"
92
+ Workbook::Book::XLS_COLORS[color_sym] ? Workbook::Book::XLS_COLORS[color_sym] : "#000000"
162
93
  end
163
94
 
164
95
  def ms_formatting_to_strftime ms_nr_format
@@ -0,0 +1,75 @@
1
+ module Workbook
2
+ module Readers
3
+ module XlsShared
4
+ XLS_COLORS = {:xls_color_1=>'#000000',
5
+ :xls_color_2=>'#FFFFFF',
6
+ :xls_color_3=>'#FF0000',
7
+ :xls_color_4=>'#00FF00',
8
+ :xls_color_5=>'#0000FF',
9
+ :xls_color_6=>'#FFFF00',
10
+ :xls_color_7=>'#FF00FF',
11
+ :xls_color_8=>'#00FFFF',
12
+ :xls_color_9=>'#800000',
13
+ :xls_color_10=>'#008000',
14
+ :xls_color_11=>'#000080',
15
+ :xls_color_12=>'#808000',
16
+ :xls_color_13=>'#800080',
17
+ :xls_color_14=>'#008080',
18
+ :xls_color_15=>'#C0C0C0',
19
+ :xls_color_16=>'#808080',
20
+ :xls_color_17=>'#9999FF',
21
+ :xls_color_18=>'#993366',
22
+ :xls_color_19=>'#FFFFCC',
23
+ :xls_color_20=>'#CCFFFF',
24
+ :xls_color_21=>'#660066',
25
+ :xls_color_22=>'#FF8080',
26
+ :xls_color_23=>'#0066CC',
27
+ :xls_color_24=>'#CCCCFF',
28
+ :xls_color_25=>'#000080',
29
+ :xls_color_26=>'#FF00FF',
30
+ :xls_color_27=>'#FFFF00',
31
+ :xls_color_28=>'#00FFFF',
32
+ :xls_color_29=>'#800080',
33
+ :xls_color_30=>'#800000',
34
+ :xls_color_31=>'#008080',
35
+ :xls_color_32=>'#0000FF',
36
+ :xls_color_33=>'#00CCFF',
37
+ :xls_color_34=>'#CCFFFF',
38
+ :xls_color_35=>'#CCFFCC',
39
+ :xls_color_36=>'#FFFF99',
40
+ :xls_color_37=>'#99CCFF',
41
+ :xls_color_38=>'#FF99CC',
42
+ :xls_color_39=>'#CC99FF',
43
+ :xls_color_40=>'#FFCC99',
44
+ :xls_color_41=>'#3366FF',
45
+ :xls_color_42=>'#33CCCC',
46
+ :xls_color_43=>'#99CC00',
47
+ :xls_color_44=>'#FFCC00',
48
+ :xls_color_45=>'#FF9900',
49
+ :xls_color_46=>'#FF6600',
50
+ :xls_color_47=>'#666699',
51
+ :xls_color_48=>'#969696',
52
+ :xls_color_49=>'#003366',
53
+ :xls_color_50=>'#339966',
54
+ :xls_color_51=>'#003300',
55
+ :xls_color_52=>'#333300',
56
+ :xls_color_53=>'#993300',
57
+ :xls_color_54=>'#993366',
58
+ :xls_color_55=>'#333399',
59
+ :xls_color_56=>'#333333',
60
+ :black=>'#000000',
61
+ :white=>'#FFFFFF',
62
+ :red=>'#FF0000',
63
+ :green=>'#00FF00',
64
+ :blue=>'#0000FF',
65
+ :yellow=>'#FFFF00',
66
+ :magenta=>'#FF00FF',
67
+ :cyan=>'#00FFFF',
68
+ :border=>'#FFFFFF',
69
+ :text=>'#000000',
70
+ :lime=>'#00f94c'
71
+ }
72
+ end
73
+ end
74
+ end
75
+
data/lib/workbook/row.rb CHANGED
@@ -1,15 +1,14 @@
1
1
  module Workbook
2
2
  class Row < Array
3
3
  alias_method :compare_without_header, :<=>
4
-
5
- # The placeholder attribute is used in compares (corresponds to newly created or removed lines (depending which side you're on)
6
- attr_accessor :placeholder
4
+ attr_accessor :placeholder # The placeholder attribute is used in compares (corresponds to newly created or removed lines (depending which side you're on)
7
5
  attr_accessor :format
8
6
 
9
-
10
- # @param [Workbook::Row, Array<Workbook::Cell>, Array] list of cells to initialize the row with, default is empty
11
- # @param [Workbook::Table] a row normally belongs to a table, reference it here
12
- # @param [Hash], option hash. Supprted options: parse_cells_on_batch_creation (parse cell values during row-initalization, default: false), cell_parse_options (default {}, see Workbook::Modules::TypeParser)
7
+ # Initialize a new row
8
+ #
9
+ # @param [Workbook::Row, Array<Workbook::Cell>, Array] cells list of cells to initialize the row with, default is empty
10
+ # @param [Workbook::Table] table a row normally belongs to a table, reference it here
11
+ # @param [Hash] options Supprted options: parse_cells_on_batch_creation (parse cell values during row-initalization, default: false), cell_parse_options (default {}, see Workbook::Modules::TypeParser)
13
12
  def initialize cells=[], table=nil, options={}
14
13
  options=options ? {:parse_cells_on_batch_creation=>false,:cell_parse_options=>{}}.merge(options) : {}
15
14
  cells = [] if cells==nil
@@ -25,22 +24,35 @@ module Workbook
25
24
  end
26
25
  end
27
26
 
27
+ # An internal function used in diffs
28
+ #
29
+ # @return [Boolean] returns true when this row is not an actual row, but a placeholder row to 'compare' against
28
30
  def placeholder?
29
31
  placeholder ? true : false
30
32
  end
31
33
 
34
+ # Returns the table this row belongs to
35
+ #
36
+ # @return [Workbook::Table] the table this row belongs to
32
37
  def table
33
38
  @table
34
39
  end
35
40
 
36
- # Set reference to the table this row belongs to (and adds the row to this table)
41
+ # Set reference to the table this row belongs to without adding the row to the table
42
+ #
43
+ # @param [Workbook::Table] t the table this row belongs to
44
+ def set_table(t)
45
+ @table = t
46
+ end
47
+
48
+ # Set reference to the table this row belongs to and add the row to this table
37
49
  #
38
- # @param [Workbook::Table]
50
+ # @param [Workbook::Table] t the table this row belongs to
39
51
  def table= t
40
52
  raise ArgumentError, "table should be a Workbook::Table (you passed a #{t.class})" unless t.is_a?(Workbook::Table) or t == nil
41
53
  if t
42
54
  @table = t
43
- table << self
55
+ table.push(self) #unless table.index(self) and self.placeholder?
44
56
  end
45
57
  end
46
58
 
@@ -50,25 +62,55 @@ module Workbook
50
62
  # row[1] #=> <Cell value="a">
51
63
  # row[:a] #=> <Cell value="a">
52
64
  #
53
- # @param [Fixnum, Symbol]
65
+ # @param [Fixnum, Symbol] index_or_hash
54
66
  # @return [Workbook::Cell, nil]
55
67
  def [](index_or_hash)
56
- if index_or_hash.is_a? Fixnum
57
- return to_a[index_or_hash]
58
- elsif index_or_hash.is_a? Symbol
68
+ if index_or_hash.is_a? Symbol
59
69
  rv = nil
60
70
  begin
61
71
  rv = to_hash[index_or_hash]
62
72
  rescue NoMethodError
63
73
  end
64
74
  return rv
75
+ else
76
+ if index_or_hash
77
+ return to_a[index_or_hash]
78
+ end
65
79
  end
66
80
  end
81
+
82
+ # Overrides normal Array's []=-function with support for symbols that identify a column based on the header-values
83
+ #
84
+ # @example Lookup using fixnum or header value encoded as symbol
85
+ # row[1] #=> <Cell value="a">
86
+ # row[:a] #=> <Cell value="a">
87
+ #
88
+ # @param [Fixnum, Symbol] index_or_hash
89
+ # @param [String, Fixnum, NilClass, Date, DateTime, Time, Float] value
90
+ # @return [Workbook::Cell, nil]
91
+ def []= (index_or_hash, value)
92
+ index = index_or_hash
93
+ if index_or_hash.is_a? Symbol
94
+ index = table_header_keys.index(index_or_hash)
95
+ end
67
96
 
97
+ value_celled = Workbook::Cell.new
98
+ if value.is_a? Workbook::Cell
99
+ value_celled = value
100
+ else
101
+ current_cell = self[index]
102
+ if current_cell.is_a? Workbook::Cell
103
+ value_celled = current_cell
104
+ end
105
+ value_celled.value=(value)
106
+ end
107
+ super(index,value_celled)
108
+ end
109
+
68
110
  # Returns an array of cells allows you to find cells by a given color, normally a string containing a hex
69
111
  #
70
- # @param [String] default :any colour, can be a CSS-style hex-string
71
- # @param [Hash] options. Option :hash_keys (default true) returns row as an array of symbols
112
+ # @param [String] color a CSS-style hex-string
113
+ # @param [Hash] options Option :hash_keys (default true) returns row as an array of symbols
72
114
  # @return [Array<Symbol>, Workbook::Row<Workbook::Cell>]
73
115
  def find_cells_by_background_color color=:any, options={}
74
116
  options = {:hash_keys=>true}.merge(options)
@@ -84,25 +126,45 @@ module Workbook
84
126
  table != nil and self.object_id == table.header.object_id
85
127
  end
86
128
 
129
+ # Is this the first row in the table
130
+ #
131
+ # @return [Boolean, NilClass] returns nil if it doesn't belong to a table, false when it isn't the first row of a table and true when it is.
132
+ def first?
133
+ table != nil and self.object_id == table.first.object_id
134
+ end
135
+
136
+ # Converts a row to an array of symbol representations of the row content, see also: Workbook::Cell#to_sym
87
137
  # @return [Array<Symbol>] returns row as an array of symbols
88
138
  def to_symbols
89
139
  collect{|c| c.to_sym}
90
140
  end
91
141
 
142
+ # Converts the row to an array of Workbook::Cell's
92
143
  # @return [Array<Workbook::Cell>] returns row as an array of symbols
93
144
  def to_a
94
145
  self.collect{|c| c}
95
146
  end
96
147
 
148
+ def table_header_keys
149
+ table.header.to_symbols
150
+ end
151
+
152
+ # Returns a hash representation of this row
153
+ #
154
+ # @return [Hash]
97
155
  def to_hash
98
156
  return @hash if @hash
99
- keys = table.header.to_symbols
157
+ keys = table_header_keys
100
158
  values = self
101
159
  @hash = {}
102
160
  keys.each_with_index {|k,i| @hash[k]=values[i]}
103
161
  return @hash
104
162
  end
105
163
 
164
+ # Compares one row wiht another
165
+ #
166
+ # @param [Workbook::Row] other row to compare against
167
+ # @return [Workbook::Row] a row with the diff result.
106
168
  def <=> other
107
169
  a = self.header? ? 0 : 1
108
170
  b = other.header? ? 0 : 1
@@ -110,6 +172,9 @@ module Workbook
110
172
  compare_without_header other
111
173
  end
112
174
 
175
+ # The first cell of the row is considered to be the key
176
+ #
177
+ # @return [Workbook::Cell] the key cell
113
178
  def key
114
179
  first
115
180
  end
@@ -119,5 +184,12 @@ module Workbook
119
184
  r = self.clone
120
185
  r = r.collect{|c| c unless c.nil?}.compact
121
186
  end
187
+
188
+ # clone the row with together with the cells
189
+ #
190
+ # @return [Workbook::Row] a cloned copy of self with cells
191
+ def clone
192
+ Workbook::Row.new(to_a.collect{|c| c.clone})
193
+ end
122
194
  end
123
195
  end
@@ -1,7 +1,14 @@
1
1
  module Workbook
2
2
  class Sheet < Array
3
+ # A Sheet is a container of tables
3
4
  attr_accessor :book
4
5
 
6
+ # Initialize a new sheet
7
+ #
8
+ # @param [Workbook::Table, Array<Array>] table The first table of this sheet
9
+ # @param [Workbook::Book] book The book this sheet belongs to
10
+ # @param [Hash] options are forwarded to Workbook::Table.new
11
+ # @return [Workbook::Sheet] (self)
5
12
  def initialize table=Workbook::Table.new([], self), book=nil, options={}
6
13
  if table.is_a? Workbook::Table
7
14
  push table
@@ -9,14 +16,33 @@ module Workbook
9
16
  push Workbook::Table.new(table, self, options)
10
17
  end
11
18
  self.book = book
19
+ return self
12
20
  end
13
21
 
22
+ # Returns true if the first table of this sheet contains anything
23
+ #
24
+ # @return [Boolean]
14
25
  def has_contents?
15
26
  table.has_contents?
16
27
  end
17
28
 
29
+ # Returns the first table of this sheet
30
+ #
31
+ # @return [Workbook::Table] the first table of this sheet
18
32
  def table
19
33
  first
20
34
  end
35
+
36
+ # Returns the book this sheet belongs to
37
+ #
38
+ # @return [Workbook::Book] the book this sheet belongs to
39
+ def book
40
+ if @book
41
+ return @book
42
+ else
43
+ self.book = Workbook::Book.new(self)
44
+ return @book
45
+ end
46
+ end
21
47
  end
22
48
  end
@@ -3,6 +3,7 @@ require 'workbook/writers/csv_table_writer'
3
3
 
4
4
 
5
5
  module Workbook
6
+ # A table is a container of rows and keeps track of the sheet it belongs to and which row is its header. Additionally suport for CSV writing and diffing with another table is included.
6
7
  class Table < Array
7
8
  include Workbook::Modules::TableDiffSort
8
9
  include Workbook::Writers::CsvTableWriter
@@ -10,7 +11,6 @@ module Workbook
10
11
  attr_accessor :header
11
12
 
12
13
  def initialize row_cel_values=[], sheet=nil, options={}
13
- #@rows = []
14
14
  row_cel_values = [] if row_cel_values == nil
15
15
  row_cel_values.each do |r|
16
16
  if r.is_a? Workbook::Row
@@ -57,6 +57,16 @@ module Workbook
57
57
  self
58
58
  end
59
59
 
60
+ def push(row)
61
+ super(row)
62
+ row.set_table(self)
63
+ end
64
+
65
+ def <<(row)
66
+ super(row)
67
+ row.set_table(self)
68
+ end
69
+
60
70
  def has_contents?
61
71
  self.clone.remove_empty_lines!.count != 0
62
72
  end
@@ -66,5 +76,14 @@ module Workbook
66
76
  self.collect{|r| r.object_id}.include? row.object_id
67
77
  end
68
78
 
79
+ def sheet
80
+ if @sheet
81
+ return @sheet
82
+ else
83
+ self.sheet = Workbook::Sheet.new(self)
84
+ return @sheet
85
+ end
86
+ end
87
+
69
88
  end
70
89
  end