workbook 0.8.0 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- 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 +16 -0
- data/Gemfile +4 -2
- data/README.md +9 -7
- data/Rakefile +9 -7
- data/lib/workbook/book.rb +74 -61
- data/lib/workbook/cell.rb +59 -12
- data/lib/workbook/column.rb +33 -29
- data/lib/workbook/format.rb +24 -23
- data/lib/workbook/generatetypes.rb +6 -5
- data/lib/workbook/modules/cache.rb +8 -7
- data/lib/workbook/modules/cell.rb +78 -99
- data/lib/workbook/modules/diff_sort.rb +93 -82
- data/lib/workbook/modules/raw_objects_storage.rb +7 -7
- data/lib/workbook/modules/type_parser.rb +31 -21
- data/lib/workbook/nil_value.rb +5 -9
- data/lib/workbook/readers/csv_reader.rb +7 -9
- data/lib/workbook/readers/ods_reader.rb +49 -49
- data/lib/workbook/readers/txt_reader.rb +7 -7
- data/lib/workbook/readers/xls_reader.rb +22 -32
- data/lib/workbook/readers/xls_shared.rb +107 -116
- data/lib/workbook/readers/xlsx_reader.rb +47 -46
- data/lib/workbook/row.rb +100 -83
- data/lib/workbook/sheet.rb +48 -37
- data/lib/workbook/table.rb +97 -71
- data/lib/workbook/template.rb +13 -14
- data/lib/workbook/types/date.rb +2 -1
- data/lib/workbook/types/false.rb +1 -1
- data/lib/workbook/types/false_class.rb +2 -1
- data/lib/workbook/types/nil.rb +1 -1
- data/lib/workbook/types/nil_class.rb +3 -2
- data/lib/workbook/types/numeric.rb +3 -2
- data/lib/workbook/types/string.rb +3 -2
- data/lib/workbook/types/time.rb +3 -2
- data/lib/workbook/types/true.rb +1 -1
- data/lib/workbook/types/true_class.rb +3 -2
- data/lib/workbook/version.rb +3 -2
- data/lib/workbook/writers/csv_table_writer.rb +11 -12
- data/lib/workbook/writers/html_writer.rb +35 -37
- data/lib/workbook/writers/json_table_writer.rb +9 -10
- data/lib/workbook/writers/xls_writer.rb +31 -35
- data/lib/workbook/writers/xlsx_writer.rb +46 -28
- data/lib/workbook.rb +17 -14
- data/test/helper.rb +8 -5
- data/test/test_book.rb +43 -38
- data/test/test_column.rb +29 -25
- data/test/test_format.rb +53 -55
- data/test/test_functional.rb +9 -8
- data/test/test_modules_cache.rb +20 -17
- data/test/test_modules_cell.rb +49 -46
- data/test/test_modules_table_diff_sort.rb +56 -63
- data/test/test_modules_type_parser.rb +63 -31
- data/test/test_readers_csv_reader.rb +50 -42
- data/test/test_readers_ods_reader.rb +30 -31
- data/test/test_readers_txt_reader.rb +23 -23
- data/test/test_readers_xls_reader.rb +22 -23
- data/test/test_readers_xls_shared.rb +5 -4
- data/test/test_readers_xlsx_reader.rb +46 -37
- data/test/test_row.rb +107 -109
- data/test/test_sheet.rb +43 -35
- data/test/test_table.rb +84 -60
- data/test/test_template.rb +18 -15
- data/test/test_types_date.rb +7 -7
- data/test/test_writers_csv_writer.rb +24 -0
- data/test/test_writers_html_writer.rb +44 -41
- data/test/test_writers_json_writer.rb +11 -9
- data/test/test_writers_xls_writer.rb +52 -35
- data/test/test_writers_xlsx_writer.rb +64 -34
- data/workbook.gemspec +26 -27
- metadata +93 -27
@@ -1,6 +1,7 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
# frozen_string_literal: true
|
3
|
-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "csv"
|
4
5
|
|
5
6
|
module Workbook
|
6
7
|
module Writers
|
@@ -9,15 +10,14 @@ module Workbook
|
|
9
10
|
#
|
10
11
|
# @param [Hash] options (not used)
|
11
12
|
# @return [String] csv (comma separated values in a string)
|
12
|
-
def to_csv options={}
|
13
|
+
def to_csv options = {}
|
13
14
|
csv = ""
|
14
|
-
|
15
|
-
|
16
|
-
line=nil
|
15
|
+
@rows.each_with_index do |r, ri|
|
16
|
+
line = nil
|
17
17
|
begin
|
18
|
-
line = CSV
|
18
|
+
line = CSV.generate_line(r.cells.collect { |c| c&.value }, row_sep: "")
|
19
19
|
rescue TypeError
|
20
|
-
line = CSV
|
20
|
+
line = CSV.generate_line(r.cells.collect { |c| c&.value })
|
21
21
|
end
|
22
22
|
csv += "#{line}\n"
|
23
23
|
end
|
@@ -29,11 +29,10 @@ module Workbook
|
|
29
29
|
# @param [String] filename
|
30
30
|
# @param [Hash] options see #to_csv
|
31
31
|
# @return [String] filename
|
32
|
-
def write_to_csv filename="
|
33
|
-
File.open(filename,
|
34
|
-
|
32
|
+
def write_to_csv filename = "untitled document.csv", options = {}
|
33
|
+
File.open(filename, "w") { |f| f.write(to_csv(options)) }
|
34
|
+
filename
|
35
35
|
end
|
36
|
-
|
37
36
|
end
|
38
37
|
end
|
39
38
|
end
|
@@ -1,34 +1,34 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
# frozen_string_literal: true
|
3
|
-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "spreadsheet"
|
4
5
|
|
5
6
|
module Workbook
|
6
7
|
module Writers
|
7
8
|
module HtmlWriter
|
8
|
-
|
9
9
|
# Generates an HTML table ()
|
10
10
|
#
|
11
11
|
# @param [Hash] options A hash with options
|
12
12
|
# @return [String] A String containing the HTML code
|
13
|
-
def to_html options={}
|
14
|
-
builder = Nokogiri::XML::Builder.new
|
13
|
+
def to_html options = {}
|
14
|
+
builder = Nokogiri::XML::Builder.new { |doc|
|
15
15
|
doc.html {
|
16
16
|
doc.body {
|
17
|
-
|
18
|
-
doc.h1
|
17
|
+
each do |sheet|
|
18
|
+
doc.h1 do
|
19
19
|
doc.text sheet.name
|
20
|
-
|
21
|
-
sheet.each
|
22
|
-
doc.h2
|
20
|
+
end
|
21
|
+
sheet.each do |table|
|
22
|
+
doc.h2 do
|
23
23
|
doc.text table.name
|
24
|
-
|
24
|
+
end
|
25
25
|
doc << table.to_html(options)
|
26
|
-
|
27
|
-
|
26
|
+
end
|
27
|
+
end
|
28
28
|
}
|
29
29
|
}
|
30
|
-
|
31
|
-
|
30
|
+
}
|
31
|
+
builder.doc.to_xhtml
|
32
32
|
end
|
33
33
|
|
34
34
|
# Write the current workbook to HTML format
|
@@ -37,9 +37,9 @@ module Workbook
|
|
37
37
|
# @param [Hash] options see #to_xls
|
38
38
|
# @return [String] filename
|
39
39
|
|
40
|
-
def write_to_html filename="#{title}.html", options={}
|
41
|
-
File.open(filename,
|
42
|
-
|
40
|
+
def write_to_html filename = "#{title}.html", options = {}
|
41
|
+
File.open(filename, "w") { |f| f.write(to_html(options)) }
|
42
|
+
filename
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -48,16 +48,16 @@ module Workbook
|
|
48
48
|
#
|
49
49
|
# @param [Hash] options A hash with options
|
50
50
|
# @return [String] A String containing the HTML code, most importantly `:style_with_inline_css` (default false)
|
51
|
-
def to_html options={}
|
52
|
-
options = {:
|
53
|
-
builder = Nokogiri::XML::Builder.new
|
51
|
+
def to_html options = {}
|
52
|
+
options = {style_with_inline_css: false}.merge(options)
|
53
|
+
builder = Nokogiri::XML::Builder.new { |doc|
|
54
54
|
doc.table do
|
55
55
|
doc.thead do
|
56
56
|
if header
|
57
57
|
doc.tr do
|
58
58
|
header.each do |cell|
|
59
59
|
th_options = build_cell_options cell, options.merge(classnames: [cell.to_sym], data: {key: cell.to_sym})
|
60
|
-
unless cell.value.
|
60
|
+
unless cell.value.instance_of?(Workbook::NilValue)
|
61
61
|
doc.th(th_options) do
|
62
62
|
doc.text cell.value
|
63
63
|
end
|
@@ -67,12 +67,12 @@ module Workbook
|
|
67
67
|
end
|
68
68
|
end
|
69
69
|
doc.tbody do
|
70
|
-
|
70
|
+
each do |row|
|
71
71
|
unless row.header?
|
72
72
|
doc.tr do
|
73
73
|
row.each do |cell|
|
74
74
|
td_options = build_cell_options cell, options
|
75
|
-
unless cell.value.
|
75
|
+
unless cell.value.instance_of?(Workbook::NilValue)
|
76
76
|
doc.td(td_options) do
|
77
77
|
doc.text cell.value
|
78
78
|
end
|
@@ -83,24 +83,22 @@ module Workbook
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
86
|
-
|
87
|
-
|
86
|
+
}
|
87
|
+
builder.doc.to_xhtml
|
88
88
|
end
|
89
89
|
|
90
|
-
def build_cell_options cell, options={}
|
90
|
+
def build_cell_options cell, options = {}
|
91
91
|
classnames = cell.format.all_names
|
92
|
-
classnames
|
92
|
+
classnames += options[:classnames] if options[:classnames]
|
93
93
|
classnames = classnames.join(" ").strip
|
94
|
-
td_options = classnames != "" ? {:
|
95
|
-
|
96
|
-
|
97
|
-
td_options = td_options.merge({("data-#{key}".to_sym) => value})
|
98
|
-
end
|
94
|
+
td_options = classnames != "" ? {class: classnames} : {}
|
95
|
+
options[:data]&.each do |key, value|
|
96
|
+
td_options = td_options.merge({"data-#{key}".to_sym => value})
|
99
97
|
end
|
100
|
-
td_options = td_options.merge({:
|
101
|
-
td_options = td_options.merge({:
|
102
|
-
td_options = td_options.merge({:
|
103
|
-
|
98
|
+
td_options = td_options.merge({style: cell.format.to_css}) if options[:style_with_inline_css] && (cell.format.to_css != "")
|
99
|
+
td_options = td_options.merge({colspan: cell.colspan}) if cell.colspan
|
100
|
+
td_options = td_options.merge({rowspan: cell.rowspan}) if cell.rowspan
|
101
|
+
td_options
|
104
102
|
end
|
105
103
|
end
|
106
104
|
end
|
@@ -1,6 +1,7 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
# frozen_string_literal: true
|
3
|
-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "json"
|
4
5
|
|
5
6
|
module Workbook
|
6
7
|
module Writers
|
@@ -9,7 +10,7 @@ module Workbook
|
|
9
10
|
#
|
10
11
|
# @param [Hash] options
|
11
12
|
# @return [String] json string
|
12
|
-
def to_json options={}
|
13
|
+
def to_json options = {}
|
13
14
|
JSON.generate(to_array_of_hashes_with_values(options))
|
14
15
|
end
|
15
16
|
|
@@ -17,9 +18,8 @@ module Workbook
|
|
17
18
|
#
|
18
19
|
# @param [Hash] options
|
19
20
|
# @return [Array<Hash>] array with hashes (comma separated values in a string)
|
20
|
-
def to_array_of_hashes_with_values options={}
|
21
|
-
|
22
|
-
return array_of_hashes
|
21
|
+
def to_array_of_hashes_with_values options = {}
|
22
|
+
collect { |a| a.to_hash_with_values unless a.header? }.compact
|
23
23
|
end
|
24
24
|
|
25
25
|
# Write the current workbook to JSON format
|
@@ -27,11 +27,10 @@ module Workbook
|
|
27
27
|
# @param [String] filename
|
28
28
|
# @param [Hash] options see #to_json
|
29
29
|
# @return [String] filename
|
30
|
-
def write_to_json filename="#{title}.json", options={}
|
31
|
-
File.open(filename,
|
32
|
-
|
30
|
+
def write_to_json filename = "#{title}.json", options = {}
|
31
|
+
File.open(filename, "w") { |f| f.write(to_json(options)) }
|
32
|
+
filename
|
33
33
|
end
|
34
|
-
|
35
34
|
end
|
36
35
|
end
|
37
36
|
end
|
@@ -1,28 +1,28 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
# frozen_string_literal: true
|
3
|
-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "spreadsheet"
|
4
5
|
|
5
6
|
module Workbook
|
6
7
|
module Writers
|
7
8
|
module XlsWriter
|
8
|
-
|
9
9
|
# Generates an Spreadsheet (from the spreadsheet gem) in order to build an xls
|
10
10
|
#
|
11
11
|
# @param [Hash] options A hash with options (unused so far)
|
12
12
|
# @return [Spreadsheet] A Spreadsheet object, ready for writing or more lower level operations
|
13
|
-
def to_xls options={}
|
13
|
+
def to_xls options = {}
|
14
14
|
book = init_spreadsheet_template
|
15
|
-
|
15
|
+
each_with_index do |s, si|
|
16
16
|
xls_sheet = xls_sheet(si)
|
17
17
|
xls_sheet.name = s.name
|
18
18
|
|
19
19
|
s.table.each_with_index do |r, ri|
|
20
|
-
xls_sheet.row(ri).height= r.format[:height] if r.format
|
20
|
+
xls_sheet.row(ri).height = r.format[:height] if r.format
|
21
21
|
r.each_with_index do |c, ci|
|
22
22
|
if c
|
23
23
|
if r.first?
|
24
|
-
xls_sheet.columns[ci] ||= Spreadsheet::Column.new(ci,nil)
|
25
|
-
xls_sheet.columns[ci].width= c.format[:width]
|
24
|
+
xls_sheet.columns[ci] ||= Spreadsheet::Column.new(ci, nil)
|
25
|
+
xls_sheet.columns[ci].width = c.format[:width]
|
26
26
|
end
|
27
27
|
xls_sheet.row(ri)[ci] = c.value
|
28
28
|
xls_sheet.row(ri).set_format(ci, format_to_xls_format(c.format))
|
@@ -30,12 +30,11 @@ module Workbook
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
(xls_sheet.last_row_index + 1 - s.table.count).times do |time|
|
33
|
-
row_to_remove = s.table.count+time
|
34
|
-
remove_row(xls_sheet,row_to_remove)
|
33
|
+
row_to_remove = s.table.count + time
|
34
|
+
remove_row(xls_sheet, row_to_remove)
|
35
35
|
end
|
36
36
|
xls_sheet.updated_from(s.table.count)
|
37
37
|
xls_sheet.dimensions
|
38
|
-
|
39
38
|
end
|
40
39
|
# kind of a hack, deleting by popping from xls worksheet results in errors in MS Excel (not LibreOffice)
|
41
40
|
# book.worksheets.pop(book.worksheets.count - self.count) if book.worksheets and book.worksheets.count > self.count
|
@@ -44,10 +43,10 @@ module Workbook
|
|
44
43
|
xls_sheet.visibility = :visible
|
45
44
|
else
|
46
45
|
xls_sheet.visibility = :strong_hidden
|
47
|
-
#also make sure all data is removed, in case someone finds out about this 'trick'
|
46
|
+
# also make sure all data is removed, in case someone finds out about this 'trick'
|
48
47
|
xls_sheet.name = "RemovedSheet#{si}"
|
49
48
|
(xls_sheet.last_row_index + 1).times do |row_index|
|
50
|
-
remove_row(xls_sheet,row_index)
|
49
|
+
remove_row(xls_sheet, row_index)
|
51
50
|
end
|
52
51
|
end
|
53
52
|
end
|
@@ -56,20 +55,17 @@ module Workbook
|
|
56
55
|
book
|
57
56
|
end
|
58
57
|
|
59
|
-
|
60
|
-
|
61
58
|
# Generates an Spreadsheet (from the spreadsheet gem) in order to build an XlS
|
62
59
|
#
|
63
60
|
# @param [Workbook::Format, Hash] f A Workbook::Format or hash with format-options (:font_weight, :rotation, :background_color, :number_format, :text_direction, :color, :font_family)
|
64
61
|
# @return [Spreadsheet::Format] A Spreadsheet format-object, ready for writing or more lower level operations
|
65
62
|
def format_to_xls_format f
|
66
|
-
xlsfmt = nil
|
67
63
|
unless f.is_a? Workbook::Format
|
68
64
|
f = Workbook::Format.new f
|
69
65
|
end
|
70
66
|
xlsfmt = f.return_raw_for Spreadsheet::Format
|
71
67
|
unless xlsfmt
|
72
|
-
xlsfmt=Spreadsheet::Format.new :
|
68
|
+
xlsfmt = Spreadsheet::Format.new weight: f[:font_weight]
|
73
69
|
xlsfmt.rotation = f[:rotation] if f[:rotation]
|
74
70
|
xlsfmt.pattern_fg_color = html_color_to_xls_color(f[:background_color]) if html_color_to_xls_color(f[:background_color])
|
75
71
|
xlsfmt.pattern = 1 if html_color_to_xls_color(f[:background_color])
|
@@ -81,7 +77,7 @@ module Workbook
|
|
81
77
|
xlsfmt.font.color = color if color
|
82
78
|
f.add_raw xlsfmt
|
83
79
|
end
|
84
|
-
|
80
|
+
xlsfmt
|
85
81
|
end
|
86
82
|
|
87
83
|
# Parses right font-family name
|
@@ -89,20 +85,20 @@ module Workbook
|
|
89
85
|
# @param [Workbook::Format, hash] format to parse
|
90
86
|
def parse_font_family(format)
|
91
87
|
font = format[:font_family].to_s.split.last
|
92
|
-
valid_values = [:none
|
88
|
+
valid_values = [:none, :roman, :swiss, :modern, :script, :decorative]
|
93
89
|
if valid_values.include?(font)
|
94
|
-
|
90
|
+
font
|
95
91
|
elsif valid_values.include?(font.to_s.downcase.to_sym)
|
96
|
-
|
92
|
+
font.to_s.downcase.to_sym
|
97
93
|
else
|
98
94
|
font = font.to_s.downcase.strip
|
99
95
|
translation = {
|
100
|
-
"arial"
|
101
|
-
"times"
|
102
|
-
"times new roman"
|
96
|
+
"arial" => :swiss,
|
97
|
+
"times" => :roman,
|
98
|
+
"times new roman" => :roman
|
103
99
|
}
|
104
100
|
tfont = translation[font]
|
105
|
-
|
101
|
+
tfont || :none
|
106
102
|
end
|
107
103
|
end
|
108
104
|
|
@@ -110,40 +106,40 @@ module Workbook
|
|
110
106
|
#
|
111
107
|
# @param [String] filename
|
112
108
|
# @param [Hash] options see #to_xls
|
113
|
-
def write_to_xls filename="#{title}.xls", options={}
|
109
|
+
def write_to_xls filename = "#{title}.xls", options = {}
|
114
110
|
if to_xls(options).write(filename)
|
115
|
-
|
111
|
+
filename
|
116
112
|
end
|
117
113
|
end
|
118
114
|
|
119
115
|
def xls_sheet a
|
120
116
|
if xls_template.worksheet(a)
|
121
|
-
|
117
|
+
xls_template.worksheet(a)
|
122
118
|
else
|
123
119
|
xls_template.create_worksheet
|
124
|
-
|
120
|
+
xls_sheet a
|
125
121
|
end
|
126
122
|
end
|
127
123
|
|
128
124
|
def xls_template
|
129
|
-
|
125
|
+
template.raws[Spreadsheet::Excel::Workbook] || template.raws[Spreadsheet::Workbook]
|
130
126
|
end
|
131
127
|
|
132
128
|
def init_spreadsheet_template
|
133
|
-
if
|
134
|
-
|
129
|
+
if xls_template.is_a? Spreadsheet::Workbook
|
130
|
+
xls_template
|
135
131
|
else
|
136
132
|
t = Spreadsheet::Workbook.new
|
137
133
|
template.add_raw t
|
138
|
-
|
134
|
+
t
|
139
135
|
end
|
140
136
|
end
|
141
137
|
|
142
138
|
private
|
143
139
|
|
144
|
-
def remove_row(xls_sheet,row_index)
|
140
|
+
def remove_row(xls_sheet, row_index)
|
145
141
|
xls_sheet.row(row_index).each_with_index do |c, ci|
|
146
|
-
xls_sheet.row(row_index)[ci]=nil
|
142
|
+
xls_sheet.row(row_index)[ci] = nil
|
147
143
|
end
|
148
144
|
xls_sheet.delete_row(row_index)
|
149
145
|
xls_sheet.row_updated(row_index, xls_sheet.row(row_index))
|
@@ -1,35 +1,51 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require
|
2
|
+
|
3
|
+
require "axlsx"
|
4
|
+
require "workbook/readers/xls_shared"
|
5
5
|
|
6
6
|
module Workbook
|
7
7
|
module Writers
|
8
8
|
module XlsxWriter
|
9
|
-
|
9
|
+
CELL_TYPE_MAPPING = {
|
10
|
+
decimal: :integer,
|
11
|
+
integer: :integer,
|
12
|
+
float: :float,
|
13
|
+
string: :text,
|
14
|
+
time: :time,
|
15
|
+
date: :date,
|
16
|
+
datetime: :time,
|
17
|
+
boolean: :boolean,
|
18
|
+
nil: :string
|
19
|
+
}
|
10
20
|
# Generates an axlsx doc, ready for export to XLSX
|
11
21
|
#
|
12
22
|
# @param [Hash] options A hash with options (unused so far)
|
13
23
|
# @return [Axlsx::Package] An object, ready for writing or more lower level operations
|
14
|
-
def to_xlsx options={}
|
24
|
+
def to_xlsx options = {}
|
15
25
|
formats_to_xlsx_format
|
26
|
+
|
16
27
|
book = init_xlsx_spreadsheet_template.workbook
|
17
|
-
book.worksheets.pop(book.worksheets.count -
|
18
|
-
|
28
|
+
book.worksheets.pop(book.worksheets.count - count) if book.worksheets && (book.worksheets.count > count)
|
29
|
+
each_with_index do |s, si|
|
19
30
|
xlsx_sheet = xlsx_sheet(si)
|
20
31
|
xlsx_sheet.name = s.name
|
21
32
|
s.table.each_with_index do |r, ri|
|
22
|
-
xlsx_row = xlsx_sheet[ri]
|
33
|
+
xlsx_row = xlsx_sheet[ri] || xlsx_sheet.add_row
|
23
34
|
xlsx_row.height = 16
|
24
35
|
xlsx_row_a = xlsx_row.to_ary
|
25
36
|
r.each_with_index do |c, ci|
|
26
37
|
xlsx_row.add_cell(c.value) unless xlsx_row_a[ci]
|
27
38
|
xlsx_cell = xlsx_row_a[ci]
|
39
|
+
|
40
|
+
xlsx_cell.type = CELL_TYPE_MAPPING[c.cell_type]
|
28
41
|
xlsx_cell.value = c.value
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
42
|
+
|
43
|
+
if c.format? && c.format.raws[Integer]
|
44
|
+
xlsx_cell.style = c.format.raws[Integer]
|
45
|
+
elsif c.format? && !(c.value.is_a?(Date) || !c.value.is_a?(DateTime) || !c.value.is_a?(Time))
|
46
|
+
# TODO: disable formatting
|
47
|
+
# xlsx_cell.style = format_to_xlsx_format(c.format)
|
48
|
+
end
|
33
49
|
end
|
34
50
|
xlsx_sheet.send(:update_column_info, xlsx_row.cells, [])
|
35
51
|
end
|
@@ -52,39 +68,39 @@ module Workbook
|
|
52
68
|
#
|
53
69
|
# @param [String] filename
|
54
70
|
# @param [Hash] options see #to_xlsx
|
55
|
-
def write_to_xlsx filename="#{title}.xlsx", options={}
|
71
|
+
def write_to_xlsx filename = "#{title}.xlsx", options = {}
|
56
72
|
if to_xlsx(options).serialize(filename)
|
57
|
-
|
73
|
+
filename
|
58
74
|
end
|
59
75
|
end
|
60
76
|
|
61
77
|
def xlsx_sheet a
|
62
78
|
if xlsx_template.workbook.worksheets[a]
|
63
|
-
|
79
|
+
xlsx_template.workbook.worksheets[a]
|
64
80
|
else
|
65
81
|
xlsx_template.workbook.add_worksheet
|
66
|
-
|
82
|
+
xlsx_sheet a
|
67
83
|
end
|
68
84
|
end
|
69
85
|
|
70
86
|
def xlsx_template
|
71
|
-
|
87
|
+
template.raws[Axlsx::Package]
|
72
88
|
end
|
73
89
|
|
74
90
|
def init_xlsx_spreadsheet_template
|
75
|
-
if
|
76
|
-
|
91
|
+
if xlsx_template.is_a? Axlsx::Package
|
92
|
+
xlsx_template
|
77
93
|
else
|
78
94
|
t = Axlsx::Package.new
|
79
95
|
template.add_raw t
|
80
96
|
# template.set_default_formats!
|
81
|
-
|
97
|
+
t
|
82
98
|
end
|
83
99
|
end
|
84
100
|
|
85
101
|
def formats_to_xlsx_format
|
86
|
-
template.formats.each do |n,v|
|
87
|
-
v.each do |
|
102
|
+
template.formats.each do |n, v|
|
103
|
+
v.each do |t, s|
|
88
104
|
format_to_xlsx_format(s)
|
89
105
|
end
|
90
106
|
end
|
@@ -97,20 +113,22 @@ module Workbook
|
|
97
113
|
def format_to_xlsx_format f
|
98
114
|
f = make_sure_f_is_a_workbook_format f
|
99
115
|
|
100
|
-
xlsfmt={}
|
101
|
-
xlsfmt[:fg_color] = "FF#{f[:color].to_s.upcase}".
|
102
|
-
xlsfmt[:b] = true if f[:font_weight].to_s == "bold"
|
116
|
+
xlsfmt = {}
|
117
|
+
xlsfmt[:fg_color] = "FF#{f[:color].to_s.upcase}".delete("#") if f[:color]
|
118
|
+
xlsfmt[:b] = true if (f[:font_weight].to_s == "bold") || (f[:font_weight].to_i >= 600) || f[:font_style].to_s.match("oblique")
|
103
119
|
xlsfmt[:i] = true if f[:font_style].to_s == "italic"
|
104
|
-
xlsfmt[:u] = true if f[:text_decoration].to_s.
|
120
|
+
xlsfmt[:u] = true if f[:text_decoration].to_s.include?("underline")
|
105
121
|
xlsfmt[:bg_color] = f[:background_color] if f[:background_color]
|
106
122
|
xlsfmt[:format_code] = strftime_to_ms_format(f[:number_format]) if f[:number_format]
|
107
123
|
xlsfmt[:font_name] = f[:font_family].split.first if f[:font_family]
|
108
124
|
# xlsfmt[:family] = parse_font_family(f) if f[:font_family]
|
109
125
|
|
110
|
-
|
126
|
+
style_reference = init_xlsx_spreadsheet_template.workbook.styles.add_style(xlsfmt)
|
127
|
+
|
128
|
+
f.add_raw style_reference
|
111
129
|
f.add_raw xlsfmt
|
112
130
|
|
113
|
-
|
131
|
+
style_reference
|
114
132
|
end
|
115
133
|
end
|
116
134
|
end
|
data/lib/workbook.rb
CHANGED
@@ -1,28 +1,31 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
# frozen_string_literal: true
|
3
|
-
|
4
|
-
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
10
|
-
require_relative
|
11
|
-
require_relative
|
12
|
-
require_relative
|
13
|
-
require_relative
|
14
|
-
require_relative
|
15
|
-
require_relative
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative "workbook/modules/cache"
|
5
|
+
require_relative "workbook/modules/cell"
|
6
|
+
require_relative "workbook/types/date"
|
7
|
+
require_relative "workbook/book"
|
8
|
+
require_relative "workbook/sheet"
|
9
|
+
require_relative "workbook/table"
|
10
|
+
require_relative "workbook/nil_value"
|
11
|
+
require_relative "workbook/row"
|
12
|
+
require_relative "workbook/cell"
|
13
|
+
require_relative "workbook/format"
|
14
|
+
require_relative "workbook/template"
|
15
|
+
require_relative "workbook/version"
|
16
|
+
require_relative "workbook/column"
|
16
17
|
|
17
18
|
module Workbook
|
18
19
|
class << self
|
19
20
|
def caching_enabled?
|
20
21
|
@@caching_enabled ||= true
|
21
22
|
end
|
23
|
+
|
22
24
|
# Disable caching globally (wherever applicable)
|
23
25
|
def disable_caching!
|
24
26
|
@@caching_enabled = false
|
25
27
|
end
|
28
|
+
|
26
29
|
# Enable caching globally (wherever applicable)
|
27
30
|
def enable_caching!
|
28
31
|
@@caching_enabled = true
|
data/test/helper.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "simplecov"
|
4
|
+
SimpleCov.start
|
5
|
+
|
6
|
+
require "minitest/autorun"
|
7
|
+
require "rubygems"
|
8
|
+
require File.join(File.dirname(__FILE__), "../lib/workbook")
|