Class: Writexlsx::Package::Comment

Inherits:
Object
  • Object
show all
Includes:
Utility
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 Utility

Utility::COL_MAX, Utility::ROW_MAX, Utility::SHEETNAME_MAX, Utility::STR_MAX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#absolute_char, #check_dimensions, #check_dimensions_and_update_max_min_values, #check_parameter, #convert_date_time, #dash_types, delete_files, #fill_properties, #float_to_str, #layout_properties, #line_fill_properties, #line_properties, #palette_color, #pixels_to_points, #process_workbook_options, #ptrue?, #put_deprecate_message, #quote_sheetname, #r_id_attributes, #row_col_notation, #shape_style_base, #store_col_max_min_values, #store_row_max_min_values, #substitute_cellref, #underline_attributes, #v_shape_attributes_base, #v_shape_style_base, #value_or_raise, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_div, #write_fill, #write_font, #write_stroke, #write_xml_declaration, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xml_str

Constructor Details

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


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/write_xlsx/package/comments.rb', line 19

def initialize(workbook, worksheet, row, col, string, options = {})
  options ||= {}
  @workbook   = workbook
  @worksheet  = worksheet
  @row, @col  = row, 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


17
18
19
# File 'lib/write_xlsx/package/comments.rb', line 17

def author
  @author
end

#colObject (readonly)

Returns the value of attribute col


16
17
18
# File 'lib/write_xlsx/package/comments.rb', line 16

def col
  @col
end

#colorObject (readonly)

Returns the value of attribute color


16
17
18
# File 'lib/write_xlsx/package/comments.rb', line 16

def color
  @color
end

#rowObject (readonly)

Returns the value of attribute row


16
17
18
# File 'lib/write_xlsx/package/comments.rb', line 16

def row
  @row
end

#stringObject (readonly)

Returns the value of attribute string


16
17
18
# File 'lib/write_xlsx/package/comments.rb', line 16

def string
  @string
end

#verticesObject (readonly)

Returns the value of attribute vertices


16
17
18
# File 'lib/write_xlsx/package/comments.rb', line 16

def vertices
  @vertices
end

#visibleObject

Returns the value of attribute visible


17
18
19
# File 'lib/write_xlsx/package/comments.rb', line 17

def visible
  @visible
end

Instance Method Details

#backgrount_color(color) ⇒ Object


41
42
43
44
45
46
47
48
49
50
# File 'lib/write_xlsx/package/comments.rb', line 41

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

  if color_id == 0
    @color = '#ffffe1'
  else
    rgb = @workbook.palette[color_id - 8]
    @color = "##{rgb_color(rgb)} [#{color_id}]\n"
  end
end

#default_start_col(col) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/write_xlsx/package/comments.rb', line 77

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


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

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


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

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


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

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.


153
154
155
156
157
# File 'lib/write_xlsx/package/comments.rb', line 153

def fill_attributes
  [
   ['color2', '#ffffe1']
  ]
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.


54
55
56
57
58
59
60
# File 'lib/write_xlsx/package/comments.rb', line 54

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

#style_additionObject


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

def style_addition
  ['visibility:', visibility]
end

#typeObject


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

def type
  '#_x0000_t202'
end

#v_shape_attributes(id, z_index) ⇒ Object


112
113
114
115
116
117
# File 'lib/write_xlsx/package/comments.rb', line 112

def v_shape_attributes(id, z_index)
  attr = v_shape_attributes_base(id, z_index)
  attr << ['fillcolor',   color]
  attr << ['o:insetmode', 'auto']
  attr
end

#visibilityObject


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

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

#write_client_dataObject

Write the <x:ClientData> element.


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/write_xlsx/package/comments.rb', line 189

def write_client_data
  attributes = [
                ['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.


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

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

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

#write_shape(writer, id, z_index) ⇒ Object


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/write_xlsx/package/comments.rb', line 127

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.


175
176
177
178
179
180
181
182
183
184
# File 'lib/write_xlsx/package/comments.rb', line 175

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

#writer=(w) ⇒ Object


210
211
212
# File 'lib/write_xlsx/package/comments.rb', line 210

def writer=(w)
  @writer = w
end