Class: Spreadsheet::Format

Inherits:
Object
  • Object
show all
Includes:
Datatypes, Encodings
Defined in:
lib/spreadsheet/format.rb

Overview

Formatting data

Direct Known Subclasses

Excel::Writer::Format

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Datatypes

append_features

Methods included from Compatibility

#ivar_name, #method_name

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ Format

Returns a new instance of Format.

Yields:

  • (_self)

Yield Parameters:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/spreadsheet/format.rb', line 90

def initialize opts = {}
  @font = Font.new client("Arial", "UTF-8"), family: :swiss
  @number_format = client "GENERAL", "UTF-8"
  @rotation = 0
  @pattern = 0
  @bottom_color = :black
  @top_color = :black
  @left_color = :black
  @right_color = :black
  @diagonal_color = :black
  @pattern_fg_color = :border
  @pattern_bg_color = :pattern_bg
  @regexes = {
    date: Regexp.new(client("[YMD]|d{2}|m{3}|y{2}", "UTF-8")),
    date_or_time: Regexp.new(client("[hmsYMD]", "UTF-8")),
    datetime: Regexp.new(client("([YMD].*[HS])|([HS].*[YMD])", "UTF-8")),
    time: Regexp.new(client("[hms]", "UTF-8")),
    number: Regexp.new(client("([#]|0+)", "UTF-8")),
    locale: Regexp.new(client(/\A\[\$-\S+\]/.to_s, "UTF-8"))
  }

  # Temp code to prevent merged formats in non-merged cells.
  @used_merge = 0
  update_format(opts)

  yield self if block_given?
end

Instance Attribute Details

#fontObject

Returns the value of attribute font.



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

def font
  @font
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#number_formatObject

Returns the value of attribute number_format.



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

def number_format
  @number_format
end

#patternObject

Returns the value of attribute pattern.



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

def pattern
  @pattern
end

#rotationObject

Text rotation



89
90
91
# File 'lib/spreadsheet/format.rb', line 89

def rotation
  @rotation
end

#used_mergeObject

Returns the value of attribute used_merge.



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

def used_merge
  @used_merge
end

Instance Method Details

#align=(location) ⇒ Object

Combined method for both horizontal and vertical alignment. Sets the first valid value (e.g. Format#align = :justify only sets the horizontal alignment. Use one of the aliases prefixed with :v if you need to disambiguate.)

This is essentially a backward-compatibility method and may be removed at some point in the future.



134
135
136
137
138
139
140
141
142
# File 'lib/spreadsheet/format.rb', line 134

def align= location
  self.horizontal_align = location
rescue ArgumentError
  self.vertical_align = begin
    location
  rescue
    ArgumentError
  end
end

#borderObject

Returns an Array containing the line styles of the four borders: bottom, top, right, left



147
148
149
# File 'lib/spreadsheet/format.rb', line 147

def border
  [bottom, top, right, left]
end

#border=(style) ⇒ Object

Set same line style on all four borders at once (left, right, top, bottom)



153
154
155
# File 'lib/spreadsheet/format.rb', line 153

def border=(style)
  [:bottom=, :top=, :right=, :left=].each { |writer| send writer, style }
end

#border_colorObject

Returns an Array containing the colors of the four borders: bottom, top, right, left



160
161
162
# File 'lib/spreadsheet/format.rb', line 160

def border_color
  [@bottom_color, @top_color, @right_color, @left_color]
end

#border_color=(color) ⇒ Object

Set all four border colors to color (left, right, top, bottom)



166
167
168
169
170
# File 'lib/spreadsheet/format.rb', line 166

def border_color=(color)
  [:bottom_color=, :top_color=, :right_color=, :left_color=].each do |writer|
    send writer, color
  end
end

#bottom_colorObject

Color attributes



51
52
53
# File 'lib/spreadsheet/format.rb', line 51

colors :bottom_color, :top_color, :left_color, :right_color,
:pattern_fg_color, :pattern_bg_color,
:diagonal_color

#center_across!Object Also known as: merge!

Backward compatibility method. May disappear at some point in the future.



190
191
192
# File 'lib/spreadsheet/format.rb', line 190

def center_across!
  self.horizontal_align = :merge
end

#cross_downObject

You can set the following boolean attributes:

#cross_down

Draws a Line from the top-left to the bottom-right corner of a cell.

#cross_up

Draws a Line from the bottom-left to the top-right corner of a cell.

#hidden

The cell is hidden.

#locked

The cell is locked.

#merge_range

The cell is in a merged range.

#shrink

Shrink the contents to fit the cell.

#text_justlast

Force the last line of a cell to be justified. This probably makes sense if horizontal_align = :justify

#left

Apply a border style to the left of the cell.

#right

Apply a border style to the right of the cell.

#top

Apply a border style at the top of the cell.

#bottom

Apply a border style at the bottom of the cell.

#rotation_stacked

Characters in the cell are stacked on top of each other. Excel will ignore other rotation values if this is set.



30
31
32
# File 'lib/spreadsheet/format.rb', line 30

boolean :cross_down, :cross_up, :hidden, :locked,
:merge_range, :shrink, :text_justlast, :text_wrap,
:rotation_stacked

#date?Boolean

Is the cell formatted as a Date?

Returns:

  • (Boolean)


196
197
198
# File 'lib/spreadsheet/format.rb', line 196

def date?
  !number? && matches_format?(:date)
end

#date_or_time?Boolean

Is the cell formatted as a Date or Time?

Returns:

  • (Boolean)


202
203
204
# File 'lib/spreadsheet/format.rb', line 202

def date_or_time?
  !number? && matches_format?(:date_or_time)
end

#datetime?Boolean

Is the cell formatted as a DateTime?

Returns:

  • (Boolean)


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

def datetime?
  !number? && matches_format?(:datetime)
end

#horizontal_alignObject

Horizontal alignment Valid values: :default, :left, :center, :right, :fill, :justify, :merge,

:distributed

Default: :default



73
74
75
76
77
# File 'lib/spreadsheet/format.rb', line 73

enum :horizontal_align, :default, :left, :center, :right, :fill, :justify,
:merge, :distributed,
center: :centre,
merge: [:center_across, :centre_across],
distributed: :equal_space

#indent_levelObject Also known as: indent

Indentation level



65
# File 'lib/spreadsheet/format.rb', line 65

enum :indent_level, 0, Integer

#matches_format?(name) ⇒ Boolean

Does the cell match a particular preset format?

Returns:

  • (Boolean)


226
227
228
229
230
231
# File 'lib/spreadsheet/format.rb', line 226

def matches_format?(name)
  # Excel number formats may optionally include a locale identifier like this:
  #   [$-409]
  format = @number_format.to_s.sub(@regexes[:locale], "")
  !!@regexes[name].match(format)
end

#number?Boolean

Is the cell formatted as a number?

Returns:

  • (Boolean)


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

def number?
  matches_format?(:number)
end

#text_directionObject Also known as: reading_order

Text direction Valid values: :context, :left_to_right, :right_to_left Default: :context



58
59
60
# File 'lib/spreadsheet/format.rb', line 58

enum :text_direction, :context, :left_to_right, :right_to_left,
left_to_right: [:ltr, :l2r],
right_to_left: [:rtl, :r2l]

#time?Boolean

Is the cell formatted as a Time?

Returns:

  • (Boolean)


214
215
216
# File 'lib/spreadsheet/format.rb', line 214

def time?
  !number? && matches_format?(:time)
end

#update_format(opts = {}) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/spreadsheet/format.rb', line 118

def update_format(opts = {})
  opts.each do |attribute, value|
    writer = "#{attribute}="
    @font.respond_to?(writer) ? @font.send(writer, value) : send(writer, value)
  end
  self
end

#vertical_alignObject

Vertical alignment Valid values: :bottom, :top, :middle, :justify, :distributed Default: :bottom



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

enum :vertical_align, :bottom, :top, :middle, :justify, :distributed,
distributed: [:vdistributed, :vequal_space, :equal_space],
justify: :vjustify,
middle: [:vcenter, :vcentre, :center, :centre]