Class: Writexlsx::Format

Inherits:
Object
  • Object
show all
Includes:
Utility::Common, Utility::XmlPrimitives
Defined in:
lib/write_xlsx/format.rb,
lib/write_xlsx/format/fill_state.rb,
lib/write_xlsx/format/fill_style.rb,
lib/write_xlsx/format/font_state.rb,
lib/write_xlsx/format/font_style.rb,
lib/write_xlsx/format/border_state.rb,
lib/write_xlsx/format/border_style.rb,
lib/write_xlsx/format/format_state.rb,
lib/write_xlsx/format/alignment_state.rb,
lib/write_xlsx/format/alignment_style.rb,
lib/write_xlsx/format/protection_state.rb,
lib/write_xlsx/format/protection_style.rb,
lib/write_xlsx/format/number_format_state.rb,
lib/write_xlsx/format/number_format_style.rb

Overview

A cell format in the workbook.

Format represents the formatting properties associated with worksheet cells. These properties are written to the workbook style tables (styles.xml) and referenced by worksheet cell records.

Responsibilities of this class include:

  • storing formatting properties such as font, fill, border, alignment, number format, and protection settings

  • exposing the public set_* formatting API used by Workbook and Worksheet

  • delegating grouped property access through style facade objects such as FillStyle, BorderStyle, FontStyle, AlignmentStyle, ProtectionStyle, and NumberFormatStyle

  • generating unique format keys used by Workbook to deduplicate style records

  • providing helper methods used when writing styles.xml

Format instances are created by Workbook#add_format and reused across worksheets through their computed format keys.

Defined Under Namespace

Classes: AlignmentState, AlignmentStyle, BorderState, BorderStyle, FillState, FillStyle, FontState, FontStyle, FormatState, NumberFormatState, NumberFormatStyle, ProtectionState, ProtectionStyle

Constant Summary

Constants included from Utility::Common

Utility::Common::PERL_TRUE_VALUES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utility::XmlPrimitives

#r_id_attributes, #write_color, #write_xml_declaration, #xml_str

Methods included from Utility::Common

#absolute_char, #check_parameter, #float_to_str, #ptrue?, #put_deprecate_message

Constructor Details

#initialize(formats, params = {}) ⇒ Format

Lifecycle



53
54
55
56
57
58
# File 'lib/write_xlsx/format.rb', line 53

def initialize(formats, params = {}) # :nodoc:
  @formats = formats
  @state   = FormatState.new

  set_format_properties(params) unless params.empty?
end

Class Method Details

.color(color_code) ⇒ Object

Class-level utilities



1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
# File 'lib/write_xlsx/format.rb', line 1314

def self.color(color_code)
  colors = Colors::COLORS

  return 0x00 unless color_code

  if color_code.respond_to?(:to_str)
    return color_code if color_code =~ /^#[0-9A-F]{6}$/i
    return colors[color_code.downcase.to_sym] if colors[color_code.downcase.to_sym]

    0x00 if color_code =~ /\D/
  else
    return color_code + 8 if color_code < 8
    return 0x00 if color_code > 63

    color_code
  end
end

Instance Method Details

#[](attr) ⇒ Object

Compatibility accessor used by older internal callers that treat Format like a hash.



1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
# File 'lib/write_xlsx/format.rb', line 1118

def [](attr)
  case attr.to_sym
  when :font           then font
  when :size           then size
  when :bold           then bold
  when :italic         then italic
  when :color          then font_color
  when :underline      then underline
  when :font_strikeout then font_strikeout
  when :font_script    then font_script
  when :rotation       then rotation
  else
    instance_variable_get("@#{attr}")
  end
end

#alignment_styleObject



98
99
100
# File 'lib/write_xlsx/format.rb', line 98

def alignment_style
  AlignmentStyle.new(self)
end

#bg_colorObject



461
462
463
# File 'lib/write_xlsx/format.rb', line 461

def bg_color
  fill_style.bg_color
end

#boldObject



224
225
226
# File 'lib/write_xlsx/format.rb', line 224

def bold
  font_style.bold
end

#bold=(value) ⇒ Object



228
229
230
# File 'lib/write_xlsx/format.rb', line 228

def bold=(value)
  font_style.bold = value
end

#bold?Boolean

Returns:

  • (Boolean)


1042
1043
1044
# File 'lib/write_xlsx/format.rb', line 1042

def bold?
  ptrue?(bold)
end

#border_attributesObject



1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
# File 'lib/write_xlsx/format.rb', line 1271

def border_attributes
  attributes = []

  if diag_type == 1
    attributes << ['diagonalUp', 1]
  elsif diag_type == 2
    attributes << ['diagonalDown', 1]
  elsif diag_type == 3
    attributes << ['diagonalUp', 1]
    attributes << ['diagonalDown', 1]
  end

  attributes
end

#border_countObject



496
497
498
# File 'lib/write_xlsx/format.rb', line 496

def border_count
  border_style.count
end

#border_count=(value) ⇒ Object



500
501
502
# File 'lib/write_xlsx/format.rb', line 500

def border_count=(value)
  border_style.count = value
end

#border_indexObject

Border properties



488
489
490
# File 'lib/write_xlsx/format.rb', line 488

def border_index
  border_style.index
end

#border_index=(value) ⇒ Object



492
493
494
# File 'lib/write_xlsx/format.rb', line 492

def border_index=(value)
  border_style.index = value
end

#border_styleObject



90
91
92
# File 'lib/write_xlsx/format.rb', line 90

def border_style
  BorderStyle.new(self)
end

#bottomObject



552
553
554
# File 'lib/write_xlsx/format.rb', line 552

def bottom
  border_style.bottom
end

#bottom=(value) ⇒ Object



556
557
558
# File 'lib/write_xlsx/format.rb', line 556

def bottom=(value)
  border_style.bottom = value
end

#bottom_colorObject



560
561
562
# File 'lib/write_xlsx/format.rb', line 560

def bottom_color
  border_style.bottom_color
end

#bottom_color=(value) ⇒ Object



564
565
566
# File 'lib/write_xlsx/format.rb', line 564

def bottom_color=(value)
  border_style.bottom_color = value
end

#color(color_code) ⇒ Object



1034
1035
1036
# File 'lib/write_xlsx/format.rb', line 1034

def color(color_code)
  Format.color(color_code)
end

#color?Boolean

Returns:

  • (Boolean)


1038
1039
1040
# File 'lib/write_xlsx/format.rb', line 1038

def color?
  ptrue?(font_color)
end

#color_indexedObject



320
321
322
# File 'lib/write_xlsx/format.rb', line 320

def color_indexed
  font_style.color_indexed
end

#color_indexed=(value) ⇒ Object



324
325
326
# File 'lib/write_xlsx/format.rb', line 324

def color_indexed=(value)
  font_style.color_indexed = value
end

#copy(other) ⇒ Object

Copy the attributes of another Format object.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/write_xlsx/format.rb', line 63

def copy(other)
  reserve = %i[
    @xdf_format_indices
    @palette
  ]

  (instance_variables - reserve).each do |v|
    value = other.instance_variable_get(v)
    value = value.dup if v == :@state && !value.nil?
    instance_variable_set(v, value)
  end
end

#diag_borderObject



568
569
570
# File 'lib/write_xlsx/format.rb', line 568

def diag_border
  border_style.diag_border
end

#diag_border=(value) ⇒ Object



572
573
574
# File 'lib/write_xlsx/format.rb', line 572

def diag_border=(value)
  border_style.diag_border = value
end

#diag_colorObject



576
577
578
# File 'lib/write_xlsx/format.rb', line 576

def diag_color
  border_style.diag_color
end

#diag_color=(value) ⇒ Object



580
581
582
# File 'lib/write_xlsx/format.rb', line 580

def diag_color=(value)
  border_style.diag_color = value
end

#diag_typeObject



584
585
586
# File 'lib/write_xlsx/format.rb', line 584

def diag_type
  border_style.diag_type
end

#diag_type=(value) ⇒ Object



588
589
590
# File 'lib/write_xlsx/format.rb', line 588

def diag_type=(value)
  border_style.diag_type = value
end

#dxf_bg_colorObject



170
171
172
# File 'lib/write_xlsx/format.rb', line 170

def dxf_bg_color
  @dxf_bg_color
end

#dxf_bg_color=(value) ⇒ Object



174
175
176
# File 'lib/write_xlsx/format.rb', line 174

def dxf_bg_color=(value)
  @dxf_bg_color = value
end

#dxf_fg_colorObject



162
163
164
# File 'lib/write_xlsx/format.rb', line 162

def dxf_fg_color
  @dxf_fg_color
end

#dxf_fg_color=(value) ⇒ Object



166
167
168
# File 'lib/write_xlsx/format.rb', line 166

def dxf_fg_color=(value)
  @dxf_fg_color = value
end

#dxf_indexObject



127
128
129
# File 'lib/write_xlsx/format.rb', line 127

def dxf_index
  state.dxf_index
end

#dxf_index=(value) ⇒ Object



131
132
133
# File 'lib/write_xlsx/format.rb', line 131

def dxf_index=(value)
  state.dxf_index = value
end

#fg_colorObject

Fill properties



457
458
459
# File 'lib/write_xlsx/format.rb', line 457

def fg_color
  fill_style.fg_color
end

#fill_countObject



477
478
479
# File 'lib/write_xlsx/format.rb', line 477

def fill_count
  fill_style.count
end

#fill_count=(value) ⇒ Object



481
482
483
# File 'lib/write_xlsx/format.rb', line 481

def fill_count=(value)
  fill_style.count = value
end

#fill_indexObject



469
470
471
# File 'lib/write_xlsx/format.rb', line 469

def fill_index
  fill_style.index
end

#fill_index=(value) ⇒ Object



473
474
475
# File 'lib/write_xlsx/format.rb', line 473

def fill_index=(value)
  fill_style.index = value
end

#fill_styleObject



86
87
88
# File 'lib/write_xlsx/format.rb', line 86

def fill_style
  FillStyle.new(self)
end

#fontObject

Font properties



200
201
202
# File 'lib/write_xlsx/format.rb', line 200

def font
  font_style.name
end

#font=(value) ⇒ Object



204
205
206
# File 'lib/write_xlsx/format.rb', line 204

def font=(value)
  font_style.name = value
end

#font_charsetObject



280
281
282
# File 'lib/write_xlsx/format.rb', line 280

def font_charset
  font_style.charset
end

#font_charset=(value) ⇒ Object



284
285
286
# File 'lib/write_xlsx/format.rb', line 284

def font_charset=(value)
  font_style.charset = value
end

#font_colorObject



216
217
218
# File 'lib/write_xlsx/format.rb', line 216

def font_color
  font_style.color
end

#font_color=(value) ⇒ Object



220
221
222
# File 'lib/write_xlsx/format.rb', line 220

def font_color=(value)
  font_style.color = value
end

#font_condenseObject



304
305
306
# File 'lib/write_xlsx/format.rb', line 304

def font_condense
  font_style.condense
end

#font_condense=(value) ⇒ Object



308
309
310
# File 'lib/write_xlsx/format.rb', line 308

def font_condense=(value)
  font_style.condense = value
end

#font_extendObject



312
313
314
# File 'lib/write_xlsx/format.rb', line 312

def font_extend
  font_style.extend
end

#font_extend=(value) ⇒ Object



316
317
318
# File 'lib/write_xlsx/format.rb', line 316

def font_extend=(value)
  font_style.extend = value
end

#font_familyObject



288
289
290
# File 'lib/write_xlsx/format.rb', line 288

def font_family
  font_style.family
end

#font_family=(value) ⇒ Object



292
293
294
# File 'lib/write_xlsx/format.rb', line 292

def font_family=(value)
  font_style.family = value
end

#font_indexObject



344
345
346
# File 'lib/write_xlsx/format.rb', line 344

def font_index
  font_style.index
end

#font_index=(value) ⇒ Object



348
349
350
# File 'lib/write_xlsx/format.rb', line 348

def font_index=(value)
  font_style.index = value
end

#font_outlineObject



264
265
266
# File 'lib/write_xlsx/format.rb', line 264

def font_outline
  font_style.outline
end

#font_outline=(value) ⇒ Object



268
269
270
# File 'lib/write_xlsx/format.rb', line 268

def font_outline=(value)
  font_style.outline = value
end

#font_schemeObject



296
297
298
# File 'lib/write_xlsx/format.rb', line 296

def font_scheme
  font_style.scheme
end

#font_scheme=(value) ⇒ Object



300
301
302
# File 'lib/write_xlsx/format.rb', line 300

def font_scheme=(value)
  font_style.scheme = value
end

#font_scriptObject



256
257
258
# File 'lib/write_xlsx/format.rb', line 256

def font_script
  font_style.script
end

#font_script=(value) ⇒ Object



260
261
262
# File 'lib/write_xlsx/format.rb', line 260

def font_script=(value)
  font_style.script = value
end

#font_shadowObject



272
273
274
# File 'lib/write_xlsx/format.rb', line 272

def font_shadow
  font_style.shadow
end

#font_shadow=(value) ⇒ Object



276
277
278
# File 'lib/write_xlsx/format.rb', line 276

def font_shadow=(value)
  font_style.shadow = value
end

#font_strikeoutObject



248
249
250
# File 'lib/write_xlsx/format.rb', line 248

def font_strikeout
  font_style.strikeout
end

#font_strikeout=(value) ⇒ Object



252
253
254
# File 'lib/write_xlsx/format.rb', line 252

def font_strikeout=(value)
  font_style.strikeout = value
end

#font_styleObject



94
95
96
# File 'lib/write_xlsx/format.rb', line 94

def font_style
  FontStyle.new(self)
end

#force_text_format?Boolean

Returns:

  • (Boolean)


1134
1135
1136
# File 'lib/write_xlsx/format.rb', line 1134

def force_text_format?
  num_format == 49
end

#get_align_propertiesObject

Alignment and protection serialization helpers



1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
# File 'lib/write_xlsx/format.rb', line 1144

def get_align_properties
  align = []

  h_align            = text_h_align
  v_align            = text_v_align
  indent_value       = indent
  rotation_value     = rotation
  wrap               = text_wrap
  shrink_value       = shrink
  reading_value      = reading_order
  just_distrib_value = just_distrib

  if h_align != 0 ||
     v_align != 0 ||
     indent_value != 0 ||
     rotation_value != 0 ||
     wrap != 0 ||
     shrink_value != 0 ||
     reading_value != 0
    changed = 1
  else
    return
  end

  if indent_value != 0 && ![1, 3, 7].include?(h_align) && ![1, 3, 5].include?(v_align)
    h_align = 1
  end

  shrink_value       = 0 if wrap != 0
  shrink_value       = 0 if h_align == 4
  shrink_value       = 0 if h_align == 5
  shrink_value       = 0 if h_align == 7
  just_distrib_value = 0 if h_align != 7
  just_distrib_value = 0 if indent_value != 0

  continuous = 'centerContinuous'

  align << %w[horizontal left]        if h_align == 1
  align << %w[horizontal center]      if h_align == 2
  align << %w[horizontal right]       if h_align == 3
  align << %w[horizontal fill]        if h_align == 4
  align << %w[horizontal justify]     if h_align == 5
  align << ['horizontal', continuous] if h_align == 6
  align << %w[horizontal distributed] if h_align == 7

  align << ['justifyLastLine', 1] if just_distrib_value != 0

  align << %w[vertical top]         if v_align == 1
  align << %w[vertical center]      if v_align == 2
  align << %w[vertical justify]     if v_align == 4
  align << %w[vertical distributed] if v_align == 5

  align << ['textRotation', rotation_value] if rotation_value != 0
  align << ['indent', indent_value]         if indent_value != 0

  align << ['wrapText', 1]    if wrap != 0
  align << ['shrinkToFit', 1] if shrink_value != 0

  align << ['readingOrder', 1] if reading_value == 1
  align << ['readingOrder', 2] if reading_value == 2

  [changed, align]
end

#get_alignment_keyObject



966
967
968
969
970
971
972
973
974
975
976
# File 'lib/write_xlsx/format.rb', line 966

def get_alignment_key
  [
    text_h_align,
    text_v_align,
    indent,
    rotation,
    text_wrap,
    shrink,
    reading_order
  ].join(':')
end

#get_border_keyObject



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
# File 'lib/write_xlsx/format.rb', line 942

def get_border_key
  [
    bottom,
    bottom_color,
    diag_border,
    diag_color,
    diag_type,
    left,
    left_color,
    right,
    right_color,
    top,
    top_color
  ].join(':')
end

#get_dxf_indexObject



988
989
990
991
992
993
994
995
996
# File 'lib/write_xlsx/format.rb', line 988

def get_dxf_index
  if dxf_index
    dxf_index
  elsif @formats.dxf_index_by_key(get_format_key)
    @formats.dxf_index_by_key(get_format_key)
  else
    self.dxf_index = @formats.set_dxf_index_by_key(get_format_key)
  end
end

#get_fill_keyObject



958
959
960
961
962
963
964
# File 'lib/write_xlsx/format.rb', line 958

def get_fill_key
  [
    pattern,
    bg_color,
    fg_color
  ].join(':')
end

#get_font_keyObject



920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'lib/write_xlsx/format.rb', line 920

def get_font_key
  [
    font,
    size,
    bold,
    italic,
    font_color,
    underline,
    font_strikeout,
    font_script,
    font_outline,
    font_shadow,
    font_family,
    font_charset,
    font_scheme,
    font_condense,
    font_extend,
    theme,
    hyperlink
  ].join(':')
end

#get_format_keyObject

Workbook integration and key generation



907
908
909
910
911
912
913
914
915
916
917
918
# File 'lib/write_xlsx/format.rb', line 907

def get_format_key
  [
    get_font_key,
    get_border_key,
    get_fill_key,
    get_alignment_key,
    num_format,
    locked,
    hidden,
    quote_prefix
  ].join(':')
end

#get_protection_propertiesObject



1208
1209
1210
1211
1212
1213
1214
1215
# File 'lib/write_xlsx/format.rb', line 1208

def get_protection_properties
  return if locked != 0 && hidden == 0

  attributes = []
  attributes << ['locked', 0] if locked == 0
  attributes << ['hidden', 1] if hidden != 0
  attributes
end

#get_xf_indexObject



978
979
980
981
982
983
984
985
986
# File 'lib/write_xlsx/format.rb', line 978

def get_xf_index
  if xf_index
    xf_index
  elsif @formats.xf_index_by_key(get_format_key)
    @formats.xf_index_by_key(get_format_key)
  else
    self.xf_index = @formats.set_xf_index_by_key(get_format_key)
  end
end

#has_border(flag) ⇒ Object



1066
1067
1068
# File 'lib/write_xlsx/format.rb', line 1066

def has_border(flag)
  @has_border = flag
end

#has_border?Boolean

Returns:

  • (Boolean)


1070
1071
1072
# File 'lib/write_xlsx/format.rb', line 1070

def has_border?
  @has_border
end

#has_dxf_border(flag) ⇒ Object



1074
1075
1076
# File 'lib/write_xlsx/format.rb', line 1074

def has_dxf_border(flag)
  @has_dxf_border = flag
end

#has_dxf_border?Boolean

Returns:

  • (Boolean)


1078
1079
1080
# File 'lib/write_xlsx/format.rb', line 1078

def has_dxf_border?
  @has_dxf_border
end

#has_dxf_fill(flag) ⇒ Object



1106
1107
1108
# File 'lib/write_xlsx/format.rb', line 1106

def has_dxf_fill(flag)
  @has_dxf_fill = flag
end

#has_dxf_fill?Boolean

Returns:

  • (Boolean)


1110
1111
1112
# File 'lib/write_xlsx/format.rb', line 1110

def has_dxf_fill?
  @has_dxf_fill
end

#has_dxf_font(flag) ⇒ Object



1090
1091
1092
# File 'lib/write_xlsx/format.rb', line 1090

def has_dxf_font(flag)
  @has_dxf_font = flag
end

#has_dxf_font?Boolean

Returns:

  • (Boolean)


1094
1095
1096
# File 'lib/write_xlsx/format.rb', line 1094

def has_dxf_font?
  @has_dxf_font
end

#has_fill(flag) ⇒ Object

Miscellaneous flags and compatibility ivars



146
147
148
# File 'lib/write_xlsx/format.rb', line 146

def has_fill
  @has_fill
end

#has_fill=(value) ⇒ Object



150
151
152
# File 'lib/write_xlsx/format.rb', line 150

def has_fill=(value)
  @has_fill = value
end

#has_fill?Boolean

Returns:

  • (Boolean)


1102
1103
1104
# File 'lib/write_xlsx/format.rb', line 1102

def has_fill?
  @has_fill
end

#has_font=(flag) ⇒ Object



1082
1083
1084
# File 'lib/write_xlsx/format.rb', line 1082

def has_font=(flag)
  @has_font = flag
end

#has_font?Boolean

Returns:

  • (Boolean)


1086
1087
1088
# File 'lib/write_xlsx/format.rb', line 1086

def has_font?
  @has_font
end

#hiddenObject



363
364
365
# File 'lib/write_xlsx/format.rb', line 363

def hidden
  protection_style.hidden
end

#hidden=(value) ⇒ Object



367
368
369
# File 'lib/write_xlsx/format.rb', line 367

def hidden=(value)
  protection_style.hidden = value
end


336
337
338
# File 'lib/write_xlsx/format.rb', line 336

def hyperlink
  font_style.hyperlink
end

#hyperlink=(value) ⇒ Object



340
341
342
# File 'lib/write_xlsx/format.rb', line 340

def hyperlink=(value)
  font_style.hyperlink = value
end

#indentObject



414
415
416
# File 'lib/write_xlsx/format.rb', line 414

def indent
  alignment_style.indent
end

#indent=(value) ⇒ Object



418
419
420
# File 'lib/write_xlsx/format.rb', line 418

def indent=(value)
  alignment_style.indent = value
end

#inspectObject

Queries and compatibility helpers



1030
1031
1032
# File 'lib/write_xlsx/format.rb', line 1030

def inspect
  to_s
end

#italicObject



232
233
234
# File 'lib/write_xlsx/format.rb', line 232

def italic
  font_style.italic
end

#italic=(value) ⇒ Object



236
237
238
# File 'lib/write_xlsx/format.rb', line 236

def italic=(value)
  font_style.italic = value
end

#italic?Boolean

Returns:

  • (Boolean)


1046
1047
1048
# File 'lib/write_xlsx/format.rb', line 1046

def italic?
  ptrue?(italic)
end

#just_distribObject



446
447
448
# File 'lib/write_xlsx/format.rb', line 446

def just_distrib
  alignment_style.just_distrib
end

#just_distrib=(value) ⇒ Object



450
451
452
# File 'lib/write_xlsx/format.rb', line 450

def just_distrib=(value)
  alignment_style.just_distrib = value
end

#leftObject



504
505
506
# File 'lib/write_xlsx/format.rb', line 504

def left
  border_style.left
end

#left=(value) ⇒ Object



508
509
510
# File 'lib/write_xlsx/format.rb', line 508

def left=(value)
  border_style.left = value
end

#left_colorObject



512
513
514
# File 'lib/write_xlsx/format.rb', line 512

def left_color
  border_style.left_color
end

#left_color=(value) ⇒ Object



516
517
518
# File 'lib/write_xlsx/format.rb', line 516

def left_color=(value)
  border_style.left_color = value
end

#lockedObject

Protection properties



355
356
357
# File 'lib/write_xlsx/format.rb', line 355

def locked
  protection_style.locked
end

#locked=(value) ⇒ Object



359
360
361
# File 'lib/write_xlsx/format.rb', line 359

def locked=(value)
  protection_style.locked = value
end

#merge_rangeObject



430
431
432
# File 'lib/write_xlsx/format.rb', line 430

def merge_range
  alignment_style.merge_range
end

#merge_range=(value) ⇒ Object



434
435
436
# File 'lib/write_xlsx/format.rb', line 434

def merge_range=(value)
  alignment_style.merge_range = value
end

#num_formatObject

Number format properties



181
182
183
# File 'lib/write_xlsx/format.rb', line 181

def num_format
  number_format_style.format_code
end

#num_format=(value) ⇒ Object



185
186
187
# File 'lib/write_xlsx/format.rb', line 185

def num_format=(value)
  number_format_style.format_code = value
end

#num_format_indexObject



189
190
191
# File 'lib/write_xlsx/format.rb', line 189

def num_format_index
  number_format_style.index
end

#num_format_index=(value) ⇒ Object



193
194
195
# File 'lib/write_xlsx/format.rb', line 193

def num_format_index=(value)
  number_format_style.index = value.to_i
end

#number_format_styleObject



106
107
108
# File 'lib/write_xlsx/format.rb', line 106

def number_format_style
  NumberFormatStyle.new(self)
end

#outline?Boolean

Returns:

  • (Boolean)


1054
1055
1056
# File 'lib/write_xlsx/format.rb', line 1054

def outline?
  ptrue?(font_outline)
end

#patternObject



465
466
467
# File 'lib/write_xlsx/format.rb', line 465

def pattern
  fill_style.pattern
end

#protection_styleObject



102
103
104
# File 'lib/write_xlsx/format.rb', line 102

def protection_style
  ProtectionStyle.new(self)
end

#quote_prefixObject



154
155
156
# File 'lib/write_xlsx/format.rb', line 154

def quote_prefix
  @quote_prefix
end

#quote_prefix=(value) ⇒ Object



158
159
160
# File 'lib/write_xlsx/format.rb', line 158

def quote_prefix=(value)
  @quote_prefix = value
end

#reading_orderObject



438
439
440
# File 'lib/write_xlsx/format.rb', line 438

def reading_order
  alignment_style.reading_order
end

#reading_order=(value) ⇒ Object



442
443
444
# File 'lib/write_xlsx/format.rb', line 442

def reading_order=(value)
  alignment_style.reading_order = value
end

#rightObject



520
521
522
# File 'lib/write_xlsx/format.rb', line 520

def right
  border_style.right
end

#right=(value) ⇒ Object



524
525
526
# File 'lib/write_xlsx/format.rb', line 524

def right=(value)
  border_style.right = value
end

#right_colorObject



528
529
530
# File 'lib/write_xlsx/format.rb', line 528

def right_color
  border_style.right_color
end

#right_color=(value) ⇒ Object



532
533
534
# File 'lib/write_xlsx/format.rb', line 532

def right_color=(value)
  border_style.right_color = value
end

#rotationObject



406
407
408
# File 'lib/write_xlsx/format.rb', line 406

def rotation
  alignment_style.rotation
end

#rotation=(value) ⇒ Object



410
411
412
# File 'lib/write_xlsx/format.rb', line 410

def rotation=(value)
  alignment_style.rotation = value
end

#set_align(location) ⇒ Object

Alignment setters



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'lib/write_xlsx/format.rb', line 678

def set_align(location)
  return unless location

  location = location.downcase

  case location
  when 'left'                         then set_text_h_align(1)
  when 'centre', 'center'             then set_text_h_align(2)
  when 'right'                        then set_text_h_align(3)
  when 'fill'                         then set_text_h_align(4)
  when 'justify'                      then set_text_h_align(5)
  when 'center_across', 'centre_across', 'merge'
    set_text_h_align(6)
  when 'distributed', 'equal_space', 'justify_distributed'
    set_text_h_align(7)
  when 'top'                          then set_text_v_align(1)
  when 'vcentre', 'vcenter'           then set_text_v_align(2)
  when 'bottom'                       then set_text_v_align(3)
  when 'vjustify'                     then set_text_v_align(4)
  when 'vdistributed', 'vequal_space' then set_text_v_align(5)
  end

  self.just_distrib = 1 if location == 'justify_distributed'
end

#set_bg_color(value) ⇒ Object



758
759
760
# File 'lib/write_xlsx/format.rb', line 758

def set_bg_color(value)
  fill_style.bg_color = color(normalize_format_property_value(value))
end

#set_bold(weight = 1) ⇒ Object



635
636
637
# File 'lib/write_xlsx/format.rb', line 635

def set_bold(weight = 1)
  self.bold = weight
end

#set_border(value) ⇒ Object

Border setters



781
782
783
784
785
786
# File 'lib/write_xlsx/format.rb', line 781

def set_border(value)
  set_bottom(value)
  set_top(value)
  set_left(value)
  set_right(value)
end

#set_border_color(value) ⇒ Object



788
789
790
791
792
793
794
# File 'lib/write_xlsx/format.rb', line 788

def set_border_color(value)
  color_value = color(normalize_format_property_value(value))
  self.bottom_color = color_value
  self.top_color    = color_value
  self.left_color   = color_value
  self.right_color  = color_value
end

#set_border_count(value) ⇒ Object



844
845
846
# File 'lib/write_xlsx/format.rb', line 844

def set_border_count(value)
  self.border_count = value
end

#set_border_index(value) ⇒ Object



840
841
842
# File 'lib/write_xlsx/format.rb', line 840

def set_border_index(value)
  self.border_index = value
end

#set_border_info(borders) ⇒ Object



1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'lib/write_xlsx/format.rb', line 1011

def set_border_info(borders)
  key = get_border_key

  if borders[key]
    self.border_index = borders[key]
    @has_border = false
  else
    self.border_index = borders.size
    borders[key] = borders.size
    @has_border = true
  end
end

#set_bottom(value) ⇒ Object



808
809
810
# File 'lib/write_xlsx/format.rb', line 808

def set_bottom(value)
  self.bottom = value
end

#set_bottom_color(value) ⇒ Object



832
833
834
# File 'lib/write_xlsx/format.rb', line 832

def set_bottom_color(value)
  self.bottom_color = color(normalize_format_property_value(value))
end

#set_center_across(_flag = 1) ⇒ Object



707
708
709
# File 'lib/write_xlsx/format.rb', line 707

def set_center_across(_flag = 1)
  set_text_h_align(6)
end

#set_color(value) ⇒ Object



631
632
633
# File 'lib/write_xlsx/format.rb', line 631

def set_color(value)
  self.font_color = color(normalize_format_property_value(value))
end

#set_color_indexed(value = 1) ⇒ Object



667
668
669
# File 'lib/write_xlsx/format.rb', line 667

def set_color_indexed(value = 1)
  self.color_indexed = value
end

#set_diag_border(value) ⇒ Object



812
813
814
# File 'lib/write_xlsx/format.rb', line 812

def set_diag_border(value)
  self.diag_border = value
end

#set_diag_color(value) ⇒ Object



836
837
838
# File 'lib/write_xlsx/format.rb', line 836

def set_diag_color(value)
  self.diag_color = color(normalize_format_property_value(value))
end

#set_diag_type(value) ⇒ Object



816
817
818
# File 'lib/write_xlsx/format.rb', line 816

def set_diag_type(value)
  self.diag_type = value
end

#set_dxf_index(value) ⇒ Object



893
894
895
# File 'lib/write_xlsx/format.rb', line 893

def set_dxf_index(value)
  self.dxf_index = value
end

#set_fg_color(value) ⇒ Object

Fill setters



754
755
756
# File 'lib/write_xlsx/format.rb', line 754

def set_fg_color(value)
  fill_style.fg_color = color(normalize_format_property_value(value))
end

#set_fill_count(value) ⇒ Object



770
771
772
# File 'lib/write_xlsx/format.rb', line 770

def set_fill_count(value)
  fill_style.count = normalize_format_property_value(value)
end

#set_fill_index(value) ⇒ Object



766
767
768
# File 'lib/write_xlsx/format.rb', line 766

def set_fill_index(value)
  fill_style.index = normalize_format_property_value(value)
end

#set_font(value) ⇒ Object

Font setters



611
612
613
# File 'lib/write_xlsx/format.rb', line 611

def set_font(value)
  self.font = normalize_format_property_value(value)
end

#set_font_charset(value) ⇒ Object



619
620
621
# File 'lib/write_xlsx/format.rb', line 619

def set_font_charset(value)
  self.font_charset = value
end

#set_font_condense(value) ⇒ Object



623
624
625
# File 'lib/write_xlsx/format.rb', line 623

def set_font_condense(value)
  self.font_condense = value
end

#set_font_extend(value = 1) ⇒ Object



663
664
665
# File 'lib/write_xlsx/format.rb', line 663

def set_font_extend(value = 1)
  self.font_extend = value
end

#set_font_family(value) ⇒ Object



615
616
617
# File 'lib/write_xlsx/format.rb', line 615

def set_font_family(value)
  self.font_family = value
end

#set_font_info(fonts) ⇒ Object



998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
# File 'lib/write_xlsx/format.rb', line 998

def set_font_info(fonts)
  key = get_font_key

  if fonts[key]
    self.font_index = fonts[key]
    @has_font = false
  else
    self.font_index = fonts.size
    fonts[key] = fonts.size
    @has_font = true
  end
end

#set_font_outline(value = 1) ⇒ Object



655
656
657
# File 'lib/write_xlsx/format.rb', line 655

def set_font_outline(value = 1)
  self.font_outline = value
end

#set_font_script(value) ⇒ Object



651
652
653
# File 'lib/write_xlsx/format.rb', line 651

def set_font_script(value)
  self.font_script = value
end

#set_font_shadow(value = 1) ⇒ Object



659
660
661
# File 'lib/write_xlsx/format.rb', line 659

def set_font_shadow(value = 1)
  self.font_shadow = value
end

#set_font_strikeout(value = 1) ⇒ Object



647
648
649
# File 'lib/write_xlsx/format.rb', line 647

def set_font_strikeout(value = 1)
  self.font_strikeout = value
end

#set_format_properties(*properties) ⇒ Object

Public formatting property API



598
599
600
601
602
603
604
605
606
# File 'lib/write_xlsx/format.rb', line 598

def set_format_properties(*properties) # :nodoc:
  return if properties.empty?

  properties.each do |property|
    property.each do |key, value|
      send("set_#{key}", value)
    end
  end
end

#set_has_fill(value) ⇒ Object



774
775
776
# File 'lib/write_xlsx/format.rb', line 774

def set_has_fill(value)
  self.has_fill = value
end

#set_has_font(value = 1) ⇒ Object



885
886
887
# File 'lib/write_xlsx/format.rb', line 885

def set_has_font(value = 1)
  self.has_font = value
end

#set_hidden(value = 1) ⇒ Object



855
856
857
# File 'lib/write_xlsx/format.rb', line 855

def set_hidden(value = 1)
  self.hidden = value
end

Hyperlink and misc setters



873
874
875
876
877
878
879
# File 'lib/write_xlsx/format.rb', line 873

def set_hyperlink(value)
  self.xf_id = 1

  set_underline(1)
  set_theme(10)
  self.hyperlink = value
end

#set_indent(value = 1) ⇒ Object



743
744
745
# File 'lib/write_xlsx/format.rb', line 743

def set_indent(value = 1)
  self.indent = value
end

#set_italic(value = 1) ⇒ Object



639
640
641
# File 'lib/write_xlsx/format.rb', line 639

def set_italic(value = 1)
  self.italic = value
end

#set_left(value) ⇒ Object



796
797
798
# File 'lib/write_xlsx/format.rb', line 796

def set_left(value)
  self.left = value
end

#set_left_color(value) ⇒ Object



820
821
822
# File 'lib/write_xlsx/format.rb', line 820

def set_left_color(value)
  self.left_color = color(normalize_format_property_value(value))
end

#set_locked(value = 1) ⇒ Object

Protection setters



851
852
853
# File 'lib/write_xlsx/format.rb', line 851

def set_locked(value = 1)
  self.locked = value
end

#set_merge(_merge = 1) ⇒ Object



711
712
713
# File 'lib/write_xlsx/format.rb', line 711

def set_merge(_merge = 1)
  set_text_h_align(6)
end

#set_num_format(format) ⇒ Object

Number format setters



862
863
864
# File 'lib/write_xlsx/format.rb', line 862

def set_num_format(format)
  self.num_format = format
end

#set_num_format_index(value) ⇒ Object



866
867
868
# File 'lib/write_xlsx/format.rb', line 866

def set_num_format_index(value)
  self.num_format_index = value
end

#set_pattern(value) ⇒ Object



762
763
764
# File 'lib/write_xlsx/format.rb', line 762

def set_pattern(value)
  fill_style.pattern = normalize_format_property_value(value)
end

#set_quote_prefix(value = 1) ⇒ Object



881
882
883
# File 'lib/write_xlsx/format.rb', line 881

def set_quote_prefix(value = 1)
  self.quote_prefix = value
end

#set_right(value) ⇒ Object



800
801
802
# File 'lib/write_xlsx/format.rb', line 800

def set_right(value)
  self.right = value
end

#set_right_color(value) ⇒ Object



824
825
826
# File 'lib/write_xlsx/format.rb', line 824

def set_right_color(value)
  self.right_color = color(normalize_format_property_value(value))
end

#set_rotation(rotation) ⇒ Object



715
716
717
718
719
720
721
722
723
724
725
# File 'lib/write_xlsx/format.rb', line 715

def set_rotation(rotation)
  if rotation == 270
    rotation = 255
  elsif rotation.between?(-90, 90)
    rotation = -rotation + 90 if rotation < 0
  else
    raise "Rotation #{rotation} outside range: -90 <= angle <= 90"
  end

  self.rotation = rotation
end

#set_shrink(value = 1) ⇒ Object



747
748
749
# File 'lib/write_xlsx/format.rb', line 747

def set_shrink(value = 1)
  self.shrink = value
end

#set_size(value) ⇒ Object



627
628
629
# File 'lib/write_xlsx/format.rb', line 627

def set_size(value)
  self.size = value
end

#set_text_h_align(value) ⇒ Object



727
728
729
# File 'lib/write_xlsx/format.rb', line 727

def set_text_h_align(value)
  self.text_h_align = value
end

#set_text_justlast(value = 1) ⇒ Object



739
740
741
# File 'lib/write_xlsx/format.rb', line 739

def set_text_justlast(value = 1)
  self.text_justlast = value
end

#set_text_v_align(value) ⇒ Object



731
732
733
# File 'lib/write_xlsx/format.rb', line 731

def set_text_v_align(value)
  self.text_v_align = value
end

#set_text_wrap(value = 1) ⇒ Object



735
736
737
# File 'lib/write_xlsx/format.rb', line 735

def set_text_wrap(value = 1)
  self.text_wrap = value
end

#set_theme(value = 1) ⇒ Object



671
672
673
# File 'lib/write_xlsx/format.rb', line 671

def set_theme(value = 1)
  self.theme = value
end

#set_top(value) ⇒ Object



804
805
806
# File 'lib/write_xlsx/format.rb', line 804

def set_top(value)
  self.top = value
end

#set_top_color(value) ⇒ Object



828
829
830
# File 'lib/write_xlsx/format.rb', line 828

def set_top_color(value)
  self.top_color = color(normalize_format_property_value(value))
end

#set_underline(value = 1) ⇒ Object



643
644
645
# File 'lib/write_xlsx/format.rb', line 643

def set_underline(value = 1)
  self.underline = value
end

#set_valign(location) ⇒ Object



703
704
705
# File 'lib/write_xlsx/format.rb', line 703

def set_valign(location)
  set_align(location)
end

#set_xf_id(value) ⇒ Object



897
898
899
# File 'lib/write_xlsx/format.rb', line 897

def set_xf_id(value)
  self.xf_id = value
end

#set_xf_index(value) ⇒ Object



889
890
891
# File 'lib/write_xlsx/format.rb', line 889

def set_xf_index(value)
  self.xf_index = value
end

#shadow?Boolean

Returns:

  • (Boolean)


1058
1059
1060
# File 'lib/write_xlsx/format.rb', line 1058

def shadow?
  ptrue?(font_shadow)
end

#shrinkObject



422
423
424
# File 'lib/write_xlsx/format.rb', line 422

def shrink
  alignment_style.shrink
end

#shrink=(value) ⇒ Object



426
427
428
# File 'lib/write_xlsx/format.rb', line 426

def shrink=(value)
  alignment_style.shrink = value
end

#sizeObject



208
209
210
# File 'lib/write_xlsx/format.rb', line 208

def size
  font_style.size
end

#size=(value) ⇒ Object



212
213
214
# File 'lib/write_xlsx/format.rb', line 212

def size=(value)
  font_style.size = value
end

#stateObject

Style facade accessors



82
83
84
# File 'lib/write_xlsx/format.rb', line 82

def state
  @state
end

#strikeout?Boolean

Returns:

  • (Boolean)


1050
1051
1052
# File 'lib/write_xlsx/format.rb', line 1050

def strikeout?
  ptrue?(font_strikeout)
end

#text_h_alignObject

Alignment properties



374
375
376
# File 'lib/write_xlsx/format.rb', line 374

def text_h_align
  alignment_style.horizontal
end

#text_h_align=(value) ⇒ Object



378
379
380
# File 'lib/write_xlsx/format.rb', line 378

def text_h_align=(value)
  alignment_style.horizontal = value
end

#text_justlastObject



398
399
400
# File 'lib/write_xlsx/format.rb', line 398

def text_justlast
  alignment_style.justlast
end

#text_justlast=(value) ⇒ Object



402
403
404
# File 'lib/write_xlsx/format.rb', line 402

def text_justlast=(value)
  alignment_style.justlast = value
end

#text_v_alignObject



390
391
392
# File 'lib/write_xlsx/format.rb', line 390

def text_v_align
  alignment_style.vertical
end

#text_v_align=(value) ⇒ Object



394
395
396
# File 'lib/write_xlsx/format.rb', line 394

def text_v_align=(value)
  alignment_style.vertical = value
end

#text_wrapObject



382
383
384
# File 'lib/write_xlsx/format.rb', line 382

def text_wrap
  alignment_style.wrap
end

#text_wrap=(value) ⇒ Object



386
387
388
# File 'lib/write_xlsx/format.rb', line 386

def text_wrap=(value)
  alignment_style.wrap = value
end

#themeObject



328
329
330
# File 'lib/write_xlsx/format.rb', line 328

def theme
  font_style.theme
end

#theme=(value) ⇒ Object



332
333
334
# File 'lib/write_xlsx/format.rb', line 332

def theme=(value)
  font_style.theme = value
end

#topObject



536
537
538
# File 'lib/write_xlsx/format.rb', line 536

def top
  border_style.top
end

#top=(value) ⇒ Object



540
541
542
# File 'lib/write_xlsx/format.rb', line 540

def top=(value)
  border_style.top = value
end

#top_colorObject



544
545
546
# File 'lib/write_xlsx/format.rb', line 544

def top_color
  border_style.top_color
end

#top_color=(value) ⇒ Object



548
549
550
# File 'lib/write_xlsx/format.rb', line 548

def top_color=(value)
  border_style.top_color = value
end

#underlineObject



240
241
242
# File 'lib/write_xlsx/format.rb', line 240

def underline
  font_style.underline
end

#underline=(value) ⇒ Object



244
245
246
# File 'lib/write_xlsx/format.rb', line 244

def underline=(value)
  font_style.underline = value
end

#underline?Boolean

Returns:

  • (Boolean)


1062
1063
1064
# File 'lib/write_xlsx/format.rb', line 1062

def underline?
  ptrue?(underline)
end

#write_font(writer, worksheet, dxf_format = nil) ⇒ Object

XML writing entry points



1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
# File 'lib/write_xlsx/format.rb', line 1223

def write_font(writer, worksheet, dxf_format = nil) # :nodoc:
  writer.tag_elements('font') do
    write_condense(writer) if ptrue?(font_condense)
    write_extend(writer)   if ptrue?(font_extend)

    write_font_shapes(writer)

    writer.empty_tag('sz', [['val', size]]) unless dxf_format

    if theme == -1
      # Ignore for excel2003_style
    elsif ptrue?(theme)
      write_color('theme', theme, writer)
    elsif ptrue?(color_indexed)
      write_color('indexed', color_indexed, writer)
    elsif ptrue?(font_color)
      color = worksheet.palette_color(font_color)
      write_color('rgb', color, writer) if color != 'Automatic'
    elsif !ptrue?(dxf_format)
      write_color('theme', 1, writer)
    end

    unless ptrue?(dxf_format)
      writer.empty_tag('name', [['val', font]])
      write_font_family_scheme(writer)
    end
  end
end

#write_font_rpr(writer, worksheet) ⇒ Object

:nodoc:



1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
# File 'lib/write_xlsx/format.rb', line 1252

def write_font_rpr(writer, worksheet) # :nodoc:
  writer.tag_elements('rPr') do
    write_font_shapes(writer)
    writer.empty_tag('sz', [['val', size]])

    if ptrue?(theme)
      write_color('theme', theme, writer)
    elsif ptrue?(font_color)
      color = worksheet.palette_color(font_color)
      write_color('rgb', color, writer)
    else
      write_color('theme', 1, writer)
    end

    writer.empty_tag('rFont', [['val', font]])
    write_font_family_scheme(writer)
  end
end

#xf_attributesObject



1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
# File 'lib/write_xlsx/format.rb', line 1286

def xf_attributes
  attributes = [
    ['numFmtId', num_format_index],
    ['fontId', font_index],
    ['fillId', fill_index],
    ['borderId', border_index],
    ['xfId', xf_id]
  ]

  attributes << ['quotePrefix', 1] if ptrue?(quote_prefix)
  attributes << ['applyNumberFormat', 1] if num_format_index > 0
  attributes << ['applyFont', 1] if font_index > 0 && !ptrue?(hyperlink)
  attributes << ['applyFill', 1] if fill_index > 0
  attributes << ['applyBorder', 1] if border_index > 0

  apply_align, _align = get_align_properties
  attributes << ['applyAlignment', 1] if apply_align || ptrue?(hyperlink)
  attributes << ['applyProtection', 1] if get_protection_properties || ptrue?(hyperlink)

  attributes
end

#xf_idObject



135
136
137
# File 'lib/write_xlsx/format.rb', line 135

def xf_id
  state.xf_id
end

#xf_id=(value) ⇒ Object



139
140
141
# File 'lib/write_xlsx/format.rb', line 139

def xf_id=(value)
  state.xf_id = value
end

#xf_indexObject

Workbook indexes



119
120
121
# File 'lib/write_xlsx/format.rb', line 119

def xf_index
  state.xf_index
end

#xf_index=(value) ⇒ Object



123
124
125
# File 'lib/write_xlsx/format.rb', line 123

def xf_index=(value)
  state.xf_index = value
end