Class: Writeexcel::Worksheet::Comment

Inherits:
Object
  • Object
show all
Defined in:
lib/writeexcel/comments.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/writeexcel/comments.rb', line 49

def initialize(worksheet, row, col, string, options = {})
  @worksheet = worksheet
  @row, @col = row, col
  @params = params_with(options)
  @string, @params[:encoding] = string_and_encoding(string, @params[:encoding], 'comment')

  # Limit the string to the max number of chars (not bytes).
  max_len = 32767
  max_len = max_len * 2 if @params[:encoding] != 0

  if @string.bytesize > max_len
    @string = @string[0 .. max_len]
  end
  @encoding        = @params[:encoding]
  @author          = @params[:author]
  @author_encoding = @params[:author_encoding]
  @visible         = @params[:visible]
  @color           = @params[:color]
  @vertices        = calc_vertices
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author


47
48
49
# File 'lib/writeexcel/comments.rb', line 47

def author
  @author
end

#author_encodingObject (readonly)

Returns the value of attribute author_encoding


47
48
49
# File 'lib/writeexcel/comments.rb', line 47

def author_encoding
  @author_encoding
end

#colObject (readonly)

Returns the value of attribute col


47
48
49
# File 'lib/writeexcel/comments.rb', line 47

def col
  @col
end

#colorObject (readonly)

Returns the value of attribute color


47
48
49
# File 'lib/writeexcel/comments.rb', line 47

def color
  @color
end

#encodingObject (readonly)

Returns the value of attribute encoding


47
48
49
# File 'lib/writeexcel/comments.rb', line 47

def encoding
  @encoding
end

#rowObject (readonly)

Returns the value of attribute row


47
48
49
# File 'lib/writeexcel/comments.rb', line 47

def row
  @row
end

#stringObject (readonly)

Returns the value of attribute string


47
48
49
# File 'lib/writeexcel/comments.rb', line 47

def string
  @string
end

#verticesObject (readonly)

Returns the value of attribute vertices


47
48
49
# File 'lib/writeexcel/comments.rb', line 47

def vertices
  @vertices
end

#visibleObject (readonly)

Returns the value of attribute visible


47
48
49
# File 'lib/writeexcel/comments.rb', line 47

def visible
  @visible
end

Instance Method Details

#obj_comment_record(obj_id) ⇒ Object

OBJ record that is part of cell comments.

obj_id      # Object ID number.

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/writeexcel/comments.rb', line 157

def obj_comment_record(obj_id)   #:nodoc:
  record      = 0x005D   # Record identifier
  length      = 0x0034   # Bytes to follow

  obj_type    = 0x0019   # Object type (comment).
  data        = ''       # Record data.

  sub_record  = 0x0000   # Sub-record identifier.
  sub_length  = 0x0000   # Length of sub-record.
  sub_data    = ''       # Data of sub-record.
  options     = 0x4011
  reserved    = 0x0000

  # Add ftCmo (common object data) subobject
  sub_record     = 0x0015   # ftCmo
  sub_length     = 0x0012
  sub_data       = [obj_type, obj_id, options, reserved, reserved, reserved].pack( "vvvVVV")
  data           = [sub_record, sub_length].pack("vv") + sub_data

  # Add ftNts (note structure) subobject
  sub_record  = 0x000D   # ftNts
  sub_length  = 0x0016
  sub_data    = [reserved,reserved,reserved,reserved,reserved,reserved].pack( "VVVVVv")
  data        += [sub_record, sub_length].pack("vv") + sub_data

  # Add ftEnd (end of object) subobject
  sub_record  = 0x0000   # ftNts
  sub_length  = 0x0000
  data        += [sub_record, sub_length].pack("vv")

  # Pack the record.
  header      = [record, length].pack("vv")

  header + data
end

#store_comment_record(i, num_objects, num_comments, spid) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/writeexcel/comments.rb', line 70

def store_comment_record(i, num_objects, num_comments, spid)
  str_len  = string.bytesize
  str_len  = str_len / 2 if encoding != 0 # Num of chars not bytes.

  spid = store_comment_mso_drawing_record(i, num_objects, num_comments, spid, visible, color, vertices)
  store_obj_comment(num_objects + i + 1)
  store_mso_drawing_text_box
  store_txo(str_len)
  store_txo_continue_1(string, encoding)
  formats  = [[0, 9], [str_len, 0]]
  store_txo_continue_2(formats)
  spid
end

#store_mso_opt_comment(spid, visible = nil, colour = 0x50) ⇒ Object

Write the Escher Opt record that is part of MSODRAWING.


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/writeexcel/comments.rb', line 127

def store_mso_opt_comment(spid, visible = nil, colour = 0x50)   #:nodoc:
  type        = 0xF00B
  version     = 3
  instance    = 9
  data        = ''
  length      = 54

  # Use the visible flag if set by the user or else use the worksheet value.
  # Note that the value used is the opposite of Comment#note_record.
  #
  if visible
    visible = visible != 0       ? 0x0000 : 0x0002
  else
    visible = @worksheet.comments_visible? ? 0x0000 : 0x0002
  end

  data = [spid].pack('V')                            +
  ['0000BF00080008005801000000008101'].pack("H*") +
  [colour].pack("C")                              +
  ['000008830150000008BF011000110001'+'02000000003F0203000300BF03'].pack("H*")  +
  [visible].pack('v')                             +
  ['0A00'].pack('H*')

  @worksheet.add_mso_generic(type, version, instance, data, length)
end

#store_note_record(obj_id) ⇒ Object

Write the worksheet NOTE record that is part of cell comments.


87
88
89
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
117
118
119
120
121
122
# File 'lib/writeexcel/comments.rb', line 87

def store_note_record(obj_id)   #:nodoc:
  comment_author = author
  comment_author_enc = author_encoding
  ruby_19 { comment_author = [comment_author].pack('a*') if comment_author.ascii_only? }
  record      = 0x001C               # Record identifier
  length      = 0x000C               # Bytes to follow

  comment_author     = '' unless comment_author
  comment_author_enc = 0  unless author_encoding

  # Use the visible flag if set by the user or else use the worksheet value.
  # The flag is also set in store_mso_opt_comment() but with the opposite
  # value.
  if visible
    comment_visible = visible != 0                 ? 0x0002 : 0x0000
  else
    comment_visible = @worksheet.comments_visible? ? 0x0002 : 0x0000
  end

  # Get the number of chars in the author string (not bytes).
  num_chars  = comment_author.bytesize
  num_chars  = num_chars / 2 if comment_author_enc != 0 && comment_author_enc

  # Null terminate the author string.
  comment_author =
    ruby_18 { comment_author + "\0" } ||
    ruby_19 { comment_author.force_encoding('BINARY') + "\0".force_encoding('BINARY') }

  # Pack the record.
  data    = [@row, @col, comment_visible, obj_id, num_chars, comment_author_enc].pack("vvvvvC")

  length  = data.bytesize + comment_author.bytesize
  header  = [record, length].pack("vv")

  append(header, data, comment_author)
end