workbook 0.8.1 → 0.9.0
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 +4 -4
- data/.codeclimate.yml +21 -0
- data/.gitignore +4 -1
- data/.ruby-version +1 -1
- data/.travis.yml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile +2 -2
- data/README.md +9 -7
- data/Rakefile +6 -6
- data/json_test.json +1 -0
- data/lib/workbook/book.rb +73 -62
- data/lib/workbook/cell.rb +58 -13
- data/lib/workbook/column.rb +31 -28
- data/lib/workbook/format.rb +23 -24
- data/lib/workbook/generatetypes.rb +4 -4
- data/lib/workbook/modules/cache.rb +6 -7
- data/lib/workbook/modules/cell.rb +77 -100
- data/lib/workbook/modules/diff_sort.rb +92 -83
- data/lib/workbook/modules/raw_objects_storage.rb +6 -8
- data/lib/workbook/modules/type_parser.rb +30 -22
- data/lib/workbook/nil_value.rb +4 -9
- data/lib/workbook/readers/csv_reader.rb +7 -10
- data/lib/workbook/readers/ods_reader.rb +51 -50
- data/lib/workbook/readers/txt_reader.rb +6 -8
- data/lib/workbook/readers/xls_reader.rb +21 -33
- data/lib/workbook/readers/xls_shared.rb +106 -117
- data/lib/workbook/readers/xlsx_reader.rb +45 -46
- data/lib/workbook/row.rb +99 -84
- data/lib/workbook/sheet.rb +47 -38
- data/lib/workbook/table.rb +96 -72
- data/lib/workbook/template.rb +12 -15
- data/lib/workbook/types/false.rb +0 -1
- data/lib/workbook/types/nil.rb +0 -1
- data/lib/workbook/types/nil_class.rb +1 -1
- data/lib/workbook/types/numeric.rb +1 -1
- data/lib/workbook/types/string.rb +1 -1
- data/lib/workbook/types/time.rb +1 -1
- data/lib/workbook/types/true.rb +0 -1
- data/lib/workbook/types/true_class.rb +1 -1
- data/lib/workbook/version.rb +2 -3
- data/lib/workbook/writers/csv_table_writer.rb +10 -13
- data/lib/workbook/writers/html_writer.rb +34 -38
- data/lib/workbook/writers/json_table_writer.rb +8 -11
- data/lib/workbook/writers/xls_writer.rb +30 -36
- data/lib/workbook/writers/xlsx_writer.rb +45 -29
- data/lib/workbook.rb +16 -15
- data/test/artifacts/currency_test.ods +0 -0
- data/test/helper.rb +6 -5
- data/test/test_book.rb +41 -38
- data/test/test_column.rb +26 -24
- data/test/test_format.rb +51 -55
- data/test/test_functional.rb +7 -8
- data/test/test_modules_cache.rb +18 -17
- data/test/test_modules_cell.rb +55 -46
- data/test/test_modules_table_diff_sort.rb +55 -64
- data/test/test_modules_type_parser.rb +61 -31
- data/test/test_readers_csv_reader.rb +48 -42
- data/test/test_readers_ods_reader.rb +36 -31
- data/test/test_readers_txt_reader.rb +21 -23
- data/test/test_readers_xls_reader.rb +20 -23
- data/test/test_readers_xls_shared.rb +2 -3
- data/test/test_readers_xlsx_reader.rb +44 -37
- data/test/test_row.rb +105 -109
- data/test/test_sheet.rb +35 -41
- data/test/test_table.rb +82 -60
- data/test/test_template.rb +16 -15
- data/test/test_types_date.rb +4 -6
- data/test/test_writers_csv_writer.rb +24 -0
- data/test/test_writers_html_writer.rb +42 -41
- data/test/test_writers_json_writer.rb +16 -9
- data/test/test_writers_xls_writer.rb +50 -35
- data/test/test_writers_xlsx_writer.rb +62 -34
- data/workbook.gemspec +25 -27
- metadata +96 -42
data/lib/workbook/sheet.rb
CHANGED
@@ -1,25 +1,25 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# -*- encoding : utf-8 -*-
|
4
2
|
# frozen_string_literal: true
|
3
|
+
|
5
4
|
module Workbook
|
6
5
|
# A Sheet is a container of tables
|
7
|
-
class Sheet
|
8
|
-
|
6
|
+
class Sheet
|
7
|
+
include Enumerable
|
9
8
|
# Initialize a new sheet
|
10
9
|
#
|
11
10
|
# @param [Workbook::Table, Array<Array>] table The first table of this sheet
|
12
11
|
# @param [Workbook::Book] book The book this sheet belongs to
|
13
12
|
# @param [Hash] options are forwarded to Workbook::Table.new
|
14
13
|
# @return [Workbook::Sheet] (self)
|
15
|
-
def initialize table=Workbook::Table.new([], self), book=nil, options={}
|
16
|
-
|
17
|
-
|
14
|
+
def initialize table = Workbook::Table.new([], self), book = nil, options = {}
|
15
|
+
@tables = []
|
16
|
+
|
17
|
+
if table.is_a?(Workbook::Table)
|
18
|
+
@tables.push table
|
18
19
|
else
|
19
|
-
push
|
20
|
+
@tables.push(Workbook::Table.new(table, self, options))
|
20
21
|
end
|
21
22
|
self.book = book
|
22
|
-
return self
|
23
23
|
end
|
24
24
|
|
25
25
|
# Returns true if the first table of this sheet contains anything
|
@@ -33,79 +33,88 @@ module Workbook
|
|
33
33
|
#
|
34
34
|
# @return [Workbook::Table] the first table of this sheet
|
35
35
|
def table
|
36
|
-
first
|
36
|
+
@tables.first
|
37
37
|
end
|
38
38
|
|
39
39
|
# Returns the name of this sheet
|
40
40
|
#
|
41
41
|
# @return [String] the name, defaulting to sheet#index when none is set
|
42
42
|
def name
|
43
|
-
@name ||= "Sheet #{book.index(self)+1}"
|
43
|
+
@name ||= "Sheet #{book.index(self) + 1}"
|
44
44
|
end
|
45
45
|
|
46
46
|
# Set the name of this sheet
|
47
47
|
#
|
48
48
|
# @param [String] the new name of the sheet
|
49
49
|
# @return [String] the given name
|
50
|
-
|
51
|
-
@name = name
|
52
|
-
end
|
50
|
+
attr_writer :name
|
53
51
|
|
54
52
|
# Set the first table of this sheet with a table or array of cells/values
|
55
53
|
# @param [Workbook::Table, Array<Array>] table The new first table of this sheet
|
56
54
|
# @param [Hash] options are forwarded to Workbook::Table.new
|
57
55
|
# @return [Workbook::Table] the first table of this sheet
|
58
|
-
def table= table, options={}
|
59
|
-
if table.is_a? Workbook::Table
|
60
|
-
|
56
|
+
def table= table, options = {}
|
57
|
+
@tables[0] = if table.is_a? Workbook::Table
|
58
|
+
table
|
61
59
|
else
|
62
|
-
|
60
|
+
Workbook::Table.new(table, self, options)
|
63
61
|
end
|
64
|
-
return table
|
65
62
|
end
|
66
63
|
|
67
|
-
# Returns the
|
64
|
+
# Returns the Book this Sheet belongs to
|
68
65
|
#
|
69
66
|
# @return [Workbook::Book] the book this sheet belongs to
|
70
67
|
def book
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
68
|
+
@book || (self.book = Workbook::Book.new(self))
|
69
|
+
end
|
70
|
+
|
71
|
+
# Returns a existing or new Table at the specified index
|
72
|
+
#
|
73
|
+
# @return [Workbook::Table]
|
74
|
+
def [] index
|
75
|
+
@tables[index] ||= Workbook::Table.new([], self)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Inserts a new Table at the specified index
|
79
|
+
#
|
80
|
+
# @param [Integer] index of the table
|
81
|
+
# @param [Workbook::Table, Array<Array>] table The new first table of this sheet
|
82
|
+
#
|
83
|
+
# @return [Workbook::Table]
|
84
|
+
def []= index, value
|
85
|
+
table_to_insert = value.is_a?(Workbook::Table) ? value : Workbook::Table.new
|
86
|
+
table_to_insert.sheet = self
|
87
|
+
@tables[index] = table_to_insert
|
77
88
|
end
|
78
89
|
|
79
|
-
|
80
|
-
|
90
|
+
# Returns an enumerator
|
91
|
+
def each(...)
|
92
|
+
@tables.each(...)
|
81
93
|
end
|
82
94
|
|
95
|
+
attr_writer :book
|
96
|
+
|
83
97
|
# Removes all lines from this table
|
84
98
|
#
|
85
99
|
# @return [Workbook::Table] (self)
|
86
100
|
def delete_all
|
87
|
-
|
101
|
+
@tables.delete_if { |b| true }
|
88
102
|
end
|
89
103
|
|
90
104
|
# clones itself *and* the tables it contains
|
91
105
|
#
|
92
106
|
# @return [Workbook::Sheet] The cloned sheet
|
93
107
|
def clone
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
s.each{|t| c << t.clone}
|
98
|
-
return c
|
108
|
+
clone = super
|
109
|
+
@tables = @tables.map(&:clone)
|
110
|
+
clone
|
99
111
|
end
|
100
112
|
|
101
113
|
# Create or open the existing table at an index value
|
102
114
|
#
|
103
115
|
# @param [Integer] index the index of the table
|
104
116
|
def create_or_open_table_at index
|
105
|
-
|
106
|
-
t = self[index] = Workbook::Table.new if t == nil
|
107
|
-
t.sheet = self
|
108
|
-
t
|
117
|
+
self[index]
|
109
118
|
end
|
110
119
|
end
|
111
120
|
end
|
data/lib/workbook/table.rb
CHANGED
@@ -1,30 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# -*- encoding : utf-8 -*-
|
4
2
|
# frozen_string_literal: true
|
5
|
-
require 'workbook/modules/diff_sort'
|
6
|
-
require 'workbook/writers/csv_table_writer'
|
7
|
-
require 'workbook/writers/json_table_writer'
|
8
|
-
require 'workbook/writers/html_writer'
|
9
3
|
|
4
|
+
require "workbook/modules/diff_sort"
|
5
|
+
require "workbook/writers/csv_table_writer"
|
6
|
+
require "workbook/writers/json_table_writer"
|
7
|
+
require "workbook/writers/html_writer"
|
10
8
|
|
11
9
|
module Workbook
|
12
10
|
# 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.
|
13
|
-
class Table
|
11
|
+
class Table
|
12
|
+
include Enumerable
|
13
|
+
extend Forwardable
|
14
|
+
|
14
15
|
include Workbook::Modules::TableDiffSort
|
15
16
|
include Workbook::Writers::CsvTableWriter
|
16
17
|
include Workbook::Writers::JsonTableWriter
|
17
18
|
include Workbook::Writers::HtmlTableWriter
|
18
19
|
|
20
|
+
delegate [:first, :last, :pop, :delete_at, :each, :collect, :each_with_index, :count, :index, :delete_if, :include?] => :@rows
|
21
|
+
|
19
22
|
attr_accessor :name
|
23
|
+
attr_reader :rows
|
20
24
|
|
21
|
-
def initialize row_cel_values=[], sheet=nil, options={}
|
22
|
-
|
23
|
-
row_cel_values
|
25
|
+
def initialize row_cel_values = [], sheet = nil, options = {}
|
26
|
+
@rows = []
|
27
|
+
row_cel_values = [] if row_cel_values.nil?
|
28
|
+
row_cel_values.each_with_index do |r, ri|
|
24
29
|
if r.is_a? Workbook::Row
|
25
30
|
r.table = self
|
26
31
|
else
|
27
|
-
r = Workbook::Row.new(r,self, options)
|
32
|
+
r = Workbook::Row.new(r, self, options)
|
28
33
|
end
|
29
34
|
define_columns_with_row(r) if ri == 0
|
30
35
|
end
|
@@ -44,9 +49,9 @@ module Workbook
|
|
44
49
|
#
|
45
50
|
# @return [Workbook::Row] The header
|
46
51
|
def header
|
47
|
-
if defined?(@header)
|
52
|
+
if defined?(@header) && (@header == false)
|
48
53
|
false
|
49
|
-
elsif defined?(@header)
|
54
|
+
elsif defined?(@header) && @header
|
50
55
|
@header
|
51
56
|
else
|
52
57
|
first
|
@@ -59,10 +64,10 @@ module Workbook
|
|
59
64
|
# @param [Workbook::Row, Integer] h should be the row or the index of this table's row
|
60
65
|
# @return [Workbook::Row] The header
|
61
66
|
def header= h
|
62
|
-
if h.is_a? Numeric
|
63
|
-
|
67
|
+
@header = if h.is_a? Numeric
|
68
|
+
self[h]
|
64
69
|
else
|
65
|
-
|
70
|
+
h
|
66
71
|
end
|
67
72
|
end
|
68
73
|
|
@@ -70,7 +75,7 @@ module Workbook
|
|
70
75
|
#
|
71
76
|
# @return [Integer] The index of the header row (typically 0)
|
72
77
|
def header_row_index
|
73
|
-
|
78
|
+
@rows.map(&:object_id).index(header.object_id)
|
74
79
|
end
|
75
80
|
|
76
81
|
def define_columns_with_row(r)
|
@@ -83,16 +88,15 @@ module Workbook
|
|
83
88
|
#
|
84
89
|
# @param [Array, Workbook::Row] cell_values is an array or row of cell values
|
85
90
|
# @return [Workbook::Row] the newly created row
|
86
|
-
def new_row cell_values=[]
|
87
|
-
|
88
|
-
return r
|
91
|
+
def new_row cell_values = []
|
92
|
+
Workbook::Row.new(cell_values, self)
|
89
93
|
end
|
90
94
|
|
91
95
|
def create_or_open_row_at index
|
92
96
|
r = self[index]
|
93
|
-
if r
|
97
|
+
if r.nil?
|
94
98
|
r = Workbook::Row.new
|
95
|
-
r.table=
|
99
|
+
r.table = self
|
96
100
|
end
|
97
101
|
r
|
98
102
|
end
|
@@ -101,28 +105,40 @@ module Workbook
|
|
101
105
|
#
|
102
106
|
# @return [Workbook::Table] self
|
103
107
|
def remove_empty_lines!
|
104
|
-
|
108
|
+
delete_if { |r| r.nil? || r.compact.empty? }
|
105
109
|
self
|
106
110
|
end
|
107
111
|
|
108
112
|
# Add row
|
109
|
-
# @param [Workbook::Table, Array] row to add
|
110
|
-
def push(
|
111
|
-
|
112
|
-
|
113
|
-
|
113
|
+
# @param [Workbook::Table, Array] row(s) to add
|
114
|
+
def push(*args)
|
115
|
+
args.each do |arg|
|
116
|
+
self.<<(arg)
|
117
|
+
end
|
114
118
|
end
|
119
|
+
alias_method :append, :push
|
115
120
|
|
116
121
|
# Add row
|
117
122
|
# @param [Workbook::Table, Array] row to add
|
118
123
|
def <<(row)
|
119
|
-
row = Workbook::Row
|
120
|
-
super(row)
|
124
|
+
row = row.is_a?(Workbook::Row) ? row : Workbook::Row.new(row)
|
121
125
|
row.set_table(self)
|
126
|
+
@rows << row
|
127
|
+
end
|
128
|
+
|
129
|
+
# Insert row
|
130
|
+
# @param [Integer] index where to add
|
131
|
+
# @param [Array, Workbook::Row] row(s) to add
|
132
|
+
def insert index, *rows
|
133
|
+
rows.each_with_index do |row, i|
|
134
|
+
row = row.is_a?(Workbook::Row) ? row : Workbook::Row.new(row)
|
135
|
+
row.set_table(self)
|
136
|
+
row.insert(index + i, row)
|
137
|
+
end
|
122
138
|
end
|
123
139
|
|
124
140
|
def has_contents?
|
125
|
-
|
141
|
+
clone.remove_empty_lines!.count != 0
|
126
142
|
end
|
127
143
|
|
128
144
|
# Returns true if the row exists in this table
|
@@ -131,42 +147,43 @@ module Workbook
|
|
131
147
|
# @return [Boolean] whether the row exist in this table
|
132
148
|
def contains_row? row
|
133
149
|
raise ArgumentError, "table should be a Workbook::Row (you passed a #{t.class})" unless row.is_a?(Workbook::Row)
|
134
|
-
|
150
|
+
collect { |r| r.object_id }.include? row.object_id
|
135
151
|
end
|
136
152
|
|
137
153
|
# Returns the sheet this table belongs to, creates a new sheet if none exists
|
138
154
|
#
|
139
155
|
# @return [Workbook::Sheet] The sheet this table belongs to
|
140
156
|
def sheet
|
141
|
-
return @sheet if defined?(@sheet)
|
142
|
-
self.sheet= Workbook::Sheet.new(self)
|
157
|
+
return @sheet if defined?(@sheet) && !@sheet.nil?
|
158
|
+
self.sheet = Workbook::Sheet.new(self)
|
143
159
|
end
|
144
160
|
|
145
161
|
# Returns the sheet this table belongs to, creates a new sheet if none exists
|
146
162
|
#
|
147
163
|
# @param [Workbook::Sheet] sheet this table belongs to
|
148
164
|
# @return [Workbook::Sheet] The sheet this table belongs to
|
149
|
-
|
150
|
-
@sheet = sheet
|
151
|
-
end
|
165
|
+
attr_writer :sheet
|
152
166
|
|
153
167
|
# Removes all lines from this table
|
154
168
|
#
|
155
169
|
# @return [Workbook::Table] (self)
|
156
170
|
def delete_all
|
157
|
-
|
171
|
+
delete_if { |b| true }
|
158
172
|
end
|
159
173
|
|
160
174
|
# clones itself *and* the rows it contains
|
161
175
|
#
|
162
176
|
# @return [Workbook::Table] The cloned table
|
163
177
|
def clone
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
178
|
+
table_clone = super
|
179
|
+
@rows = @rows.map { |row|
|
180
|
+
cloned_row = row.clone
|
181
|
+
cloned_row.set_table(self)
|
182
|
+
cloned_row
|
183
|
+
}
|
184
|
+
self.header = @rows[header_row_index] if header_row_index
|
185
|
+
table_clone.header = table_clone.rows[header_row_index] if header_row_index
|
186
|
+
table_clone
|
170
187
|
end
|
171
188
|
|
172
189
|
# Overrides normal Array's []-function with support for symbols that identify a column based on the header-values
|
@@ -175,19 +192,25 @@ module Workbook
|
|
175
192
|
# table[0] #=> <Row [a,2,3,4]> (first row)
|
176
193
|
# table["A1"] #=> <Cell value="a"> (first cell of first row)
|
177
194
|
#
|
178
|
-
# @param [
|
195
|
+
# @param [Integer, String] index_or_string to reference to either the row, or the cell
|
179
196
|
# @return [Workbook::Row, Workbook::Cell, nil]
|
180
197
|
def [] index_or_string
|
181
198
|
if index_or_string.is_a? String
|
182
199
|
match = index_or_string.match(/([A-Z]+)([0-9]*)/i)
|
183
200
|
col_index = Workbook::Column.alpha_index_to_number_index(match[1])
|
184
|
-
|
185
|
-
|
201
|
+
if match[2] == ""
|
202
|
+
columns[col_index]
|
203
|
+
else
|
204
|
+
row_index = match[2].to_i - 1
|
205
|
+
@rows[row_index][col_index]
|
206
|
+
end
|
207
|
+
elsif index_or_string.is_a? Symbol
|
208
|
+
header[index_or_string].column
|
186
209
|
elsif index_or_string.is_a? Range
|
187
|
-
collection =
|
188
|
-
|
210
|
+
collection = @rows[index_or_string].collect { |a| a.clone }
|
211
|
+
Workbook::Table.new collection
|
189
212
|
elsif index_or_string.is_a? Integer
|
190
|
-
|
213
|
+
@rows[index_or_string]
|
191
214
|
end
|
192
215
|
end
|
193
216
|
|
@@ -198,7 +221,7 @@ module Workbook
|
|
198
221
|
# `table[0] = <Row [a,2,3,4]>` (set first row)
|
199
222
|
# `table["A1"] = 2` (set first cell of first row to 2)
|
200
223
|
#
|
201
|
-
# @param [
|
224
|
+
# @param [Integer, String] index_or_string to reference to either the row, or the cell
|
202
225
|
# @param [Workbook::Table, Array] new_value to set
|
203
226
|
# @return [Workbook::Cell, nil]
|
204
227
|
def []= index_or_string, new_value
|
@@ -206,65 +229,66 @@ module Workbook
|
|
206
229
|
match = index_or_string.upcase.match(/([A-Z]*)([0-9]*)/)
|
207
230
|
cell_index = Workbook::Column.alpha_index_to_number_index(match[1])
|
208
231
|
row_index = match[2].to_i - 1
|
209
|
-
|
232
|
+
@rows[row_index][cell_index].value = new_value
|
210
233
|
else
|
211
|
-
row = new_value
|
212
|
-
row =
|
213
|
-
super(index_or_string,row)
|
214
|
-
row.set_table(self)
|
234
|
+
row = new_value.is_a?(Workbook::Row) ? new_value : Workbook::Row.new(new_value)
|
235
|
+
row.table = self
|
215
236
|
end
|
216
237
|
end
|
217
238
|
|
239
|
+
def == other
|
240
|
+
to_csv == other.to_csv
|
241
|
+
end
|
242
|
+
|
218
243
|
# remove all the trailing empty-rows (returning a trimmed clone)
|
219
244
|
#
|
220
245
|
# @param [Integer] desired_row_length of the rows
|
221
246
|
# @return [Workbook::Row] a trimmed clone of the array
|
222
|
-
def trim desired_row_length=nil
|
223
|
-
|
247
|
+
def trim desired_row_length = nil
|
248
|
+
clone.trim!(desired_row_length)
|
224
249
|
end
|
225
250
|
|
226
251
|
# remove all the trailing empty-rows (returning a trimmed self)
|
227
252
|
#
|
228
253
|
# @param [Integer] desired_row_length of the new row
|
229
254
|
# @return [Workbook::Row] self
|
230
|
-
def trim! desired_row_length=nil
|
231
|
-
max_length =
|
232
|
-
self_count =
|
233
|
-
|
255
|
+
def trim! desired_row_length = nil
|
256
|
+
max_length = collect { |a| a.trim.length }.max
|
257
|
+
self_count = count - 1
|
258
|
+
count.times do |index|
|
234
259
|
index = self_count - index
|
235
|
-
if
|
236
|
-
|
260
|
+
if @rows[index].trim.empty?
|
261
|
+
delete_at(index)
|
237
262
|
else
|
238
263
|
break
|
239
264
|
end
|
240
265
|
end
|
241
|
-
|
266
|
+
each { |a| a.trim!(max_length) }
|
242
267
|
self
|
243
268
|
end
|
244
269
|
|
245
270
|
# Returns The dimensions of this sheet based on longest row
|
246
271
|
# @return [Array] x-width, y-height
|
247
272
|
def dimensions
|
248
|
-
height =
|
249
|
-
width =
|
250
|
-
[width,height]
|
273
|
+
height = count
|
274
|
+
width = collect { |a| a.length }.max
|
275
|
+
[width, height]
|
251
276
|
end
|
252
277
|
|
253
278
|
# Returns an array of Column-classes describing the columns of this table
|
254
279
|
# @return [Array<Column>] columns
|
255
280
|
def columns
|
256
|
-
@columns ||= header.collect
|
281
|
+
@columns ||= header.collect { |header_cell|
|
257
282
|
Column.new(self)
|
258
|
-
|
283
|
+
}
|
259
284
|
end
|
260
285
|
|
261
286
|
# Returns an array of Column-classes describing the columns of this table
|
262
287
|
# @param [Array<Column>] columns
|
263
288
|
# @return [Array<Column>] columns
|
264
289
|
def columns= columns
|
265
|
-
columns.each{|c| c.table=self}
|
290
|
+
columns.each { |c| c.table = self }
|
266
291
|
@columns = columns
|
267
292
|
end
|
268
|
-
|
269
293
|
end
|
270
294
|
end
|
data/lib/workbook/template.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# -*- encoding : utf-8 -*-
|
4
2
|
# frozen_string_literal: true
|
5
|
-
|
3
|
+
|
4
|
+
require "workbook/modules/raw_objects_storage"
|
6
5
|
|
7
6
|
module Workbook
|
8
7
|
# Workbook::Template is a container for different Workbook::Format's and the storage of raw template data that isn't really supported by Workbook, but should survive a typical read/write cyclus.
|
@@ -25,9 +24,9 @@ module Workbook
|
|
25
24
|
def add_format format
|
26
25
|
if format.is_a? Workbook::Format
|
27
26
|
if format.name
|
28
|
-
@formats[format.name]=format
|
27
|
+
@formats[format.name] = format
|
29
28
|
else
|
30
|
-
@formats[@formats.keys.count]=format
|
29
|
+
@formats[@formats.keys.count] = format
|
31
30
|
end
|
32
31
|
else
|
33
32
|
raise ArgumentError, "format should be a Workboot::Format"
|
@@ -36,31 +35,29 @@ module Workbook
|
|
36
35
|
|
37
36
|
# Return the list of associated formats
|
38
37
|
# @return [Hash] A keyed-hash of named formats
|
39
|
-
|
40
|
-
@formats
|
41
|
-
end
|
38
|
+
attr_reader :formats
|
42
39
|
|
43
40
|
# Create or find a format by name
|
44
41
|
# @return [Workbook::Format] The new or found format
|
45
42
|
# @param [String] name of the format (e.g. whatever you want, in diff names such as 'destroyed', 'updated' and 'created' are being used)
|
46
43
|
# @param [Symbol] variant can also be a strftime formatting string (e.g. "%Y-%m-%d")
|
47
|
-
def create_or_find_format_by name, variant
|
44
|
+
def create_or_find_format_by name, variant = :default
|
48
45
|
fs = @formats[name]
|
49
46
|
fs = @formats[name] = {} if fs.nil?
|
50
47
|
f = fs[variant]
|
51
48
|
if f.nil?
|
52
|
-
|
53
|
-
|
54
|
-
|
49
|
+
@formats[name][variant] = if (variant != :default) && fs[:default]
|
50
|
+
fs[:default].clone
|
51
|
+
else
|
52
|
+
Workbook::Format.new
|
55
53
|
end
|
56
|
-
@formats[name][variant] = f
|
57
54
|
end
|
58
|
-
|
55
|
+
@formats[name][variant]
|
59
56
|
end
|
60
57
|
|
61
58
|
def set_default_formats!
|
62
59
|
header_fmt = create_or_find_format_by :header
|
63
|
-
header_fmt[:font_weight] =
|
60
|
+
header_fmt[:font_weight] = "bold"
|
64
61
|
end
|
65
62
|
end
|
66
63
|
end
|
data/lib/workbook/types/false.rb
CHANGED
data/lib/workbook/types/nil.rb
CHANGED
data/lib/workbook/types/time.rb
CHANGED
data/lib/workbook/types/true.rb
CHANGED
data/lib/workbook/version.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# -*- encoding : utf-8 -*-
|
4
2
|
# frozen_string_literal: true
|
5
|
-
|
3
|
+
|
4
|
+
require "csv"
|
6
5
|
|
7
6
|
module Workbook
|
8
7
|
module Writers
|
@@ -11,15 +10,14 @@ module Workbook
|
|
11
10
|
#
|
12
11
|
# @param [Hash] options (not used)
|
13
12
|
# @return [String] csv (comma separated values in a string)
|
14
|
-
def to_csv options={}
|
13
|
+
def to_csv options = {}
|
15
14
|
csv = ""
|
16
|
-
|
17
|
-
|
18
|
-
line=nil
|
15
|
+
@rows.each_with_index do |r, ri|
|
16
|
+
line = nil
|
19
17
|
begin
|
20
|
-
line = CSV
|
18
|
+
line = CSV.generate_line(r.cells.collect { |c| c&.value }, row_sep: "")
|
21
19
|
rescue TypeError
|
22
|
-
line = CSV
|
20
|
+
line = CSV.generate_line(r.cells.collect { |c| c&.value })
|
23
21
|
end
|
24
22
|
csv += "#{line}\n"
|
25
23
|
end
|
@@ -31,11 +29,10 @@ module Workbook
|
|
31
29
|
# @param [String] filename
|
32
30
|
# @param [Hash] options see #to_csv
|
33
31
|
# @return [String] filename
|
34
|
-
def write_to_csv filename="
|
35
|
-
File.open(filename,
|
36
|
-
|
32
|
+
def write_to_csv filename = "untitled document.csv", options = {}
|
33
|
+
File.open(filename, "w") { |f| f.write(to_csv(options)) }
|
34
|
+
filename
|
37
35
|
end
|
38
|
-
|
39
36
|
end
|
40
37
|
end
|
41
38
|
end
|