workbook 0.4.2 → 0.4.3
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.
- data/.gitignore +2 -1
- data/.yardoc/checksums +22 -21
- data/.yardoc/object_types +0 -0
- data/.yardoc/objects/root.dat +0 -0
- data/Gemfile.lock +2 -2
- data/doc/RubyXL.html +1 -1
- data/doc/RubyXL/Cell.html +1 -1
- data/doc/RubyXL/Workbook.html +31 -31
- data/doc/Workbook.html +4 -4
- data/doc/Workbook/Book.html +5 -5
- data/doc/Workbook/Cell.html +202 -62
- data/doc/Workbook/Format.html +32 -32
- data/doc/Workbook/Modules.html +1 -1
- data/doc/Workbook/Modules/RawObjectsStorage.html +5 -5
- data/doc/Workbook/Modules/TableDiffSort.html +14 -14
- data/doc/Workbook/Modules/TypeParser.html +7 -7
- data/doc/Workbook/NilValue.html +434 -0
- data/doc/Workbook/Readers.html +3 -3
- data/doc/Workbook/Readers/CsvReader.html +6 -6
- data/doc/Workbook/Readers/OdsReader.html +562 -60
- data/doc/Workbook/Readers/TxtReader.html +2 -2
- data/doc/Workbook/Readers/XlsReader.html +14 -14
- data/doc/Workbook/Readers/XlsShared.html +67 -67
- data/doc/Workbook/Readers/XlsxReader.html +7 -7
- data/doc/Workbook/Row.html +243 -5
- data/doc/Workbook/Sheet.html +2 -2
- data/doc/Workbook/Table.html +669 -58
- data/doc/Workbook/Template.html +5 -5
- data/doc/Workbook/Writers.html +1 -1
- data/doc/Workbook/Writers/CsvTableWriter.html +1 -1
- data/doc/Workbook/Writers/HtmlWriter.html +27 -13
- data/doc/Workbook/Writers/XlsWriter.html +2 -2
- data/doc/_index.html +23 -8
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +16 -12
- data/doc/index.html +16 -12
- data/doc/method_list.html +232 -72
- data/doc/top-level-namespace.html +1 -1
- data/lib/workbook/book.rb +25 -25
- data/lib/workbook/cell.rb +33 -33
- data/lib/workbook/format.rb +10 -10
- data/lib/workbook/modules/raw_objects_storage.rb +19 -19
- data/lib/workbook/modules/table_diff_sort.rb +22 -22
- data/lib/workbook/modules/type_parser.rb +18 -18
- data/lib/workbook/nil_value.rb +6 -6
- data/lib/workbook/readers/csv_reader.rb +8 -8
- data/lib/workbook/readers/ods_reader.rb +92 -46
- data/lib/workbook/readers/txt_reader.rb +2 -2
- data/lib/workbook/readers/xls_reader.rb +19 -19
- data/lib/workbook/readers/xls_shared.rb +70 -70
- data/lib/workbook/readers/xlsx_reader.rb +42 -42
- data/lib/workbook/row.rb +59 -29
- data/lib/workbook/sheet.rb +8 -8
- data/lib/workbook/table.rb +52 -19
- data/lib/workbook/template.rb +10 -10
- data/lib/workbook/version.rb +1 -1
- data/lib/workbook/writers/csv_table_writer.rb +1 -1
- data/lib/workbook/writers/html_writer.rb +6 -6
- data/lib/workbook/writers/xls_writer.rb +12 -12
- data/rbeautify.rb +232 -0
- data/test/artifacts/book_with_colspans.ods +0 -0
- data/test/artifacts/book_with_tabs_and_colours.ods +0 -0
- data/test/test_book.rb +10 -10
- data/test/test_cell.rb +14 -14
- data/test/test_format.rb +11 -11
- data/test/test_functional.rb +3 -3
- data/test/test_modules_table_diff_sort.rb +24 -24
- data/test/test_modules_type_parser.rb +27 -27
- data/test/test_readers_csv_reader.rb +11 -11
- data/test/test_readers_ods_reader.rb +22 -15
- data/test/test_readers_txt_reader.rb +13 -13
- data/test/test_readers_xls_reader.rb +11 -11
- data/test/test_readers_xlsx_reader.rb +5 -5
- data/test/test_row.rb +96 -26
- data/test/test_sheet.rb +9 -9
- data/test/test_table.rb +42 -26
- data/test/test_template.rb +3 -3
- data/test/test_writers_html_writer.rb +6 -4
- data/test/test_writers_xls_writer.rb +7 -7
- metadata +5 -1
@@ -11,17 +11,17 @@ module Workbook
|
|
11
11
|
template.add_raw sp
|
12
12
|
parse_xls sp
|
13
13
|
rescue Ole::Storage::FormatError => e
|
14
|
-
begin
|
14
|
+
begin
|
15
15
|
# Assuming it is a tab separated txt inside .xls
|
16
|
-
open(file_obj.path, 'txt')
|
16
|
+
open(file_obj.path, 'txt')
|
17
17
|
rescue
|
18
18
|
raise e
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
end
|
23
|
-
|
24
|
-
|
23
|
+
|
24
|
+
|
25
25
|
def parse_xls xls_spreadsheet=template.raws[Spreadsheet::Excel::Workbook], options={}
|
26
26
|
options = {:additional_type_parsing=>true}.merge options
|
27
27
|
number_of_worksheets = xls_spreadsheet.worksheets.count
|
@@ -29,16 +29,16 @@ module Workbook
|
|
29
29
|
xls_sheet = xls_spreadsheet.worksheets[si]
|
30
30
|
begin
|
31
31
|
number_of_rows = xls_spreadsheet.worksheets[si].count
|
32
|
-
s = create_or_open_sheet_at(si)
|
32
|
+
s = create_or_open_sheet_at(si)
|
33
33
|
(0..number_of_rows-1).each do |ri|
|
34
34
|
xls_row = xls_sheet.row(ri)
|
35
35
|
r = s.table.create_or_open_row_at(ri)
|
36
36
|
col_widths = xls_sheet.columns.collect{|c| c.width if c}
|
37
37
|
xls_row.each_with_index do |xls_cell,ci|
|
38
|
-
|
38
|
+
|
39
39
|
begin
|
40
|
-
r[ci] = Workbook::Cell.new xls_cell
|
41
|
-
r[ci].parse!
|
40
|
+
r[ci] = Workbook::Cell.new xls_cell
|
41
|
+
r[ci].parse!
|
42
42
|
rescue ArgumentError => e
|
43
43
|
if e.message.match('not a Spreadsheet::Formula')
|
44
44
|
v = xls_cell.value
|
@@ -48,27 +48,27 @@ module Workbook
|
|
48
48
|
end
|
49
49
|
if v.is_a? Spreadsheet::Excel::Error
|
50
50
|
v = "----!"
|
51
|
-
end
|
51
|
+
end
|
52
52
|
r[ci] = Workbook::Cell.new v
|
53
53
|
elsif e.message.match('not a Spreadsheet::Link')
|
54
54
|
r[ci] = Workbook::Cell.new xls_cell.to_s
|
55
55
|
elsif e.message.match('not a Spreadsheet::Link')
|
56
56
|
r[ci] = Workbook::Cell.new xls_cell.to_s
|
57
57
|
elsif e.message.match('not a Spreadsheet::Excel::Error')
|
58
|
-
r[ci] = "._."
|
59
|
-
else
|
58
|
+
r[ci] = "._."
|
59
|
+
else
|
60
60
|
r[ci] = "._." # raise e (we're going to be silent for now)
|
61
61
|
end
|
62
62
|
end
|
63
63
|
xls_format = xls_row.format(ci)
|
64
64
|
col_width = nil
|
65
|
-
|
65
|
+
|
66
66
|
if ri == 0
|
67
67
|
col_width = col_widths[ci]
|
68
68
|
end
|
69
69
|
f = template.create_or_find_format_by "object_id_#{xls_format.object_id}",col_width
|
70
70
|
f[:width]= col_width
|
71
|
-
f[:rotation] = xls_format.rotation if xls_format.rotation
|
71
|
+
f[:rotation] = xls_format.rotation if xls_format.rotation
|
72
72
|
f[:background_color] = xls_color_to_html_hex(xls_format.pattern_fg_color)
|
73
73
|
f[:number_format] = ms_formatting_to_strftime(xls_format.number_format)
|
74
74
|
f[:text_direction] = xls_format.text_direction
|
@@ -77,7 +77,7 @@ module Workbook
|
|
77
77
|
f[:color] = xls_color_to_html_hex(xls_format.font.color)
|
78
78
|
|
79
79
|
f.add_raw xls_format
|
80
|
-
|
80
|
+
|
81
81
|
r[ci].format = f
|
82
82
|
end
|
83
83
|
end
|
@@ -87,13 +87,13 @@ module Workbook
|
|
87
87
|
end
|
88
88
|
end
|
89
89
|
end
|
90
|
-
|
91
|
-
private
|
90
|
+
|
91
|
+
private
|
92
92
|
def xls_color_to_html_hex color_sym
|
93
93
|
Workbook::Book::XLS_COLORS[color_sym] ? Workbook::Book::XLS_COLORS[color_sym] : "#000000"
|
94
94
|
end
|
95
|
-
|
96
|
-
def ms_formatting_to_strftime ms_nr_format
|
95
|
+
|
96
|
+
def ms_formatting_to_strftime ms_nr_format
|
97
97
|
ms_nr_format = ms_nr_format.downcase
|
98
98
|
return nil if ms_nr_format == 'general'
|
99
99
|
ms_nr_format.gsub('yyyy','%Y').gsub('dddd','%A').gsub('mmmm','%B').gsub('ddd','%a').gsub('mmm','%b').gsub('yy','%y').gsub('dd','%d').gsub('mm','%m').gsub('y','%y').gsub('%%y','%y').gsub('d','%e').gsub('%%e','%d').gsub('m','%m').gsub('%%m','%m').gsub(';@','').gsub('\\','')
|
@@ -3,74 +3,74 @@ module Workbook
|
|
3
3
|
module Readers
|
4
4
|
module XlsShared
|
5
5
|
XLS_COLORS = {:xls_color_1=>'#000000',
|
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
|
-
|
6
|
+
:xls_color_2=>'#FFFFFF',
|
7
|
+
:xls_color_3=>'#FF0000',
|
8
|
+
:xls_color_4=>'#00FF00',
|
9
|
+
:xls_color_5=>'#0000FF',
|
10
|
+
:xls_color_6=>'#FFFF00',
|
11
|
+
:xls_color_7=>'#FF00FF',
|
12
|
+
:xls_color_8=>'#00FFFF',
|
13
|
+
:xls_color_9=>'#800000',
|
14
|
+
:xls_color_10=>'#008000',
|
15
|
+
:xls_color_11=>'#000080',
|
16
|
+
:xls_color_12=>'#808000',
|
17
|
+
:xls_color_13=>'#800080',
|
18
|
+
:xls_color_14=>'#008080',
|
19
|
+
:xls_color_15=>'#C0C0C0',
|
20
|
+
:xls_color_16=>'#808080',
|
21
|
+
:xls_color_17=>'#9999FF',
|
22
|
+
:xls_color_18=>'#993366',
|
23
|
+
:xls_color_19=>'#FFFFCC',
|
24
|
+
:xls_color_20=>'#CCFFFF',
|
25
|
+
:xls_color_21=>'#660066',
|
26
|
+
:xls_color_22=>'#FF8080',
|
27
|
+
:xls_color_23=>'#0066CC',
|
28
|
+
:xls_color_24=>'#CCCCFF',
|
29
|
+
:xls_color_25=>'#000080',
|
30
|
+
:xls_color_26=>'#FF00FF',
|
31
|
+
:xls_color_27=>'#FFFF00',
|
32
|
+
:xls_color_28=>'#00FFFF',
|
33
|
+
:xls_color_29=>'#800080',
|
34
|
+
:xls_color_30=>'#800000',
|
35
|
+
:xls_color_31=>'#008080',
|
36
|
+
:xls_color_32=>'#0000FF',
|
37
|
+
:xls_color_33=>'#00CCFF',
|
38
|
+
:xls_color_34=>'#CCFFFF',
|
39
|
+
:xls_color_35=>'#CCFFCC',
|
40
|
+
:xls_color_36=>'#FFFF99',
|
41
|
+
:xls_color_37=>'#99CCFF',
|
42
|
+
:xls_color_38=>'#FF99CC',
|
43
|
+
:xls_color_39=>'#CC99FF',
|
44
|
+
:xls_color_40=>'#FFCC99',
|
45
|
+
:xls_color_41=>'#3366FF',
|
46
|
+
:xls_color_42=>'#33CCCC',
|
47
|
+
:xls_color_43=>'#99CC00',
|
48
|
+
:xls_color_44=>'#FFCC00',
|
49
|
+
:xls_color_45=>'#FF9900',
|
50
|
+
:xls_color_46=>'#FF6600',
|
51
|
+
:xls_color_47=>'#666699',
|
52
|
+
:xls_color_48=>'#969696',
|
53
|
+
:xls_color_49=>'#003366',
|
54
|
+
:xls_color_50=>'#339966',
|
55
|
+
:xls_color_51=>'#003300',
|
56
|
+
:xls_color_52=>'#333300',
|
57
|
+
:xls_color_53=>'#993300',
|
58
|
+
:xls_color_54=>'#993366',
|
59
|
+
:xls_color_55=>'#333399',
|
60
|
+
:xls_color_56=>'#333333',
|
61
|
+
:black=>'#000000',
|
62
|
+
:white=>'#FFFFFF',
|
63
|
+
:red=>'#FF0000',
|
64
|
+
:green=>'#00FF00',
|
65
|
+
:blue=>'#0000FF',
|
66
|
+
:yellow=>'#FFFF00',
|
67
|
+
:magenta=>'#FF00FF',
|
68
|
+
:cyan=>'#00FFFF',
|
69
|
+
:border=>'#FFFFFF',
|
70
|
+
:text=>'#000000',
|
71
|
+
:lime=>'#00f94c'
|
72
|
+
}
|
73
|
+
end
|
74
|
+
end
|
75
75
|
end
|
76
|
-
|
76
|
+
|
@@ -4,7 +4,7 @@ require 'rubyXL'
|
|
4
4
|
# Monkeypatching rubyXL, pull request submitted: https://github.com/gilt/rubyXL/pull/47
|
5
5
|
module RubyXL
|
6
6
|
class Workbook
|
7
|
-
|
7
|
+
|
8
8
|
# Improves upon date format detection
|
9
9
|
def is_date_format?(num_fmt)
|
10
10
|
num_fmt.downcase!
|
@@ -71,36 +71,36 @@ module RubyXL
|
|
71
71
|
def num_fmts_by_id
|
72
72
|
return @num_fmts_hash unless @num_fmts_hash.nil?
|
73
73
|
@num_fmts_hash={1=>{:attributes=>{:formatCode=>'0'}},
|
74
|
-
2=>{:attributes=>{:formatCode=>'0.00'}},
|
75
|
-
3=>{:attributes=>{:formatCode=>'#, ##0'}},
|
76
|
-
4=>{:attributes=>{:formatCode=>'#, ##0.00'}},
|
77
|
-
5=>{:attributes=>{:formatCode=>'$#, ##0_);($#, ##0)'}},
|
78
|
-
6=>{:attributes=>{:formatCode=>'$#, ##0_);[Red]($#, ##0)'}},
|
79
|
-
7=>{:attributes=>{:formatCode=>'$#, ##0.00_);($#, ##0.00)'}},
|
80
|
-
8=>{:attributes=>{:formatCode=>'$#, ##0.00_);[Red]($#, ##0.00)'}},
|
81
|
-
9=>{:attributes=>{:formatCode=>'0%'}},
|
82
|
-
10=>{:attributes=>{:formatCode=>'0.00%'}},
|
83
|
-
11=>{:attributes=>{:formatCode=>'0.00E+00'}},
|
84
|
-
12=>{:attributes=>{:formatCode=>'# ?/?'}},
|
85
|
-
13=>{:attributes=>{:formatCode=>'# ??/??'}},
|
86
|
-
14=>{:attributes=>{:formatCode=>'m/d/yyyy'}},
|
87
|
-
15=>{:attributes=>{:formatCode=>'d-mmm-yy'}},
|
88
|
-
16=>{:attributes=>{:formatCode=>'d-mmm'}},
|
89
|
-
17=>{:attributes=>{:formatCode=>'mmm-yy'}},
|
90
|
-
18=>{:attributes=>{:formatCode=>'h:mm AM/PM'}},
|
91
|
-
19=>{:attributes=>{:formatCode=>'h:mm:ss AM/PM'}},
|
92
|
-
20=>{:attributes=>{:formatCode=>'h:mm'}},
|
93
|
-
21=>{:attributes=>{:formatCode=>'h:mm:ss'}},
|
94
|
-
22=>{:attributes=>{:formatCode=>'m/d/yyyy h:mm'}},
|
95
|
-
37=>{:attributes=>{:formatCode=>'#, ##0_);(#, ##0)'}},
|
96
|
-
38=>{:attributes=>{:formatCode=>'#, ##0_);[Red](#, ##0)'}},
|
97
|
-
39=>{:attributes=>{:formatCode=>'#, ##0.00_);(#, ##0.00)'}},
|
98
|
-
40=>{:attributes=>{:formatCode=>'#, ##0.00_);[Red](#, ##0.00)'}},
|
99
|
-
45=>{:attributes=>{:formatCode=>'mm:ss'}},
|
100
|
-
46=>{:attributes=>{:formatCode=>'[h]:mm:ss'}},
|
101
|
-
47=>{:attributes=>{:formatCode=>'mm:ss.0'}},
|
102
|
-
48=>{:attributes=>{:formatCode=>'##0.0E+0'}},
|
103
|
-
49=>{:attributes=>{:formatCode=>'@'}}}
|
74
|
+
2=>{:attributes=>{:formatCode=>'0.00'}},
|
75
|
+
3=>{:attributes=>{:formatCode=>'#, ##0'}},
|
76
|
+
4=>{:attributes=>{:formatCode=>'#, ##0.00'}},
|
77
|
+
5=>{:attributes=>{:formatCode=>'$#, ##0_);($#, ##0)'}},
|
78
|
+
6=>{:attributes=>{:formatCode=>'$#, ##0_);[Red]($#, ##0)'}},
|
79
|
+
7=>{:attributes=>{:formatCode=>'$#, ##0.00_);($#, ##0.00)'}},
|
80
|
+
8=>{:attributes=>{:formatCode=>'$#, ##0.00_);[Red]($#, ##0.00)'}},
|
81
|
+
9=>{:attributes=>{:formatCode=>'0%'}},
|
82
|
+
10=>{:attributes=>{:formatCode=>'0.00%'}},
|
83
|
+
11=>{:attributes=>{:formatCode=>'0.00E+00'}},
|
84
|
+
12=>{:attributes=>{:formatCode=>'# ?/?'}},
|
85
|
+
13=>{:attributes=>{:formatCode=>'# ??/??'}},
|
86
|
+
14=>{:attributes=>{:formatCode=>'m/d/yyyy'}},
|
87
|
+
15=>{:attributes=>{:formatCode=>'d-mmm-yy'}},
|
88
|
+
16=>{:attributes=>{:formatCode=>'d-mmm'}},
|
89
|
+
17=>{:attributes=>{:formatCode=>'mmm-yy'}},
|
90
|
+
18=>{:attributes=>{:formatCode=>'h:mm AM/PM'}},
|
91
|
+
19=>{:attributes=>{:formatCode=>'h:mm:ss AM/PM'}},
|
92
|
+
20=>{:attributes=>{:formatCode=>'h:mm'}},
|
93
|
+
21=>{:attributes=>{:formatCode=>'h:mm:ss'}},
|
94
|
+
22=>{:attributes=>{:formatCode=>'m/d/yyyy h:mm'}},
|
95
|
+
37=>{:attributes=>{:formatCode=>'#, ##0_);(#, ##0)'}},
|
96
|
+
38=>{:attributes=>{:formatCode=>'#, ##0_);[Red](#, ##0)'}},
|
97
|
+
39=>{:attributes=>{:formatCode=>'#, ##0.00_);(#, ##0.00)'}},
|
98
|
+
40=>{:attributes=>{:formatCode=>'#, ##0.00_);[Red](#, ##0.00)'}},
|
99
|
+
45=>{:attributes=>{:formatCode=>'mm:ss'}},
|
100
|
+
46=>{:attributes=>{:formatCode=>'[h]:mm:ss'}},
|
101
|
+
47=>{:attributes=>{:formatCode=>'mm:ss.0'}},
|
102
|
+
48=>{:attributes=>{:formatCode=>'##0.0E+0'}},
|
103
|
+
49=>{:attributes=>{:formatCode=>'@'}}}
|
104
104
|
if num_fmts and num_fmts[:numFmt]
|
105
105
|
num_fmts[:numFmt].each do |num_fmt|
|
106
106
|
@num_fmts_hash[num_fmt[:attributes][:numFmtId]]=num_fmt
|
@@ -125,7 +125,7 @@ module RubyXL
|
|
125
125
|
end
|
126
126
|
end
|
127
127
|
end
|
128
|
-
# end of monkey patch
|
128
|
+
# end of monkey patch
|
129
129
|
|
130
130
|
module Workbook
|
131
131
|
module Readers
|
@@ -136,25 +136,25 @@ module Workbook
|
|
136
136
|
template.add_raw sp
|
137
137
|
parse_xlsx sp
|
138
138
|
end
|
139
|
-
|
139
|
+
|
140
140
|
def parse_xlsx xlsx_spreadsheet=template.raws[RubyXL::Workbook], options={}
|
141
141
|
options = {:additional_type_parsing=>false}.merge options
|
142
142
|
#number_of_worksheets = xlsx_spreadsheet.worksheets.count
|
143
143
|
xlsx_spreadsheet.worksheets.each_with_index do |worksheet, si|
|
144
|
-
s = create_or_open_sheet_at(si)
|
144
|
+
s = create_or_open_sheet_at(si)
|
145
145
|
col_widths = xlsx_spreadsheet.worksheets.first.cols.collect{|a| a[:attributes][:width].to_f if a[:attributes]}
|
146
146
|
worksheet.each_with_index do |row, ri|
|
147
147
|
r = s.table.create_or_open_row_at(ri)
|
148
|
-
|
148
|
+
|
149
149
|
row.each_with_index do |cell,ci|
|
150
150
|
if cell.nil?
|
151
151
|
r[ci] = Workbook::Cell.new nil
|
152
152
|
else
|
153
|
-
r[ci] = Workbook::Cell.new cell.value
|
154
|
-
r[ci].parse!
|
153
|
+
r[ci] = Workbook::Cell.new cell.value
|
154
|
+
r[ci].parse!
|
155
155
|
xls_format = cell.style_index
|
156
156
|
col_width = nil
|
157
|
-
|
157
|
+
|
158
158
|
if ri == 0
|
159
159
|
col_width = col_widths[ci]
|
160
160
|
end
|
@@ -166,16 +166,16 @@ module Workbook
|
|
166
166
|
f[:color] = "##{cell.font_color}"
|
167
167
|
|
168
168
|
f.add_raw xls_format
|
169
|
-
|
169
|
+
|
170
170
|
r[ci].format = f
|
171
171
|
end
|
172
172
|
end
|
173
173
|
end
|
174
174
|
end
|
175
175
|
end
|
176
|
-
|
177
|
-
def ms_formatting_to_strftime ms_nr_format
|
178
|
-
if ms_nr_format
|
176
|
+
private
|
177
|
+
def ms_formatting_to_strftime ms_nr_format
|
178
|
+
if ms_nr_format
|
179
179
|
ms_nr_format = ms_nr_format.downcase
|
180
180
|
return nil if ms_nr_format == 'general'
|
181
181
|
ms_nr_format.gsub('yyyy','%Y').gsub('dddd','%A').gsub('mmmm','%B').gsub('ddd','%a').gsub('mmm','%b').gsub('yy','%y').gsub('dd','%d').gsub('mm','%m').gsub('y','%y').gsub('%%y','%y').gsub('d','%e').gsub('%%e','%d').gsub('m','%m').gsub('%%m','%m').gsub(';@','').gsub('\\','')
|
data/lib/workbook/row.rb
CHANGED
@@ -4,17 +4,17 @@ module Workbook
|
|
4
4
|
alias_method :compare_without_header, :<=>
|
5
5
|
attr_accessor :placeholder # The placeholder attribute is used in compares (corresponds to newly created or removed lines (depending which side you're on)
|
6
6
|
attr_accessor :format
|
7
|
-
|
7
|
+
|
8
8
|
# Initialize a new row
|
9
9
|
#
|
10
10
|
# @param [Workbook::Row, Array<Workbook::Cell>, Array] cells list of cells to initialize the row with, default is empty
|
11
11
|
# @param [Workbook::Table] table a row normally belongs to a table, reference it here
|
12
12
|
# @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
13
|
def initialize cells=[], table=nil, options={}
|
14
|
-
options=options ? {:parse_cells_on_batch_creation=>false,:cell_parse_options=>{}}.merge(options) : {}
|
14
|
+
options=options ? {:parse_cells_on_batch_creation=>false,:cell_parse_options=>{}}.merge(options) : {}
|
15
15
|
cells = [] if cells==nil
|
16
16
|
self.table= table
|
17
|
-
cells.each do |c|
|
17
|
+
cells.each do |c|
|
18
18
|
if c.is_a? Workbook::Cell
|
19
19
|
c = c
|
20
20
|
else
|
@@ -24,28 +24,28 @@ module Workbook
|
|
24
24
|
push c
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
# An internal function used in diffs
|
29
|
-
#
|
30
|
-
# @return [Boolean] returns true when this row is not an actual row, but a placeholder row to 'compare' against
|
29
|
+
#
|
30
|
+
# @return [Boolean] returns true when this row is not an actual row, but a placeholder row to 'compare' against
|
31
31
|
def placeholder?
|
32
32
|
placeholder ? true : false
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
# Returns the table this row belongs to
|
36
36
|
#
|
37
37
|
# @return [Workbook::Table] the table this row belongs to
|
38
38
|
def table
|
39
39
|
@table
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
# Set reference to the table this row belongs to without adding the row to the table
|
43
43
|
#
|
44
44
|
# @param [Workbook::Table] t the table this row belongs to
|
45
45
|
def set_table(t)
|
46
46
|
@table = t
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
# Set reference to the table this row belongs to and add the row to this table
|
50
50
|
#
|
51
51
|
# @param [Workbook::Table] t the table this row belongs to
|
@@ -56,7 +56,7 @@ module Workbook
|
|
56
56
|
table.push(self) #unless table.index(self) and self.placeholder?
|
57
57
|
end
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
60
|
# Overrides normal Array's []-function with support for symbols that identify a column based on the header-values
|
61
61
|
#
|
62
62
|
# @example Lookup using fixnum or header value encoded as symbol
|
@@ -73,7 +73,7 @@ module Workbook
|
|
73
73
|
rescue NoMethodError
|
74
74
|
end
|
75
75
|
return rv
|
76
|
-
else
|
76
|
+
else
|
77
77
|
if index_or_hash
|
78
78
|
return to_a[index_or_hash]
|
79
79
|
end
|
@@ -87,27 +87,27 @@ module Workbook
|
|
87
87
|
# row[:a] #=> <Cell value="a">
|
88
88
|
#
|
89
89
|
# @param [Fixnum, Symbol] index_or_hash
|
90
|
-
# @param [String, Fixnum, NilClass, Date, DateTime, Time, Float] value
|
90
|
+
# @param [String, Fixnum, NilClass, Date, DateTime, Time, Float] value
|
91
91
|
# @return [Workbook::Cell, nil]
|
92
92
|
def []= (index_or_hash, value)
|
93
93
|
index = index_or_hash
|
94
94
|
if index_or_hash.is_a? Symbol
|
95
95
|
index = table_header_keys.index(index_or_hash)
|
96
96
|
end
|
97
|
-
|
97
|
+
|
98
98
|
value_celled = Workbook::Cell.new
|
99
99
|
if value.is_a? Workbook::Cell
|
100
100
|
value_celled = value
|
101
101
|
else
|
102
102
|
current_cell = self[index]
|
103
103
|
if current_cell.is_a? Workbook::Cell
|
104
|
-
value_celled = current_cell
|
104
|
+
value_celled = current_cell
|
105
105
|
end
|
106
106
|
value_celled.value=(value)
|
107
107
|
end
|
108
108
|
super(index,value_celled)
|
109
109
|
end
|
110
|
-
|
110
|
+
|
111
111
|
# Returns an array of cells allows you to find cells by a given color, normally a string containing a hex
|
112
112
|
#
|
113
113
|
# @param [String] color a CSS-style hex-string
|
@@ -119,44 +119,44 @@ module Workbook
|
|
119
119
|
r = Row.new cells
|
120
120
|
options[:hash_keys] ? r.to_symbols : r
|
121
121
|
end
|
122
|
-
|
123
|
-
# Returns true when the row belongs to a table and it is the header row (typically the first row)
|
122
|
+
|
123
|
+
# Returns true when the row belongs to a table and it is the header row (typically the first row)
|
124
124
|
#
|
125
125
|
# @return [Boolean]
|
126
126
|
def header?
|
127
127
|
table != nil and self.object_id == table.header.object_id
|
128
128
|
end
|
129
|
-
|
129
|
+
|
130
130
|
# Is this the first row in the table
|
131
|
-
#
|
131
|
+
#
|
132
132
|
# @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.
|
133
133
|
def first?
|
134
134
|
table != nil and self.object_id == table.first.object_id
|
135
135
|
end
|
136
|
-
|
136
|
+
|
137
137
|
# Returns true when all the cells in the row have values whose to_s value equals an empty string
|
138
138
|
#
|
139
139
|
# @return [Boolean]
|
140
140
|
def no_values?
|
141
141
|
all? {|c| c.value.to_s == ''}
|
142
142
|
end
|
143
|
-
|
143
|
+
|
144
144
|
# Converts a row to an array of symbol representations of the row content, see also: Workbook::Cell#to_sym
|
145
145
|
# @return [Array<Symbol>] returns row as an array of symbols
|
146
146
|
def to_symbols
|
147
147
|
collect{|c| c.to_sym}
|
148
148
|
end
|
149
|
-
|
149
|
+
|
150
150
|
# Converts the row to an array of Workbook::Cell's
|
151
151
|
# @return [Array<Workbook::Cell>] returns row as an array of symbols
|
152
152
|
def to_a
|
153
153
|
self.collect{|c| c}
|
154
154
|
end
|
155
|
-
|
155
|
+
|
156
156
|
def table_header_keys
|
157
157
|
table.header.to_symbols
|
158
158
|
end
|
159
|
-
|
159
|
+
|
160
160
|
# Returns a hash representation of this row
|
161
161
|
#
|
162
162
|
# @return [Hash]
|
@@ -168,36 +168,66 @@ module Workbook
|
|
168
168
|
keys.each_with_index {|k,i| @hash[k]=values[i]}
|
169
169
|
return @hash
|
170
170
|
end
|
171
|
-
|
171
|
+
|
172
172
|
# Compares one row wiht another
|
173
173
|
#
|
174
174
|
# @param [Workbook::Row] other row to compare against
|
175
|
-
# @return [Workbook::Row] a row with the diff result.
|
175
|
+
# @return [Workbook::Row] a row with the diff result.
|
176
176
|
def <=> other
|
177
177
|
a = self.header? ? 0 : 1
|
178
178
|
b = other.header? ? 0 : 1
|
179
179
|
return (a <=> b) if (a==0 or b==0)
|
180
180
|
compare_without_header other
|
181
181
|
end
|
182
|
-
|
182
|
+
|
183
183
|
# The first cell of the row is considered to be the key
|
184
184
|
#
|
185
185
|
# @return [Workbook::Cell] the key cell
|
186
186
|
def key
|
187
187
|
first
|
188
188
|
end
|
189
|
-
|
189
|
+
|
190
190
|
# Compact detaches the row from the table
|
191
191
|
def compact
|
192
192
|
r = self.clone
|
193
193
|
r = r.collect{|c| c unless c.nil?}.compact
|
194
194
|
end
|
195
|
-
|
195
|
+
|
196
196
|
# clone the row with together with the cells
|
197
197
|
#
|
198
198
|
# @return [Workbook::Row] a cloned copy of self with cells
|
199
199
|
def clone
|
200
200
|
Workbook::Row.new(to_a.collect{|c| c.clone})
|
201
201
|
end
|
202
|
+
|
203
|
+
# remove all the trailing nil-cells (returning a trimmed clone)
|
204
|
+
#
|
205
|
+
# @param [Integer] desired_length of the new row
|
206
|
+
# @return [Workbook::Row] a trimmed clone of the array
|
207
|
+
def trim(desired_length=nil)
|
208
|
+
self.clone.trim!(desired_length)
|
209
|
+
end
|
210
|
+
|
211
|
+
# remove all the trailing nil-cells (returning a trimmed self)
|
212
|
+
#
|
213
|
+
# @param [Integer] desired_length of the new row
|
214
|
+
# @return [Workbook::Row] self
|
215
|
+
def trim!(desired_length=nil)
|
216
|
+
self_count = self.count-1
|
217
|
+
self.count.times do |index|
|
218
|
+
index = self_count - index
|
219
|
+
if desired_length and index < desired_length
|
220
|
+
break
|
221
|
+
elsif desired_length and index >= desired_length
|
222
|
+
self.delete_at(index)
|
223
|
+
elsif self[index].nil?
|
224
|
+
self.delete_at(index)
|
225
|
+
else
|
226
|
+
break
|
227
|
+
end
|
228
|
+
end
|
229
|
+
(desired_length - self.count).times{|a| self << (Workbook::Cell.new(nil))} if desired_length and (desired_length - self.count) > 0
|
230
|
+
self
|
231
|
+
end
|
202
232
|
end
|
203
233
|
end
|