-
Notifications
You must be signed in to change notification settings - Fork 43
/
general_test.rb
199 lines (162 loc) · 5.48 KB
/
general_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
require 'test_helper'
class XlsxGeneralTest < ActiveSupport::TestCase
test "kitchen sink" do
options = {
headers: [
['Latest Posts'],
['Title','Category','Author','Posted on','Posted At','Earnings']
],
data: 50.times.map{|i| [i, "foobar-#{i}", 5.4*i, true, Date.today, Time.now]},
header_style: {background_color: "000000", color: "FFFFFF", align: :center, font_size: 12, bold: true},
row_style: {background_color: nil, color: "000000", align: :left, font_size: 12},
sheet_name: 'Kitchen Sink',
freeze_headers: true,
column_styles: [
{columns: 0, styles: {bold: true}},
{columns: (1..3), styles: {color: "444444"}},
{columns: [4], include_header: false, styles: {italic: true}}
],
range_styles: [
{range: "B2:C4", styles: {background_color: "CCCCCC"}},
{range: {rows: 1, columns: :all}, styles: {bold: true}},
{range: {rows: (20..25), columns: (1..4)}, styles: {italic: true}},
{range: {rows: :all, columns: (3..4)}, styles: {color: "999999"}}
],
conditional_row_styles: [
{
if: Proc.new{|row_data, row_index|
row_index == 0 || row_data[0].to_i == 2
},
styles: {font_size: 18}
},
{
unless: Proc.new{|row_data, row_index|
row_index <= 10 || row_data[0].to_i == 15
},
styles: {align: :right}
},
],
borders: [
{range: "B2:C4"},
{range: "D6:D7", border_styles: {style: :dashDot, color: "333333"}},
{range: {rows: (2..11), columns: :all}, border_styles: {edges: [:top,:bottom]}},
{range: {rows: 3, columns: 4}, border_styles: {edges: [:top,:bottom]}}
],
merges: [
{range: "A1:C1"},
{range: {rows: 2, columns: :all}},
{range: {rows: (3..4), columns: (0..3)}}
],
column_types: [
:integer,
:string,
:float,
:boolean,
:date,
:time,
nil,
],
}
# Using Array Data
file_data = SpreadsheetArchitect.to_xlsx(options)
save_file("xlsx/kitchen_sink.xlsx", file_data)
end
describe "header" do
test "header: false" do
spreadsheet = SpreadsheetArchitect.to_xlsx(headers: false, data: [[1]])
save_file("xlsx/headers_false_test.xlsx", spreadsheet)
end
end
test "use_zero_based_row_index" do
spreadsheet = SpreadsheetArchitect.to_xlsx(
use_zero_based_row_index: true,
headers: [1, 2, 3],
data: [
[4,5,6],
[7,8,9],
[10,11,12],
],
range_styles: [
{range: {rows: 0, columns: 0}, styles: {bold: true}},
{range: {rows: (2..3), columns: 0}, styles: {italic: true}},
],
merges: [
{range: {rows: 0, columns: 1..2}},
{range: {rows: (2..3), columns: 2}},
],
borders: [
{range: {rows: 0, columns: :all}},
{range: {rows: 3, columns: 0}},
],
)
save_file("xlsx/use_zero_based_row_index_test.xlsx", spreadsheet)
end
test "multisheet" do
test_data = [[1,2,3], [4,5,6], [7,8,9]]
package = Post.to_axlsx_package
package = CustomPost.to_axlsx_package({sheet_name: 'Latest Projects'}, package)
package = SpreadsheetArchitect.to_axlsx_package({data: test_data, sheet_name: 'Another Sheet'}, package)
save_file("xlsx/multi_sheet.xlsx", package)
end
describe "column_types" do
test "validates provided types" do
assert_raise SpreadsheetArchitect::Exceptions::ArgumentError do
SpreadsheetArchitect.to_axlsx_package(
data: [],
column_types: [""]
)
end
SpreadsheetArchitect.to_axlsx_package(
data: [],
column_types: [:string, :integer, :float, :date, :time, :boolean, :hyperlink, nil, ->(x){ :foo }]
)
end
test "works with Proc types" do
url = "https://github.com/westonganger/spreadsheet_architect"
data = [
[1, 2],
[1, url],
]
p = SpreadsheetArchitect.to_axlsx_package(
data: data,
column_types: [
:string,
->(x){ x.to_s.start_with?("http") ? :hyperlink : :string }
]
)
doc = parse_axlsx_package(p)
hyperlinks = doc.css("hyperlinks hyperlink")
assert_equal 1, hyperlinks.size
ref = "B2"
assert_equal ref, hyperlinks[0].attr("ref")
cell = doc.at("c[r='#{ref}']")
assert_equal url, cell.text
assert_equal "inlineStr", cell.attr("t")
save_file("xlsx/hyperlinks.xlsx", p)
end
describe "hyperlinks" do
test "shows the text and have the correct attributes" do
url = "https://github.com/westonganger/spreadsheet_architect"
data = [
[1,2,3],
[1, url, "https://github.com/caxlsx/caxlsx"],
]
p = SpreadsheetArchitect.to_axlsx_package(data: data, column_types: [:string, :hyperlink, :string])
doc = parse_axlsx_package(p)
hyperlinks = doc.css("hyperlinks hyperlink")
assert_equal 2, hyperlinks.size
ref = "B1"
assert_equal ref, hyperlinks[0].attr("ref")
cell = doc.at("c[r='#{ref}']")
assert_equal "2", cell.text
assert_equal "inlineStr", cell.attr("t")
ref = "B2"
assert_equal ref, hyperlinks[1].attr("ref")
cell = doc.at("c[r='#{ref}']")
assert_equal url, cell.text
assert_equal "inlineStr", cell.attr("t")
save_file("xlsx/hyperlinks.xlsx", p)
end
end
end
end