Class: Writexlsx::Package::Comment

Inherits:
Object
  • Object
show all
Includes:
Constants, Utility::CellReference, Utility::Common, Utility::Drawing
Defined in:
lib/write_xlsx/package/comments.rb

Constant Summary collapse

DEFAULT_COLOR =

what color ?

81
DEFAULT_WIDTH =
128
DEFAULT_HEIGHT =
74

Constants included from Constants

Constants::COL_MAX, Constants::ROW_MAX, Constants::SHEETNAME_MAX, Constants::STR_MAX

Constants included from Utility::Common

Utility::Common::PERL_TRUE_VALUES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility::Drawing

#pixels_to_points, #shape_style_base, #v_shape_attributes_base, #v_shape_style_base, #write_anchor, #write_auto_fill, #write_comment_path, #write_div, #write_fill, #write_font, #write_stroke

Methods included from Utility::CellReference

#row_col_notation, #substitute_cellref, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell

Methods included from Utility::Common

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

Constructor Details

#initialize(workbook, worksheet, row, col, string, options = {}) ⇒ Comment

Returns a new instance of Comment.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/write_xlsx/package/comments.rb', line 27

def initialize(workbook, worksheet, row, col, string, options = {})
  options ||= {}
  @workbook    = workbook
  @worksheet   = worksheet
  @row = row
  @col = col
  options_parse(row, col, options)
  @string = string[0, STR_MAX]
  @start_row   ||= default_start_row(row)
  @start_col   ||= default_start_col(col)
  @visible     = options[:visible]
  @x_offset    = options[:x_offset] || default_x_offset(col)
  @y_offset    = options[:y_offset] || default_y_offset(row)
  @x_scale     = options[:x_scale]  || 1
  @y_scale     = options[:y_scale]  || 1
  @width       = (0.5 + ((options[:width]  || DEFAULT_WIDTH)  * @x_scale)).to_i
  @height      = (0.5 + ((options[:height] || DEFAULT_HEIGHT) * @y_scale)).to_i
  @vertices    = @worksheet.position_object_pixels(
    @start_col, @start_row, @x_offset, @y_offset,
    @width, @height
  ) << [@width, @height]
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



25
26
27
# File 'lib/write_xlsx/package/comments.rb', line 25

def author
  @author
end

#colObject (readonly)

Returns the value of attribute col.



23
24
25
# File 'lib/write_xlsx/package/comments.rb', line 23

def col
  @col
end

#colorObject (readonly)

Returns the value of attribute color.



23
24
25
# File 'lib/write_xlsx/package/comments.rb', line 23

def color
  @color
end

#font_familyObject (readonly)

Returns the value of attribute font_family.



24
25
26
# File 'lib/write_xlsx/package/comments.rb', line 24

def font_family
  @font_family
end

#font_sizeObject (readonly)

Returns the value of attribute font_size.



24
25
26
# File 'lib/write_xlsx/package/comments.rb', line 24

def font_size
  @font_size
end

#rowObject (readonly)

Returns the value of attribute row.



23
24
25
# File 'lib/write_xlsx/package/comments.rb', line 23

def row
  @row
end

#stringObject (readonly)

Returns the value of attribute string.



23
24
25
# File 'lib/write_xlsx/package/comments.rb', line 23

def string
  @string
end

#verticesObject (readonly)

Returns the value of attribute vertices.



23
24
25
# File 'lib/write_xlsx/package/comments.rb', line 23

def vertices
  @vertices
end

#visibleObject

Returns the value of attribute visible.



25
26
27
# File 'lib/write_xlsx/package/comments.rb', line 25

def visible
  @visible
end

#writer=(value) ⇒ Object (writeonly)

Sets the attribute writer

Parameters:

  • value

    the value to set the attribute writer to.



221
222
223
# File 'lib/write_xlsx/package/comments.rb', line 221

def writer=(value)
  @writer = value
end

Instance Method Details

#backgrount_color(color) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/write_xlsx/package/comments.rb', line 50

def backgrount_color(color)
  color_id = Format.color(color)

  if color_id.to_s =~ /^#[0-9A-F]{6}/i
    @color = color_id.to_s
  elsif color_id == 0
    @color = '#ffffe1'
  else
    rgb = @workbook.palette[color_id - 8]
    @color = "##{rgb_color(rgb)} [#{color_id}]"
  end
end

#default_start_col(col) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/write_xlsx/package/comments.rb', line 87

def default_start_col(col)
  case col
  when COL_MAX - 3
    COL_MAX - 6
  when COL_MAX - 2
    COL_MAX - 5
  when COL_MAX - 1
    COL_MAX - 4
  else
    col + 1
  end
end

#default_start_row(row) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/write_xlsx/package/comments.rb', line 72

def default_start_row(row)
  case row
  when 0
    0
  when ROW_MAX - 3
    ROW_MAX - 7
  when ROW_MAX - 2
    ROW_MAX - 6
  when ROW_MAX - 1
    ROW_MAX - 5
  else
    row - 1
  end
end

#default_x_offset(col) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/write_xlsx/package/comments.rb', line 100

def default_x_offset(col)
  case col
  when COL_MAX - 3, COL_MAX - 2, COL_MAX - 1
    49
  else
    15
  end
end

#default_y_offset(row) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/write_xlsx/package/comments.rb', line 109

def default_y_offset(row)
  case row
  when 0
    2
  when ROW_MAX - 3, ROW_MAX - 2
    16
  when ROW_MAX - 1
    14
  else
    10
  end
end

#fill_attributesObject

Write the <v:fill> element.



164
165
166
167
168
# File 'lib/write_xlsx/package/comments.rb', line 164

def fill_attributes
  [
    ['color2', '#ffffe1']
  ]
end

#font_nameObject



223
224
225
# File 'lib/write_xlsx/package/comments.rb', line 223

def font_name
  @font
end

#rgb_color(rgb) ⇒ Object

Minor modification to allow comparison testing. Change RGB colors from long format, ffcc00 to short format fc0 used by VML.



65
66
67
68
69
70
# File 'lib/write_xlsx/package/comments.rb', line 65

def rgb_color(rgb)
  r, g, b = rgb
  result = sprintf("%02x%02x%02x", r, g, b)
  result = "#{::Regexp.last_match(1)}#{::Regexp.last_match(2)}#{::Regexp.last_match(3)}" if result =~ /^([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3$/
  result
end

#style_additionObject



134
135
136
# File 'lib/write_xlsx/package/comments.rb', line 134

def style_addition
  ['visibility:', visibility]
end

#typeObject



130
131
132
# File 'lib/write_xlsx/package/comments.rb', line 130

def type
  '#_x0000_t202'
end

#v_shape_attributes(id, z_index) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/write_xlsx/package/comments.rb', line 122

def v_shape_attributes(id, z_index)
  attr = v_shape_attributes_base(id)
  attr << ['style', (v_shape_style_base(z_index, vertices) + style_addition).join]
  attr << ['fillcolor',   color]
  attr << ['o:insetmode', 'auto']
  attr
end

#visibilityObject



157
158
159
# File 'lib/write_xlsx/package/comments.rb', line 157

def visibility
  ptrue?(visible) ? 'visible' : 'hidden'
end

#write_client_dataObject

Write the <x:ClientData> element.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/write_xlsx/package/comments.rb', line 200

def write_client_data
  attributes = [
    %w[ObjectType Note]
  ]

  @writer.tag_elements('x:ClientData', attributes) do
    @writer.empty_tag('x:MoveWithCells')
    @writer.empty_tag('x:SizeWithCells')
    # Write the x:Anchor element.
    write_anchor
    # Write the x:AutoFill element.
    write_auto_fill
    # Write the x:Row element.
    @writer.data_element('x:Row', row)
    # Write the x:Column element.
    @writer.data_element('x:Column', col)
    # Write the x:Visible element.
    @writer.empty_tag('x:Visible') if ptrue?(visible)
  end
end

#write_shadowObject

Write the <v:shadow> element.



173
174
175
176
177
178
179
180
181
# File 'lib/write_xlsx/package/comments.rb', line 173

def write_shadow
  attributes = [
    %w[on t],
    %w[color black],
    %w[obscured t]
  ]

  @writer.empty_tag('v:shadow', attributes)
end

#write_shape(writer, id, z_index) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/write_xlsx/package/comments.rb', line 138

def write_shape(writer, id, z_index)
  @writer = writer

  attributes = v_shape_attributes(id, z_index)

  @writer.tag_elements('v:shape', attributes) do
    # Write the v:fill element.
    write_fill
    # Write the v:shadow element.
    write_shadow
    # Write the v:path element.
    write_comment_path(nil, 'none')
    # Write the v:textbox element.
    write_textbox
    # Write the x:ClientData element.
    write_client_data
  end
end

#write_textboxObject

Write the <v:textbox> element.



186
187
188
189
190
191
192
193
194
195
# File 'lib/write_xlsx/package/comments.rb', line 186

def write_textbox
  attributes = [
    ['style', 'mso-direction-alt:auto']
  ]

  @writer.tag_elements('v:textbox', attributes) do
    # Write the div element.
    write_div('left')
  end
end