Class: Writexlsx::Package::Comments

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

Constant Summary

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 Method Summary collapse

Methods included from Utility::XmlPrimitives

#r_id_attributes, #write_xml_declaration, #xml_str

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(worksheet) ⇒ Comments

Returns a new instance of Comments.



256
257
258
259
260
261
# File 'lib/write_xlsx/package/comments.rb', line 256

def initialize(worksheet)
  @worksheet = worksheet
  @writer = Package::XMLWriterSimple.new
  @author_ids = {}
  @comments = {}
end

Instance Method Details

#[](row) ⇒ Object



263
264
265
# File 'lib/write_xlsx/package/comments.rb', line 263

def [](row)
  @comments[row]
end

#add(workbook, worksheet, row, col, string, options) ⇒ Object



267
268
269
270
# File 'lib/write_xlsx/package/comments.rb', line 267

def add(workbook, worksheet, row, col, string, options)
  @comments[row] ||= {}
  @comments[row][col] = [workbook, worksheet, row, col, string, options]
end

#assemble_xml_fileObject



284
285
286
287
288
289
290
291
# File 'lib/write_xlsx/package/comments.rb', line 284

def assemble_xml_file
  write_xml_declaration do
    write_comments do
      write_authors(sorted_comments)
      write_comment_list(sorted_comments)
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @comments.empty?
end

#has_comment_in_row?(row) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_comment_in_row?(row)
  !!@comments[row]
end

#set_xml_writer(filename) ⇒ Object



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

def set_xml_writer(filename)
  @writer.set_xml_writer(filename)
end

#sizeObject



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

def size
  sorted_comments.size
end

#sorted_commentsObject



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/write_xlsx/package/comments.rb', line 293

def sorted_comments
  unless @sorted_comments
    @sorted_comments = []
    # We sort the comments by row and column but that isn't strictly required.
    @comments.keys.sort.each do |row|
      @comments[row].keys.sort.each do |col|
        user_options = @comments[row][col]
        comment = Comment.new(*user_options)
        @comments[row][col] = comment

        # Set comment visibility if required and not already user defined.
        @comments[row][col].visible ||= 1 if comments_visible?

        # Set comment author if not already user defined.
        @comments[row][col].author ||= @worksheet.comments_author
        @sorted_comments << @comments[row][col]
      end
    end
  end

  @sorted_comments
end