summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/workbook/worksheet/comment.rb
blob: 4275cf8ee8fe39a58bf2524095293fe5876998da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# frozen_string_literal: true

module Axlsx
  # A comment is the text data for a comment
  class Comment
    include Axlsx::OptionsParser
    include Axlsx::Accessors

    # Creates a new comment object
    # @param [Comments] comments The comment collection this comment belongs to
    # @param [Hash] options
    # @option [String] author the author of the comment
    # @option [String] text The text for the comment
    # @option [String] ref The refence (e.g. 'A3' where this comment will be anchored.
    # @option [Boolean] visible This controls the visiblity of the associated vml_shape.
    def initialize(comments, options = {})
      raise ArgumentError, "A comment needs a parent comments object" unless comments.is_a?(Comments)

      @visible = true
      @comments = comments
      parse_options options
      yield self if block_given?
    end

    string_attr_accessor :text, :author
    boolean_attr_accessor :visible

    # The owning Comments object
    # @return [Comments]
    attr_reader :comments

    # The string based cell position reference (e.g. 'A1') that determines the positioning of this comment
    # @return [String|Cell]
    attr_reader :ref

    # TODO
    # r (Rich Text Run)
    # rPh (Phonetic Text Run)
    # phoneticPr (Phonetic Properties)

    # The vml shape that will render this comment
    # @return [VmlShape]
    def vml_shape
      @vml_shape ||= initialize_vml_shape
    end

    # The index of this author in a unique sorted list of all authors in
    # the comment.
    # @return [Integer]
    def author_index
      @comments.authors.index(author)
    end

    # @see ref
    def ref=(v)
      Axlsx::DataTypeValidator.validate :comment_ref, [String, Cell], v
      @ref = v if v.is_a?(String)
      @ref = v.r if v.is_a?(Cell)
    end

    # serialize the object
    # @param [String] str
    # @return [String]
    def to_xml_string(str = +'')
      author = @comments.authors[author_index]
      str << '<comment ref="' << ref << '" authorId="' << author_index.to_s << '">'
      str << '<text>'
      unless author.to_s == ""
        str << '<r><rPr><b/><color indexed="81"/></rPr>'
        str << "<t>" << ::CGI.escapeHTML(author.to_s) << ":\n</t></r>"
      end
      str << '<r>'
      str << '<rPr><color indexed="81"/></rPr>'
      str << '<t>' << ::CGI.escapeHTML(text) << '</t></r></text>'
      str << '</comment>'
    end

    private

    # initialize the vml shape based on this comment's ref/position in the worksheet.
    # by default, all columns are 5 columns wide and 5 rows high
    def initialize_vml_shape
      pos = Axlsx.name_to_indices(ref)
      @vml_shape = VmlShape.new(row: pos[1], column: pos[0], visible: @visible) do |vml|
        vml.left_column = vml.column
        vml.right_column = vml.column + 2
        vml.top_row = vml.row
        vml.bottom_row = vml.row + 4
      end
    end
  end
end