Class: Axlsx::Comments

Inherits:
SimpleTypedList
  • Object
show all
Defined in:
lib/axlsx/workbook/worksheet/comments.rb

Overview

Comments is a collection of Comment objects for a worksheet

Constant Summary

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Comments) initialize(worksheet)

Creates a new Comments object

Parameters:

  • worksheet (Worksheet)

    The sheet that these comments belong to.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
# File 'lib/axlsx/workbook/worksheet/comments.rb', line 29

def initialize(worksheet)
  raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
  super(Comment)
  @worksheet = worksheet
  @vml_drawing = VmlDrawing.new(self)
end

Instance Attribute Details

- (VmlDrawing) vml_drawing (readonly)

the vml_drawing that holds the shapes for comments

Returns:



9
10
11
# File 'lib/axlsx/workbook/worksheet/comments.rb', line 9

def vml_drawing
  @vml_drawing
end

- (Worksheet) worksheet (readonly)

The worksheet that these comments belong to

Returns:



13
14
15
# File 'lib/axlsx/workbook/worksheet/comments.rb', line 13

def worksheet
  @worksheet
end

Instance Method Details

- (Object) add_comment(options = {}) {|last| ... }

Note:

the author, text and ref options are required

Adds a new comment to the worksheet that owns these comments.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • author (String)

    The name of the author for this comment

  • text (String)

    The text for this comment

  • ref (Stirng|Cell)

    The cell that this comment is attached to.

Yields:

  • (last)

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
# File 'lib/axlsx/workbook/worksheet/comments.rb', line 41

def add_comment(options={})
  raise ArgumentError, "Comment require an author" unless options[:author]
  raise ArgumentError, "Comment requires text" unless options[:text]
  raise ArgumentError, "Comment requires ref" unless options[:ref]
  self << Comment.new(self, options)
  yield last if block_given?
  last
end

- (Array) authors

A sorted list of the unique authors in the contained comments

Returns:

  • (Array)


52
53
54
# File 'lib/axlsx/workbook/worksheet/comments.rb', line 52

def authors
  map { |comment| comment.author.to_s }.uniq.sort
end

- (Integer) index

The index of this collection in the workbook. Effectively the index of the worksheet.

Returns:

  • (Integer)


17
18
19
# File 'lib/axlsx/workbook/worksheet/comments.rb', line 17

def index
  @worksheet.index
end

- (String) pn

The part name for this object

Returns:



23
24
25
# File 'lib/axlsx/workbook/worksheet/comments.rb', line 23

def pn
  "#{COMMENT_PN % (index+1)}"
end

- (Array) relationships

The relationships required by this object

Returns:

  • (Array)


58
59
60
61
# File 'lib/axlsx/workbook/worksheet/comments.rb', line 58

def relationships
  [Relationship.new(self, VML_DRAWING_R, "../#{vml_drawing.pn}"),
   Relationship.new(self, COMMENT_R, "../#{pn}")]
end

- (String) to_xml_string(str = "")

serialize the object

Parameters:

  • str (String) (defaults to: "")

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/axlsx/workbook/worksheet/comments.rb', line 66

def to_xml_string(str="")
  str << '<?xml version="1.0" encoding="UTF-8"?>'
  str << ('<comments xmlns="' << XML_NS << '"><authors>')
  authors.each do  |author|
    str << ('<author>' << author.to_s << '</author>')
  end
  str << '</authors><commentList>'
  each do |comment|
    comment.to_xml_string str
  end
  str << '</commentList></comments>'

end