writeexcel 0.4.0 → 0.4.1

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.
@@ -0,0 +1,59 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ class Colors
4
+ COLORS = {
5
+ :aqua => 0x0F,
6
+ :cyan => 0x0F,
7
+ :black => 0x08,
8
+ :blue => 0x0C,
9
+ :brown => 0x10,
10
+ :magenta => 0x0E,
11
+ :fuchsia => 0x0E,
12
+ :gray => 0x17,
13
+ :grey => 0x17,
14
+ :green => 0x11,
15
+ :lime => 0x0B,
16
+ :navy => 0x12,
17
+ :orange => 0x35,
18
+ :pink => 0x21,
19
+ :purple => 0x14,
20
+ :red => 0x0A,
21
+ :silver => 0x16,
22
+ :white => 0x09,
23
+ :yellow => 0x0D,
24
+ } # :nodoc:
25
+
26
+ ###############################################################################
27
+ #
28
+ # get_color(colour)
29
+ #
30
+ # Used in conjunction with the set_xxx_color methods to convert a color
31
+ # string into a number. Color range is 0..63 but we will restrict it
32
+ # to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.
33
+ #
34
+ def get_color(color = nil) # :nodoc:
35
+ if color.respond_to?(:to_int) && color.respond_to?(:+)
36
+ # the default color if arg is outside range,
37
+ if color < 0 || 63 < color
38
+ 0x7FFF
39
+ # or an index < 8 mapped into the correct range,
40
+ elsif color < 8
41
+ (color + 8).to_i
42
+ # or an integer in the valid range
43
+ else
44
+ color.to_i
45
+ end
46
+ elsif color.respond_to?(:to_sym)
47
+ color = color.downcase.to_sym if color.respond_to?(:to_str)
48
+ # or the color string converted to an integer,
49
+ if COLORS.has_key?(color)
50
+ COLORS[color]
51
+ # or the default color if string is unrecognised,
52
+ else
53
+ 0x7FFF
54
+ end
55
+ else
56
+ 0x7FFF
57
+ end
58
+ end
59
+ end
@@ -222,7 +222,7 @@ def ord
222
222
 
223
223
  unless "".respond_to?(:force_encoding)
224
224
  def force_encoding(encoding)
225
- if encoding.kind_of?(String)
225
+ if encoding.respond_to?(:to_str)
226
226
  @encoding = case encoding
227
227
  when /ASCII/i
228
228
  Encoding::ASCII
@@ -0,0 +1,33 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'writeexcel/caller_info'
4
+
5
+ if defined?($debug)
6
+ class BIFFWriter
7
+ include CallerInfo
8
+
9
+ def append(*args)
10
+ data = args.collect{ |arg| arg.dup.force_encoding('ASCII-8BIT') }.join
11
+ print_caller_info(data, :method => 'append')
12
+ super
13
+ end
14
+
15
+ def prepend(*args)
16
+ data = args.collect{ |arg| arg.dup.force_encoding('ASCII-8BIT') }.join
17
+ print_caller_info(data, :method => 'prepend')
18
+ super
19
+ end
20
+
21
+ def print_caller_info(data, param = {})
22
+ infos = caller_info
23
+
24
+ print "#{param[:method]}\n" if param[:method]
25
+ infos.each do |info|
26
+ print "#{info[:file]}:#{info[:line]}"
27
+ print " in #{info[:method]}" if info[:method]
28
+ print "\n"
29
+ end
30
+ print data.unpack('C*').map! {|byte| sprintf("%02X", byte) }.join(' ') + "\n\n"
31
+ end
32
+ end
33
+ end
@@ -1,139 +1,139 @@
1
- #
2
- # copyright (c) 2009, 2010 Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
3
-
4
-
5
- class ExcelFormulaParser
6
-
7
- prechigh
8
- nonassoc UMINUS
9
- right '^'
10
- left '&'
11
- left '*' '/'
12
- left '+' '-'
13
- left '<' '>' '<=' '>=' '<>'
14
- left '='
15
- preclow
16
-
17
- rule
18
-
19
- formula : expr_list
20
-
21
- expr_list : { result = [] }
22
- | expr_list expr EOL { result.push val[1], '_arg', '1' }
23
- | expr_list EOL
24
-
25
- expr : expr '+' expr { result = [ val[0], val[2], 'ptgAdd' ] }
26
- | expr '-' expr { result = [ val[0], val[2], 'ptgSub' ] }
27
- | expr '*' expr { result = [ val[0], val[2], 'ptgMul' ] }
28
- | expr '/' expr { result = [ val[0], val[2], 'ptgDiv' ] }
29
- | expr '^' expr { result = [ val[0], val[2], 'ptgPower' ] }
30
- | expr '&' expr { result = [ val[0], val[2], 'ptgConcat' ] }
31
- | expr LT expr { result = [ val[0], val[2], 'ptgLT' ] }
32
- | expr GT expr { result = [ val[0], val[2], 'ptgGT' ] }
33
- | expr LE expr { result = [ val[0], val[2], 'ptgLE' ] }
34
- | expr GE expr { result = [ val[0], val[2], 'ptgGE' ] }
35
- | expr NE expr { result = [ val[0], val[2], 'ptgNE' ] }
36
- | expr '=' expr { result = [ val[0], val[2], 'ptgEQ' ] }
37
- | primary
38
-
39
- primary : '(' expr ')' { result = [ val[1], '_arg', '1', 'ptgParen'] }
40
- | '-' expr = UMINUS { result = [ '_num', '-1', val[1], 'ptgMul' ] }
41
- | FUNC
42
- | NUMBER { result = [ '_num', val[0] ] }
43
- | STRING { result = [ '_str', val[0] ] }
44
- | REF2D { result = [ '_ref2d', val[0] ] }
45
- | REF3D { result = [ '_ref3d', val[0] ] }
46
- | RANGE2D { result = [ '_range2d', val[0] ] }
47
- | RANGE3D { result = [ '_range3d', val[0] ] }
48
- | NAME { result = [ '_name', val[0] ] }
49
- | TRUE { result = [ 'ptgBool', '1' ] }
50
- | FALSE { result = [ 'ptgBool', '0' ] }
51
- | funcall
52
-
53
- funcall : FUNC '(' args ')' { result = [ '_class', val[0], val[2], '_arg', val[2].size.to_s, '_func', val[0] ] }
54
- | FUNC '(' ')' { result = [ '_func', val[0] ] }
55
-
56
- args : expr { result = val }
57
- | args ',' expr { result.push val[2] }
58
-
59
- end
60
-
61
-
62
- ---- footer
63
-
64
- class ExcelFormulaParserError < StandardError; end
65
-
66
- class Node
67
-
68
- def exec_list(nodes)
69
- v = nil
70
- nodes.each { |i| v = i.evaluate }
71
- v
72
- end
73
-
74
- def excelformulaparser_error(msg)
75
- raise ExcelFormulaParserError,
76
- "in #{fname}:#{lineno}: #{msg}"
77
- end
78
-
79
- end
80
-
81
- class RootNode < Node
82
-
83
- def initialize(tree)
84
- @tree = tree
85
- end
86
-
87
- def evaluate
88
- exec_list @tree
89
- end
90
-
91
- end
92
-
93
-
94
- class FuncallNode < Node
95
-
96
- def initialize(func, args)
97
- @func = func
98
- @args = args
99
- end
100
-
101
- def evaluate
102
- arg = @args.collect {|i| i.evaluate }
103
- out = []
104
- arg.each { |i| o.push i }
105
- o.push @func
106
- p o
107
- end
108
-
109
- end
110
-
111
- class NumberNode < Node
112
-
113
- def initialize(val)
114
- @val = val
115
- end
116
-
117
- def evaluate
118
- p @val
119
- end
120
-
121
- end
122
-
123
- class OperateNode < Node
124
-
125
- def initialize(op, left, right)
126
- @op = op
127
- @left = left
128
- @right = right
129
- end
130
-
131
- def evaluate
132
- o = []
133
- o.push @left
134
- o.push @right
135
- o.push @op
136
- p o
137
- end
138
- end
139
-
1
+ #
2
+ # copyright (c) 2009, 2010 Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
3
+
4
+
5
+ class ExcelFormulaParser
6
+
7
+ prechigh
8
+ nonassoc UMINUS
9
+ right '^'
10
+ left '&'
11
+ left '*' '/'
12
+ left '+' '-'
13
+ left '<' '>' '<=' '>=' '<>'
14
+ left '='
15
+ preclow
16
+
17
+ rule
18
+
19
+ formula : expr_list
20
+
21
+ expr_list : { result = [] }
22
+ | expr_list expr EOL { result.push val[1], '_arg', '1' }
23
+ | expr_list EOL
24
+
25
+ expr : expr '+' expr { result = [ val[0], val[2], 'ptgAdd' ] }
26
+ | expr '-' expr { result = [ val[0], val[2], 'ptgSub' ] }
27
+ | expr '*' expr { result = [ val[0], val[2], 'ptgMul' ] }
28
+ | expr '/' expr { result = [ val[0], val[2], 'ptgDiv' ] }
29
+ | expr '^' expr { result = [ val[0], val[2], 'ptgPower' ] }
30
+ | expr '&' expr { result = [ val[0], val[2], 'ptgConcat' ] }
31
+ | expr LT expr { result = [ val[0], val[2], 'ptgLT' ] }
32
+ | expr GT expr { result = [ val[0], val[2], 'ptgGT' ] }
33
+ | expr LE expr { result = [ val[0], val[2], 'ptgLE' ] }
34
+ | expr GE expr { result = [ val[0], val[2], 'ptgGE' ] }
35
+ | expr NE expr { result = [ val[0], val[2], 'ptgNE' ] }
36
+ | expr '=' expr { result = [ val[0], val[2], 'ptgEQ' ] }
37
+ | primary
38
+
39
+ primary : '(' expr ')' { result = [ val[1], '_arg', '1', 'ptgParen'] }
40
+ | '-' expr = UMINUS { result = [ '_num', '-1', val[1], 'ptgMul' ] }
41
+ | FUNC
42
+ | NUMBER { result = [ '_num', val[0] ] }
43
+ | STRING { result = [ '_str', val[0] ] }
44
+ | REF2D { result = [ '_ref2d', val[0] ] }
45
+ | REF3D { result = [ '_ref3d', val[0] ] }
46
+ | RANGE2D { result = [ '_range2d', val[0] ] }
47
+ | RANGE3D { result = [ '_range3d', val[0] ] }
48
+ | NAME { result = [ '_name', val[0] ] }
49
+ | TRUE { result = [ 'ptgBool', '1' ] }
50
+ | FALSE { result = [ 'ptgBool', '0' ] }
51
+ | funcall
52
+
53
+ funcall : FUNC '(' args ')' { result = [ '_class', val[0], val[2], '_arg', val[2].size.to_s, '_func', val[0] ] }
54
+ | FUNC '(' ')' { result = [ '_func', val[0] ] }
55
+
56
+ args : expr { result = val }
57
+ | args ',' expr { result.push val[2] }
58
+
59
+ end
60
+
61
+
62
+ ---- footer
63
+
64
+ class ExcelFormulaParserError < StandardError; end
65
+
66
+ class Node
67
+
68
+ def exec_list(nodes)
69
+ v = nil
70
+ nodes.each { |i| v = i.evaluate }
71
+ v
72
+ end
73
+
74
+ def excelformulaparser_error(msg)
75
+ raise ExcelFormulaParserError,
76
+ "in #{fname}:#{lineno}: #{msg}"
77
+ end
78
+
79
+ end
80
+
81
+ class RootNode < Node
82
+
83
+ def initialize(tree)
84
+ @tree = tree
85
+ end
86
+
87
+ def evaluate
88
+ exec_list @tree
89
+ end
90
+
91
+ end
92
+
93
+
94
+ class FuncallNode < Node
95
+
96
+ def initialize(func, args)
97
+ @func = func
98
+ @args = args
99
+ end
100
+
101
+ def evaluate
102
+ arg = @args.collect {|i| i.evaluate }
103
+ out = []
104
+ arg.each { |i| o.push i }
105
+ o.push @func
106
+ p o
107
+ end
108
+
109
+ end
110
+
111
+ class NumberNode < Node
112
+
113
+ def initialize(val)
114
+ @val = val
115
+ end
116
+
117
+ def evaluate
118
+ p @val
119
+ end
120
+
121
+ end
122
+
123
+ class OperateNode < Node
124
+
125
+ def initialize(op, left, right)
126
+ @op = op
127
+ @left = left
128
+ @right = right
129
+ end
130
+
131
+ def evaluate
132
+ o = []
133
+ o.push @left
134
+ o.push @right
135
+ o.push @op
136
+ p o
137
+ end
138
+ end
139
+
@@ -17,29 +17,10 @@
17
17
  #
18
18
  # See CELL FORMATTING, FORMAT METHODS, COLOURS IN EXCEL in WriteExcel's rdoc.
19
19
  #
20
- class Format
21
-
22
- COLORS = {
23
- 'aqua' => 0x0F,
24
- 'cyan' => 0x0F,
25
- 'black' => 0x08,
26
- 'blue' => 0x0C,
27
- 'brown' => 0x10,
28
- 'magenta' => 0x0E,
29
- 'fuchsia' => 0x0E,
30
- 'gray' => 0x17,
31
- 'grey' => 0x17,
32
- 'green' => 0x11,
33
- 'lime' => 0x0B,
34
- 'navy' => 0x12,
35
- 'orange' => 0x35,
36
- 'pink' => 0x21,
37
- 'purple' => 0x14,
38
- 'red' => 0x0A,
39
- 'silver' => 0x16,
40
- 'white' => 0x09,
41
- 'yellow' => 0x0D,
42
- } # :nodoc:
20
+ require 'writeexcel/compatibility'
21
+ require 'writeexcel/colors'
22
+
23
+ class Format < Colors
43
24
 
44
25
  ###############################################################################
45
26
  #
@@ -139,8 +120,6 @@ def initialize(xf_index = 0, properties = {}) # :nodoc:
139
120
  # copying.
140
121
  #
141
122
  def copy(other)
142
- return unless other.kind_of?(Format)
143
-
144
123
  # copy properties except xf, merge_range, used_merge
145
124
  # Copy properties
146
125
  @type = other.type
@@ -392,7 +371,7 @@ def get_font # :nodoc:
392
371
 
393
372
  # Handle utf8 strings
394
373
  if rgch.encoding == Encoding::UTF_8
395
- rgch = rgch.encode('UTF-16BE')
374
+ rgch = utf8_to_16be(rgch)
396
375
  encoding = 1
397
376
  end
398
377
 
@@ -634,48 +613,6 @@ def font_only # :nodoc:
634
613
  @font_only
635
614
  end
636
615
 
637
- ###############################################################################
638
- #
639
- # get_color(colour)
640
- #
641
- # Used in conjunction with the set_xxx_color methods to convert a color
642
- # string into a number. Color range is 0..63 but we will restrict it
643
- # to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.
644
- #
645
- def get_color(colour = nil) # :nodoc:
646
- # Return the default color, 0x7FFF, if undef,
647
- return 0x7FFF if colour.nil?
648
-
649
- if colour.kind_of?(Numeric)
650
- if colour < 0
651
- return 0x7FFF
652
-
653
- # or an index < 8 mapped into the correct range,
654
- elsif colour < 8
655
- return (colour + 8).to_i
656
-
657
- # or the default color if arg is outside range,
658
- elsif colour > 63
659
- return 0x7FFF
660
-
661
- # or an integer in the valid range
662
- else
663
- return colour.to_i
664
- end
665
- elsif colour.kind_of?(String)
666
- # or the color string converted to an integer,
667
- if COLORS.has_key?(colour)
668
- return COLORS[colour]
669
-
670
- # or the default color if string is unrecognised,
671
- else
672
- return 0x7FFF
673
- end
674
- else
675
- return 0x7FFF
676
- end
677
- end
678
-
679
616
  ###############################################################################
680
617
  #
681
618
  # class method Format._get_color(colour)
@@ -684,38 +621,8 @@ def get_color(colour = nil) # :nodoc:
684
621
  #
685
622
  # this is cut & copy of get_color().
686
623
  #
687
- def self._get_color(colour) # :nodoc:
688
- # Return the default color, 0x7FFF, if undef,
689
- return 0x7FFF if colour.nil?
690
-
691
- if colour.kind_of?(Numeric)
692
- if colour < 0
693
- return 0x7FFF
694
-
695
- # or an index < 8 mapped into the correct range,
696
- elsif colour < 8
697
- return (colour + 8).to_i
698
-
699
- # or the default color if arg is outside range,
700
- elsif 63 < colour
701
- return 0x7FFF
702
-
703
- # or an integer in the valid range
704
- else
705
- return colour.to_i
706
- end
707
- elsif colour.kind_of?(String)
708
- # or the color string converted to an integer,
709
- if COLORS.has_key?(colour)
710
- return COLORS[colour]
711
-
712
- # or the default color if string is unrecognised,
713
- else
714
- return 0x7FFF
715
- end
716
- else
717
- return 0x7FFF
718
- end
624
+ def self._get_color(color) # :nodoc:
625
+ Colors.new.get_color(color)
719
626
  end
720
627
 
721
628
  ###############################################################################
@@ -746,9 +653,9 @@ def set_type(type = nil) # :nodoc:
746
653
  # format.set_size(30)
747
654
  #
748
655
  def set_size(size = 1)
749
- if size.kind_of?(Numeric) && size >= 1
750
- @size = size.to_i
751
- end
656
+ if size.respond_to?(:to_int) && size.respond_to?(:+) && size >= 1 # avoid Symbol
657
+ @size = size.to_int
658
+ end
752
659
  end
753
660
 
754
661
  #
@@ -809,17 +716,16 @@ def set_italic(arg = 1)
809
716
  # value to 1 and use normal bold.
810
717
  #
811
718
  def set_bold(weight = nil)
719
+
812
720
  if weight.nil?
813
721
  weight = 0x2BC
814
- elsif !weight.kind_of?(Numeric)
722
+ elsif !weight.respond_to?(:to_int) || !weight.respond_to?(:+) # avoid Symbol
815
723
  weight = 0x190
816
- elsif weight == 1 # Bold text
724
+ elsif weight == 1 # Bold text
817
725
  weight = 0x2BC
818
- elsif weight == 0 # Normal text
819
- weight = 0x190
820
- elsif weight < 0x064 # Lower bound
726
+ elsif weight == 0 # Normal text
821
727
  weight = 0x190
822
- elsif weight > 0x3E8 # Upper bound
728
+ elsif weight < 0x064 || 0x3E8 < weight # Out bound
823
729
  weight = 0x190
824
730
  else
825
731
  weight = weight.to_i
@@ -1055,12 +961,7 @@ def set_hidden(arg = 1)
1055
961
  # For further examples see the 'Alignment' worksheet created by formats.rb.
1056
962
  #
1057
963
  def set_align(align = 'left')
1058
-
1059
- return unless align.kind_of?(String)
1060
-
1061
- location = align.downcase
1062
-
1063
- case location
964
+ case align.to_s.downcase
1064
965
  when 'left' then set_text_h_align(1)
1065
966
  when 'centre', 'center' then set_text_h_align(2)
1066
967
  when 'right' then set_text_h_align(3)
@@ -1078,6 +979,7 @@ def set_align(align = 'left')
1078
979
  when 'vjustify' then set_text_v_align(3)
1079
980
  when 'vdistributed' then set_text_v_align(4)
1080
981
  when 'vequal_space' then set_text_v_align(4) # ParseExcel
982
+ else nil
1081
983
  end
1082
984
  end
1083
985
 
@@ -1338,9 +1240,6 @@ def set_right_color(color)
1338
1240
  # from top to bottom.
1339
1241
  #
1340
1242
  def set_rotation(rotation)
1341
- # Argument should be a number
1342
- return unless rotation.kind_of?(Numeric)
1343
-
1344
1243
  # The arg type can be a double but the Excel dialog only allows integers.
1345
1244
  rotation = rotation.to_i
1346
1245
 
@@ -1390,11 +1289,11 @@ def set_format_properties(*properties) # :nodoc:
1390
1289
  return if properties.empty?
1391
1290
  properties.each do |property|
1392
1291
  property.each do |key, value|
1393
- # Strip leading "-" from Tk style properties e.g. -color => 'red'.
1394
- key.sub!(/^-/, '') if key.kind_of?(String)
1292
+ # Strip leading "-" from Tk style properties e.g. "-color" => 'red'.
1293
+ key.sub!(/^-/, '') if key.respond_to?(:to_str)
1395
1294
 
1396
1295
  # Create a sub to set the property.
1397
- if value.kind_of?(String)
1296
+ if value.respond_to?(:to_str) || !value.respond_to?(:+)
1398
1297
  s = "set_#{key}('#{value}')"
1399
1298
  else
1400
1299
  s = "set_#{key}(#{value})"
@@ -1686,10 +1585,10 @@ def method_missing(name, *args) # :nodoc:
1686
1585
  else # for "set_xxx" methods
1687
1586
  value = args[0].nil? ? 1 : args[0]
1688
1587
  end
1689
- if value.kind_of?(String)
1690
- s = "#{attribute} = \"#{value.to_s}\""
1588
+ if value.respond_to?(:to_str) || !value.respond_to?(:+)
1589
+ s = %Q!#{attribute} = "#{value.to_s}"!
1691
1590
  else
1692
- s = "#{attribute} = #{value.to_s}"
1591
+ s = %Q!#{attribute} = #{value.to_s}!
1693
1592
  end
1694
1593
  eval s
1695
1594
  end