forked from westonganger/spreadsheet_architect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex_xlsx_styling.rb
52 lines (43 loc) · 1.34 KB
/
complex_xlsx_styling.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
headers = [
['Latest Posts'],
['Title','Category','Author','Posted on','Earnings']
]
data = @posts.map{|x| [x.title, x.category.name, x.author.name, x.created_at, x.earnings]}
header_style = {}
row_style = {}
column_styles = [
{columns: 0, styles: {bold: true}},
{columns: (1..3), styles: {format_code: "$#,##0.00"}},
{columns: [4,9], include_header: true, styles: {italic: true}}
]
range_styles = [
{range: "B2:C4", styles: {background_color: "CCCCCC"}},
{range: {rows: 1, columns: :all}, styles: {bold: true}},
{range: {rows: (0..5), columns: (1..5)}, styles: {italic: true}},
{range: {rows: :all, columns: (3..5)}, styles: {color: "FFFFFF"}}
]
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: 0, columns: :all}},
{range: {rows: (0..1), columns: (0..3)}}
]
file_data = SpreadsheetArchitect.to_xlsx({
headers: headers,
data: data,
header_style: header_style,
row_style: row_style,
column_styles: column_styles,
range_styles: range_styles,
borders: borders,
merges: merges
})
# Save the file_data
File.open('path/to/file.xlsx', 'w+b') do |f|
f.write file_data
end