workbook 0.8.0 → 0.8.2

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.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +21 -0
  3. data/.gitignore +4 -1
  4. data/.ruby-version +1 -1
  5. data/.travis.yml +4 -4
  6. data/CHANGELOG.md +16 -0
  7. data/Gemfile +4 -2
  8. data/README.md +9 -7
  9. data/Rakefile +9 -7
  10. data/lib/workbook/book.rb +74 -61
  11. data/lib/workbook/cell.rb +59 -12
  12. data/lib/workbook/column.rb +33 -29
  13. data/lib/workbook/format.rb +24 -23
  14. data/lib/workbook/generatetypes.rb +6 -5
  15. data/lib/workbook/modules/cache.rb +8 -7
  16. data/lib/workbook/modules/cell.rb +78 -99
  17. data/lib/workbook/modules/diff_sort.rb +93 -82
  18. data/lib/workbook/modules/raw_objects_storage.rb +7 -7
  19. data/lib/workbook/modules/type_parser.rb +31 -21
  20. data/lib/workbook/nil_value.rb +5 -9
  21. data/lib/workbook/readers/csv_reader.rb +7 -9
  22. data/lib/workbook/readers/ods_reader.rb +49 -49
  23. data/lib/workbook/readers/txt_reader.rb +7 -7
  24. data/lib/workbook/readers/xls_reader.rb +22 -32
  25. data/lib/workbook/readers/xls_shared.rb +107 -116
  26. data/lib/workbook/readers/xlsx_reader.rb +47 -46
  27. data/lib/workbook/row.rb +100 -83
  28. data/lib/workbook/sheet.rb +48 -37
  29. data/lib/workbook/table.rb +97 -71
  30. data/lib/workbook/template.rb +13 -14
  31. data/lib/workbook/types/date.rb +2 -1
  32. data/lib/workbook/types/false.rb +1 -1
  33. data/lib/workbook/types/false_class.rb +2 -1
  34. data/lib/workbook/types/nil.rb +1 -1
  35. data/lib/workbook/types/nil_class.rb +3 -2
  36. data/lib/workbook/types/numeric.rb +3 -2
  37. data/lib/workbook/types/string.rb +3 -2
  38. data/lib/workbook/types/time.rb +3 -2
  39. data/lib/workbook/types/true.rb +1 -1
  40. data/lib/workbook/types/true_class.rb +3 -2
  41. data/lib/workbook/version.rb +3 -2
  42. data/lib/workbook/writers/csv_table_writer.rb +11 -12
  43. data/lib/workbook/writers/html_writer.rb +35 -37
  44. data/lib/workbook/writers/json_table_writer.rb +9 -10
  45. data/lib/workbook/writers/xls_writer.rb +31 -35
  46. data/lib/workbook/writers/xlsx_writer.rb +46 -28
  47. data/lib/workbook.rb +17 -14
  48. data/test/helper.rb +8 -5
  49. data/test/test_book.rb +43 -38
  50. data/test/test_column.rb +29 -25
  51. data/test/test_format.rb +53 -55
  52. data/test/test_functional.rb +9 -8
  53. data/test/test_modules_cache.rb +20 -17
  54. data/test/test_modules_cell.rb +49 -46
  55. data/test/test_modules_table_diff_sort.rb +56 -63
  56. data/test/test_modules_type_parser.rb +63 -31
  57. data/test/test_readers_csv_reader.rb +50 -42
  58. data/test/test_readers_ods_reader.rb +30 -31
  59. data/test/test_readers_txt_reader.rb +23 -23
  60. data/test/test_readers_xls_reader.rb +22 -23
  61. data/test/test_readers_xls_shared.rb +5 -4
  62. data/test/test_readers_xlsx_reader.rb +46 -37
  63. data/test/test_row.rb +107 -109
  64. data/test/test_sheet.rb +43 -35
  65. data/test/test_table.rb +84 -60
  66. data/test/test_template.rb +18 -15
  67. data/test/test_types_date.rb +7 -7
  68. data/test/test_writers_csv_writer.rb +24 -0
  69. data/test/test_writers_html_writer.rb +44 -41
  70. data/test/test_writers_json_writer.rb +11 -9
  71. data/test/test_writers_xls_writer.rb +52 -35
  72. data/test/test_writers_xlsx_writer.rb +64 -34
  73. data/workbook.gemspec +26 -27
  74. metadata +93 -27
@@ -1,32 +1,36 @@
1
- # -*- encoding : utf-8 -*-
2
1
  # frozen_string_literal: true
2
+
3
3
  module Workbook
4
4
  module Readers
5
5
  module OdsReader
6
6
  # reads self with and ods-type content.xml
7
7
  # @param [String,File] file_obj a file or file reference
8
8
  # @return [Workbook::Book] self
9
- def load_ods file_obj, options={}
9
+ def load_ods file_obj, options = {}
10
10
  file_obj = file_obj.path if file_obj.is_a? File
11
11
  content = ""
12
12
  styles = ""
13
+
13
14
  Zip::File.open(file_obj) do |zipfile|
14
15
  zipfile.entries.each do |file|
15
16
  styles = zipfile.read(file.name) if file.name == "styles.xml"
16
17
  content = zipfile.read(file.name) if file.name == "content.xml"
17
18
  end
18
19
  end
20
+
19
21
  content = Nokogiri.XML(content)
20
22
  styles = Nokogiri.XML(styles)
23
+
21
24
  template.add_raw content
22
25
  parse_ods_style styles
23
26
  parse_ods content, options
24
- return self
27
+
28
+ self
25
29
  end
26
30
 
27
31
  def set_format_property format, property, value
28
32
  value.strip!
29
- format[property] = value if value and value != ""
33
+ format[property] = value if value && (value != "")
30
34
  end
31
35
 
32
36
  def parse_ods_style parse_ods_style
@@ -35,14 +39,14 @@ module Workbook
35
39
  if style_family == "table-cell"
36
40
  format = Workbook::Format.new
37
41
  format.name = style.xpath("@style:name").to_s
38
- format.parent = self.template.formats[style.xpath("@style:parent-style-name").to_s]
42
+ format.parent = template.formats[style.xpath("@style:parent-style-name").to_s]
39
43
  set_format_property format, :border, style.xpath("style:table-cell-properties/@fo:border").to_s
40
- set_format_property format, :vertical_align, style.xpath("style:table-cell-properties/@style:vertical-align").to_s.gsub("automatic","auto")
41
- set_format_property format, :padding, style.xpath("style:table-cell-properties/@fo:padding").to_s.gsub("automatic","auto")
44
+ set_format_property format, :vertical_align, style.xpath("style:table-cell-properties/@style:vertical-align").to_s.gsub("automatic", "auto")
45
+ set_format_property format, :padding, style.xpath("style:table-cell-properties/@fo:padding").to_s.gsub("automatic", "auto")
42
46
  set_format_property format, :font, style.xpath("style:text-properties/style:font-name").to_s + " " + style.xpath("style:text-properties/fo:font-size").to_s + " " + style.xpath("style:text-properties/fo:font-weight").to_s
43
47
  set_format_property format, :color, style.xpath("style:text-properties/@fo:color").to_s
44
48
  set_format_property format, :background_color, style.xpath("style:table-cell-properties/@fo:background-color").to_s
45
- self.template.add_format(format)
49
+ template.add_format(format)
46
50
  end
47
51
  end
48
52
  end
@@ -50,105 +54,101 @@ module Workbook
50
54
  # updates self with and ods-type content.xml
51
55
  # @param [Nokogiri::XML::Document] ods_spreadsheet nokogirified content.xml
52
56
  # @return [Workbook::Book] self
53
- def parse_ods ods_spreadsheet=template.raws[Nokogiri::XML::Document], options={}
54
- require 'cgi'
57
+ def parse_ods ods_spreadsheet = template.raws[Nokogiri::XML::Document], options = {}
58
+ require "cgi"
55
59
 
56
- options = {:additional_type_parsing=>false}.merge options
57
- # styles
58
- #puts ods_spreadsheet
59
60
  parse_ods_style ods_spreadsheet
60
61
 
61
- # data
62
- ods_spreadsheet.xpath("//office:body/office:spreadsheet").each_with_index do |sheet,sheetindex|
63
- workbook_sheet = self.create_or_open_sheet_at(sheetindex)
64
- sheet.xpath("table:table").each_with_index do |table,tableindex|
65
- parse_local_table(workbook_sheet,table,tableindex)
62
+ ods_spreadsheet.xpath("//office:body/office:spreadsheet").each_with_index do |sheet, sheetindex|
63
+ workbook_sheet = create_or_open_sheet_at(sheetindex)
64
+ sheet.xpath("table:table").each_with_index do |table, tableindex|
65
+ parse_local_table(workbook_sheet, table, tableindex)
66
66
  end
67
67
  end
68
- return self
68
+ self
69
69
  end
70
70
 
71
- #parse the contents of an entire table by parsing every row in it and adding it to the table
72
- def parse_local_table(sheet,table,tableindex)
73
- local_table = sheet.create_or_open_table_at(tableindex)
71
+ # parse the contents of an entire table by parsing every row in it and adding it to the table
72
+ def parse_local_table(sheet, table, tableindex)
73
+ local_table = sheet[tableindex]
74
74
  local_table.name = table.xpath("@table:name").to_s
75
- #column_count = get_column_count(table)
75
+
76
76
  table.xpath("table:table-row").each do |row|
77
77
  local_table << parse_local_row(row)
78
78
  end
79
+
79
80
  local_table.trim!
80
81
  end
81
82
 
82
- #set column count
83
+ # set column count
83
84
  def get_column_count(table)
84
85
  first_row = table.xpath("table:table-row").first
85
86
  cells = first_row.xpath("table:table-cell|table:covered-table-cell")
86
87
  column_count = 0
87
88
  cells.each do |cell|
88
- if cell.xpath('@table:number-columns-spanned').children.size>0
89
- column_count +=cell.xpath('@table:number-columns-spanned').children[0].inner_text.to_i
89
+ column_count += if cell.xpath("@table:number-columns-spanned").children.size > 0
90
+ cell.xpath("@table:number-columns-spanned").children[0].inner_text.to_i
90
91
  else
91
- column_count +=1
92
+ 1
92
93
  end
93
94
  end
94
95
  column_count
95
96
  end
96
97
 
97
- #parse the contents of an entire row by parsing every cell in it and adding it to the row
98
+ # parse the contents of an entire row by parsing every cell in it and adding it to the row
98
99
  def parse_local_row(row)
99
100
  cells = row.xpath("table:table-cell|table:covered-table-cell")
100
101
  workbook_row = Workbook::Row.new
101
102
  cells.each do |cell|
102
103
  @cell = cell
103
- repeat = get_repeat
104
- workbook_cell = Workbook::Cell.new()
104
+ repeat = cell_repeat
105
+ workbook_cell = Workbook::Cell.new
105
106
  workbook_cell.value = @cell.nil? ? nil : parse_local_cell(workbook_cell)
106
107
  repeat.times do
107
108
  workbook_row << workbook_cell
108
109
  end
109
110
  end
110
- return workbook_row
111
+ workbook_row
111
112
  end
112
113
 
113
- def get_repeat
114
- pre_set = @cell.xpath('@table:number-columns-repeated').to_s
115
- return 1 if (pre_set.nil? || pre_set=="") # if not present, don't repeat.
116
- return 1 unless "#{pre_set.to_i}"=="#{pre_set}" # return 1 if it's not a valid integer
114
+ def cell_repeat
115
+ pre_set = @cell.xpath("@table:number-columns-repeated").to_s
116
+ return 1 if pre_set.nil? || pre_set == "" # if not present, don't repeat.
117
+ return 1 unless pre_set.to_i.to_s == pre_set.to_s # return 1 if it's not a valid integer
117
118
  return 1 if pre_set.to_i < 1 # return 1, negative repeats make no sense
118
- return pre_set.to_i
119
+ pre_set.to_i
119
120
  end
120
121
 
121
- #parse the contents of a single cell
122
+ # parse the contents of a single cell
122
123
  def parse_local_cell(workbook_cell)
123
124
  return Workbook::NilValue.new(:covered) if @cell.name == "covered-table-cell"
124
- set_cell_attributes(workbook_cell)
125
- valuetype = @cell.xpath('@office:value-type').to_s
125
+ configure_cell_attributes(workbook_cell)
126
+ valuetype = @cell.xpath("@office:value-type").to_s
126
127
  parse_local_value(valuetype)
127
128
  end
128
129
 
129
130
  # Sets cell attributes for rowspan, colspan and format
130
- def set_cell_attributes(workbook_cell)
131
- workbook_cell.format = self.template.formats[@cell.xpath('@table:style-name').to_s]
132
- workbook_cell.colspan= @cell.xpath('@table:number-columns-spanned').to_s
133
- workbook_cell.rowspan= @cell.xpath('@table:number-rows-spanned').to_s
131
+ def configure_cell_attributes(workbook_cell)
132
+ workbook_cell.format = template.formats[@cell.xpath("@table:style-name").to_s]
133
+ workbook_cell.colspan = @cell.xpath("@table:number-columns-spanned").to_s
134
+ workbook_cell.rowspan = @cell.xpath("@table:number-rows-spanned").to_s
134
135
  end
135
136
 
136
137
  # Sets value in right context type
137
138
  def parse_local_value(valuetype)
138
139
  value = CGI.unescapeHTML(@cell.xpath("text:p//text()").to_s)
139
- value = (value=="") ? nil : value
140
+ value = value == "" ? nil : value
140
141
  case valuetype
141
- when 'integer'
142
+ when "integer"
142
143
  value = @cell.xpath("@office:value").to_s.to_i
143
- when 'float'
144
+ when "float"
144
145
  value = @cell.xpath("@office:value").to_s.to_f
145
- value = value.to_i unless @cell.xpath("@office:value").to_s.match(/\./) #sadly most integers are typed as floats...
146
- when 'date'
146
+ value = value.to_i unless /\./.match?(@cell.xpath("@office:value").to_s) # sadly most integers are typed as floats...
147
+ when "date"
147
148
  value = DateTime.parse(@cell.xpath("@office:date-value").to_s)
148
149
  end
149
150
  value
150
151
  end
151
-
152
152
  end
153
153
  end
154
154
  end
@@ -1,17 +1,17 @@
1
- # -*- encoding : utf-8 -*-
2
1
  # frozen_string_literal: true
2
+ # frozen_string_literal: true
3
+
3
4
  module Workbook
4
5
  module Readers
5
6
  module TxtReader
6
- def load_txt text, options={}
7
+ def load_txt text, options = {}
7
8
  csv = text
8
- parse_txt csv, options
9
+ parse_txt(csv, options)
9
10
  end
10
11
 
11
- def parse_txt csv_raw, options={}
12
- csv = []
13
- csv_raw.split("\n").each {|l| csv << CSV.parse_line(l,{:col_sep=>"\t"});nil}
14
- self[0]=Workbook::Sheet.new(csv,self,{:parse_cells_on_batch_creation=>true, :cell_parse_options=>{:detect_date=>true}}) unless sheet.has_contents?
12
+ def parse_txt(csv_raw, options = {})
13
+ csv = csv_raw.split("\n").collect { |l| CSV.parse_line(l, col_sep: "\t") }
14
+ self[0] = Workbook::Sheet.new(csv, self, parse_cells_on_batch_creation: true, cell_parse_options: {detect_date: true}) unless sheet.has_contents?
15
15
  end
16
16
  end
17
17
  end
@@ -1,7 +1,8 @@
1
- # -*- encoding : utf-8 -*-
2
1
  # frozen_string_literal: true
3
- require 'spreadsheet'
4
- require 'workbook/readers/xls_shared'
2
+ # frozen_string_literal: true
3
+
4
+ require "spreadsheet"
5
+ require "workbook/readers/xls_shared"
5
6
 
6
7
  module Workbook
7
8
  module Readers
@@ -9,20 +10,11 @@ module Workbook
9
10
  include Workbook::Readers::XlsShared
10
11
 
11
12
  def load_xls file_obj, options
12
- begin
13
- sp = Spreadsheet.open(file_obj, 'rb')
14
- template.add_raw sp
15
- parse_xls sp, options
16
- rescue Ole::Storage::FormatError
17
- begin
18
- # Assuming it is a tab separated txt inside .xls
19
- import(file_obj.path, 'txt')
20
- rescue Exception => ef
21
-
22
- raise ef
23
- end
24
- end
25
-
13
+ sp = Spreadsheet.open(file_obj, "rb")
14
+ template.add_raw sp
15
+ parse_xls sp, options
16
+ rescue Ole::Storage::FormatError
17
+ import(file_obj.path, "txt")
26
18
  end
27
19
 
28
20
  def parse_xls_cell xls_cell, xls_row, ci
@@ -31,9 +23,9 @@ module Workbook
31
23
  rv = Workbook::Cell.new xls_cell
32
24
  rv.parse!
33
25
  rescue ArgumentError => e
34
- if e.message.match('not a Spreadsheet::Formula')
26
+ if e.message.match?("not a Spreadsheet::Formula")
35
27
  v = xls_cell.value
36
- if v.class == Float and xls_row.format(ci).date?
28
+ if v.instance_of?(Float) && xls_row.format(ci).date?
37
29
  xls_row[ci] = v
38
30
  v = xls_row.datetime(ci)
39
31
  end
@@ -41,14 +33,12 @@ module Workbook
41
33
  v = "----!"
42
34
  end
43
35
  rv = Workbook::Cell.new v
44
- elsif e.message.match('not a Spreadsheet::Link')
36
+ elsif e.message.match?("not a Spreadsheet::Link")
45
37
  rv = Workbook::Cell.new xls_cell.to_s
46
- elsif e.message.match('not a Spreadsheet::Link')
47
- rv = Workbook::Cell.new xls_cell.to_s
48
- elsif e.message.match('not a Spreadsheet::Excel::Error')
38
+ elsif e.message.match?("not a Spreadsheet::Excel::Error")
49
39
  rv = "._."
50
40
  else
51
- rv = "._." # raise e (we're going to be silent for now)
41
+ rv = "._." # raise e (we're going to be silent for now)
52
42
  end
53
43
  end
54
44
  rv
@@ -62,8 +52,8 @@ module Workbook
62
52
  col_width = col_widths[ci]
63
53
  end
64
54
 
65
- f = template.create_or_find_format_by "object_id_#{xls_format.object_id}",col_width
66
- f[:width]= col_width
55
+ f = template.create_or_find_format_by "object_id_#{xls_format.object_id}", col_width
56
+ f[:width] = col_width
67
57
  f[:rotation] = xls_format.rotation if xls_format.rotation
68
58
  f[:background_color] = xls_color_to_html_hex(xls_format.pattern_fg_color)
69
59
  f[:number_format] = ms_formatting_to_strftime(xls_format.number_format)
@@ -79,15 +69,14 @@ module Workbook
79
69
  def parse_xls_row ri, s, xls_sheet
80
70
  xls_row = xls_sheet.row(ri)
81
71
  r = s.table.create_or_open_row_at(ri)
82
- col_widths = xls_sheet.columns.collect{|c| c.width if c}
83
- xls_row.each_with_index do |xls_cell,ci|
72
+ col_widths = xls_sheet.columns.collect { |c| c&.width }
73
+ xls_row.each_with_index do |xls_cell, ci|
84
74
  r[ci] = parse_xls_cell xls_cell, xls_row, ci
85
75
  r[ci].format = parse_xls_format xls_row, ci, ri, col_widths
86
76
  end
87
77
  end
88
78
 
89
- def parse_xls xls_spreadsheet=template.raws[Spreadsheet::Excel::Workbook], options={}
90
- options = {:additional_type_parsing=>true}.merge options
79
+ def parse_xls xls_spreadsheet = template.raws[Spreadsheet::Excel::Workbook], options = {}
91
80
  number_of_worksheets = xls_spreadsheet.worksheets.count
92
81
  number_of_worksheets.times do |si|
93
82
  xls_sheet = xls_spreadsheet.worksheets[si]
@@ -101,15 +90,16 @@ module Workbook
101
90
  end
102
91
  rescue TypeError
103
92
  puts "WARNING: Failed at worksheet (#{si})... ignored"
104
- #ignore SpreadsheetGem can be buggy...
93
+ # ignore SpreadsheetGem can be buggy...
105
94
  end
106
95
  end
107
96
  end
108
97
  end
109
98
 
110
99
  private
100
+
111
101
  def xls_color_to_html_hex color_sym
112
- Workbook::Book::XLS_COLORS[color_sym] ? Workbook::Book::XLS_COLORS[color_sym] : "#000000"
102
+ Workbook::Book::XLS_COLORS[color_sym] || "#000000"
113
103
  end
114
104
  end
115
105
  end
@@ -1,11 +1,11 @@
1
- # -*- encoding : utf-8 -*-
2
1
  # frozen_string_literal: true
3
- require 'date'
2
+ # frozen_string_literal: true
3
+
4
+ require "date"
4
5
 
5
6
  module Workbook
6
7
  module Readers
7
8
  module XlsShared
8
-
9
9
  # Converts standard (ruby/C++/unix/...) strftime formatting to MS's formatting
10
10
  #
11
11
  # @param [String, nil] ms_nr_format (nil returns nil)
@@ -14,26 +14,26 @@ module Workbook
14
14
  ms_nr_format = num_fmt_id_to_ms_formatting(ms_nr_format) if ms_nr_format.is_a? Integer
15
15
  if ms_nr_format
16
16
  ms_nr_format = ms_nr_format.to_s.downcase
17
- return nil if ms_nr_format == 'general' or ms_nr_format == ""
17
+ return nil if (ms_nr_format == "general") || (ms_nr_format == "")
18
18
  translation_table = {
19
- 'yyyy'=>'%Y',
20
- 'dddd'=>'%A',
21
- 'mmmm'=>'%B',
22
- 'ddd'=>'%a',
23
- 'mmm'=>'%b',
24
- 'yy'=>'%y',
25
- 'dd'=>'%d',
26
- 'mm'=>'%m',
27
- 'y'=>'%y',
28
- '%%y'=>'%y',
29
- 'd'=>'%e',
30
- '%%e'=>'%d',
31
- 'm'=>'%m',
32
- '%%m'=>'%m',
33
- ';@'=>'',
34
- '\\'=>''
19
+ "yyyy" => "%Y",
20
+ "dddd" => "%A",
21
+ "mmmm" => "%B",
22
+ "ddd" => "%a",
23
+ "mmm" => "%b",
24
+ "yy" => "%y",
25
+ "dd" => "%d",
26
+ "mm" => "%m",
27
+ "y" => "%y",
28
+ "%%y" => "%y",
29
+ "d" => "%e",
30
+ "%%e" => "%d",
31
+ "m" => "%m",
32
+ "%%m" => "%m",
33
+ ";@" => "",
34
+ "\\" => ""
35
35
  }
36
- translation_table.each{|k,v| ms_nr_format.gsub!(k,v) }
36
+ translation_table.each { |k, v| ms_nr_format.gsub!(k, v) }
37
37
  ms_nr_format
38
38
  end
39
39
  end
@@ -43,37 +43,30 @@ module Workbook
43
43
  # @return [String] number format (excel markup)
44
44
  def num_fmt_id_to_ms_formatting num_fmt_id
45
45
  # from: https://stackoverflow.com/questions/4730152/what-indicates-an-office-open-xml-cell-contains-a-date-time-value
46
- {'0'=>nil, '1'=>'0', '2'=>'0.00', '3'=>'#,##0', '4'=>'#,##0.00',
47
- '9'=>'0%', '10'=>'0.00%', '11'=>'0.00E+00', '12'=>'# ?/?',
48
- '13'=>'# ??/??', '14'=>'mm-dd-yy', '15'=>'d-mmm-yy', '16'=>'d-mmm',
49
- '17'=>'mmm-yy', '18'=>'h:mm AM/PM', '19'=>'h:mm:ss AM/PM',
50
- '20'=>'h:mm', '21'=>'h:mm:ss', '22'=>'m/d/yy h:mm',
51
- '37'=>'#,##0 ;(#,##0)', '38'=>'#,##0 ;[Red](#,##0)',
52
- '39'=>'#,##0.00;(#,##0.00)', '40'=>'#,##0.00;[Red](#,##0.00)',
53
- '44'=>'_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)',
54
- '45'=>'mm:ss', '46'=>'[h]:mm:ss', '47'=>'mmss.0', '48'=>'##0.0E+0',
55
- '49'=>'@', '27'=>'[$-404]e/m/d', '30'=>'m/d/yy', '36'=>'[$-404]e/m/d',
56
- '50'=>'[$-404]e/m/d', '57'=>'[$-404]e/m/d', '59'=>'t0', '60'=>'t0.00',
57
- '61'=>'t#,##0', '62'=>'t#,##0.00', '67'=>'t0%', '68'=>'t0.00%',
58
- '69'=>'t# ?/?', '70' => 't# ??/??'}[num_fmt_id.to_s]
46
+ {"0" => nil, "1" => "0", "2" => "0.00", "3" => "#,##0", "4" => "#,##0.00",
47
+ "9" => "0%", "10" => "0.00%", "11" => "0.00E+00", "12" => "# ?/?",
48
+ "13" => "# ??/??", "14" => "mm-dd-yy", "15" => "d-mmm-yy", "16" => "d-mmm",
49
+ "17" => "mmm-yy", "18" => "h:mm AM/PM", "19" => "h:mm:ss AM/PM",
50
+ "20" => "h:mm", "21" => "h:mm:ss", "22" => "m/d/yy h:mm",
51
+ "37" => "#,##0 ;(#,##0)", "38" => "#,##0 ;[Red](#,##0)",
52
+ "39" => "#,##0.00;(#,##0.00)", "40" => "#,##0.00;[Red](#,##0.00)",
53
+ "44" => '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)',
54
+ "45" => "mm:ss", "46" => "[h]:mm:ss", "47" => "mmss.0", "48" => "##0.0E+0",
55
+ "49" => "@", "27" => "[$-404]e/m/d", "30" => "m/d/yy", "36" => "[$-404]e/m/d",
56
+ "50" => "[$-404]e/m/d", "57" => "[$-404]e/m/d", "59" => "t0", "60" => "t0.00",
57
+ "61" => "t#,##0", "62" => "t#,##0.00", "67" => "t0%", "68" => "t0.00%",
58
+ "69" => "t# ?/?", "70" => "t# ??/??"}[num_fmt_id.to_s]
59
59
  end
60
60
 
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
61
  # Attempt to convert html-hex color value to xls color number
69
62
  #
70
63
  # @param [String] hex color
71
64
  # @return [String] xls color
72
65
  def html_color_to_xls_color hex
73
- Workbook::Readers::XlsShared::XLS_COLORS.each do |k,v|
74
- return k if (v == hex or (hex and hex != "" and k == hex.to_sym))
66
+ Workbook::Readers::XlsShared::XLS_COLORS.each do |k, v|
67
+ return k if (v == hex) || (hex && (hex != "") && (k == hex.to_sym))
75
68
  end
76
- return nil
69
+ nil
77
70
  end
78
71
 
79
72
  # Converts standard (ruby/C++/unix/...) strftime formatting to MS's formatting
@@ -82,86 +75,84 @@ module Workbook
82
75
  # @return [String, nil]
83
76
  def strftime_to_ms_format numberformat
84
77
  return nil if numberformat.nil?
85
- return numberformat.gsub('%Y','yyyy').gsub('%A','dddd').gsub('%B','mmmm').gsub('%a','ddd').gsub('%b','mmm').gsub('%y','yy').gsub('%d','dd').gsub('%m','mm').gsub('%y','y').gsub('%y','%%y').gsub('%e','d')
78
+ numberformat.gsub("%Y", "yyyy").gsub("%A", "dddd").gsub("%B", "mmmm").gsub("%a", "ddd").gsub("%b", "mmm").gsub("%y", "yy").gsub("%d", "dd").gsub("%m", "mm").gsub("%y", "y").gsub("%y", "%%y").gsub("%e", "d")
86
79
  end
87
80
 
88
- def xls_number_to_time number, base_date = DateTime.new(1899,12,30)
81
+ def xls_number_to_time number, base_date = DateTime.new(1899, 12, 30)
89
82
  base_date + number.to_f
90
83
  end
91
84
 
92
- def xls_number_to_date number, base_date = Date.new(1899,12,30)
85
+ def xls_number_to_date number, base_date = Date.new(1899, 12, 30)
93
86
  base_date + number.to_i
94
87
  end
95
88
 
96
- XLS_COLORS = {:xls_color_1=>'#000000',
97
- :xls_color_2=>'#FFFFFF',
98
- :xls_color_3=>'#FF0000',
99
- :xls_color_4=>'#00FF00',
100
- :xls_color_5=>'#0000FF',
101
- :xls_color_6=>'#FFFF00',
102
- :xls_color_7=>'#FF00FF',
103
- :xls_color_8=>'#00FFFF',
104
- :xls_color_9=>'#800000',
105
- :xls_color_10=>'#008000',
106
- :xls_color_11=>'#000080',
107
- :xls_color_12=>'#808000',
108
- :xls_color_13=>'#800080',
109
- :xls_color_14=>'#008080',
110
- :xls_color_15=>'#C0C0C0',
111
- :xls_color_16=>'#808080',
112
- :xls_color_17=>'#9999FF',
113
- :xls_color_18=>'#993366',
114
- :xls_color_19=>'#FFFFCC',
115
- :xls_color_20=>'#CCFFFF',
116
- :xls_color_21=>'#660066',
117
- :xls_color_22=>'#FF8080',
118
- :xls_color_23=>'#0066CC',
119
- :xls_color_24=>'#CCCCFF',
120
- :xls_color_25=>'#000080',
121
- :xls_color_26=>'#FF00FF',
122
- :xls_color_27=>'#FFFF00',
123
- :xls_color_28=>'#00FFFF',
124
- :xls_color_29=>'#800080',
125
- :xls_color_30=>'#800000',
126
- :xls_color_31=>'#008080',
127
- :xls_color_32=>'#0000FF',
128
- :xls_color_33=>'#00CCFF',
129
- :xls_color_34=>'#CCFFFF',
130
- :xls_color_35=>'#CCFFCC',
131
- :xls_color_36=>'#FFFF99',
132
- :xls_color_37=>'#99CCFF',
133
- :xls_color_38=>'#FF99CC',
134
- :xls_color_39=>'#CC99FF',
135
- :xls_color_40=>'#FFCC99',
136
- :xls_color_41=>'#3366FF',
137
- :xls_color_42=>'#33CCCC',
138
- :xls_color_43=>'#99CC00',
139
- :xls_color_44=>'#FFCC00',
140
- :xls_color_45=>'#FF9900',
141
- :xls_color_46=>'#FF6600',
142
- :xls_color_47=>'#666699',
143
- :xls_color_48=>'#969696',
144
- :xls_color_49=>'#003366',
145
- :xls_color_50=>'#339966',
146
- :xls_color_51=>'#003300',
147
- :xls_color_52=>'#333300',
148
- :xls_color_53=>'#993300',
149
- :xls_color_54=>'#993366',
150
- :xls_color_55=>'#333399',
151
- :xls_color_56=>'#333333',
152
- :black=>'#000000',
153
- :white=>'#FFFFFF',
154
- :red=>'#FF0000',
155
- :green=>'#00FF00',
156
- :blue=>'#0000FF',
157
- :yellow=>'#FFFF00',
158
- :magenta=>'#FF00FF',
159
- :cyan=>'#00FFFF',
160
- :border=>'#FFFFFF',
161
- :text=>'#000000',
162
- :lime=>'#00f94c'
163
- }
89
+ XLS_COLORS = {xls_color_1: "#000000",
90
+ xls_color_2: "#FFFFFF",
91
+ xls_color_3: "#FF0000",
92
+ xls_color_4: "#00FF00",
93
+ xls_color_5: "#0000FF",
94
+ xls_color_6: "#FFFF00",
95
+ xls_color_7: "#FF00FF",
96
+ xls_color_8: "#00FFFF",
97
+ xls_color_9: "#800000",
98
+ xls_color_10: "#008000",
99
+ xls_color_11: "#000080",
100
+ xls_color_12: "#808000",
101
+ xls_color_13: "#800080",
102
+ xls_color_14: "#008080",
103
+ xls_color_15: "#C0C0C0",
104
+ xls_color_16: "#808080",
105
+ xls_color_17: "#9999FF",
106
+ xls_color_18: "#993366",
107
+ xls_color_19: "#FFFFCC",
108
+ xls_color_20: "#CCFFFF",
109
+ xls_color_21: "#660066",
110
+ xls_color_22: "#FF8080",
111
+ xls_color_23: "#0066CC",
112
+ xls_color_24: "#CCCCFF",
113
+ xls_color_25: "#000080",
114
+ xls_color_26: "#FF00FF",
115
+ xls_color_27: "#FFFF00",
116
+ xls_color_28: "#00FFFF",
117
+ xls_color_29: "#800080",
118
+ xls_color_30: "#800000",
119
+ xls_color_31: "#008080",
120
+ xls_color_32: "#0000FF",
121
+ xls_color_33: "#00CCFF",
122
+ xls_color_34: "#CCFFFF",
123
+ xls_color_35: "#CCFFCC",
124
+ xls_color_36: "#FFFF99",
125
+ xls_color_37: "#99CCFF",
126
+ xls_color_38: "#FF99CC",
127
+ xls_color_39: "#CC99FF",
128
+ xls_color_40: "#FFCC99",
129
+ xls_color_41: "#3366FF",
130
+ xls_color_42: "#33CCCC",
131
+ xls_color_43: "#99CC00",
132
+ xls_color_44: "#FFCC00",
133
+ xls_color_45: "#FF9900",
134
+ xls_color_46: "#FF6600",
135
+ xls_color_47: "#666699",
136
+ xls_color_48: "#969696",
137
+ xls_color_49: "#003366",
138
+ xls_color_50: "#339966",
139
+ xls_color_51: "#003300",
140
+ xls_color_52: "#333300",
141
+ xls_color_53: "#993300",
142
+ xls_color_54: "#993366",
143
+ xls_color_55: "#333399",
144
+ xls_color_56: "#333333",
145
+ black: "#000000",
146
+ white: "#FFFFFF",
147
+ red: "#FF0000",
148
+ green: "#00FF00",
149
+ blue: "#0000FF",
150
+ yellow: "#FFFF00",
151
+ magenta: "#FF00FF",
152
+ cyan: "#00FFFF",
153
+ border: "#FFFFFF",
154
+ text: "#000000",
155
+ lime: "#00f94c"}
164
156
  end
165
157
  end
166
158
  end
167
-