summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/workbook/worksheet/comments.rb
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-05-04 16:55:54 +0900
committerRandy Morgan <[email protected]>2012-05-04 16:55:54 +0900
commita668167e555c0d69b6ac943695e0b5cf24e054a9 (patch)
treeb68240d63d32ab50ee369aae61ae3d606fdbdca6 /lib/axlsx/workbook/worksheet/comments.rb
parentf6a2ddc9e84e37d02aefc7f69bb2e3c72698d165 (diff)
downloadcaxlsx-a668167e555c0d69b6ac943695e0b5cf24e054a9.tar.gz
caxlsx-a668167e555c0d69b6ac943695e0b5cf24e054a9.zip
beginnings of comments w/o TDD (^ ^)/
Diffstat (limited to 'lib/axlsx/workbook/worksheet/comments.rb')
-rw-r--r--lib/axlsx/workbook/worksheet/comments.rb115
1 files changed, 115 insertions, 0 deletions
diff --git a/lib/axlsx/workbook/worksheet/comments.rb b/lib/axlsx/workbook/worksheet/comments.rb
new file mode 100644
index 00000000..310adb8f
--- /dev/null
+++ b/lib/axlsx/workbook/worksheet/comments.rb
@@ -0,0 +1,115 @@
+module Axlsx
+
+ class Comments
+
+ # a collection of the comment authors
+ # @return [SimpleTypedList]
+ attr_reader :authors
+
+ # a collection of comment objects
+ # @return [SimpleTypedList]
+ attr_reader :comment_list
+
+
+ # The worksheet that these comments belong to
+ # @return [Worksheet]
+ attr_reader :worksheet
+
+ # Creates a new Comments object
+ # @param [Worksheet] worksheet The sheet that these comments belong to.
+ def initialize(worksheet)
+ raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
+ @worksheet = worksheet
+ @authors = SimpleTypedList.new String
+ @comment_list = SimpleTypedList.new Comment
+ end
+
+ # Adds a new comment to the worksheet that owns these comments.
+ # @note the author, text and ref options are required
+ # @option options [String] author The name of the author for this comment
+ # @option options [String] text The text for this comment
+ # @option options [Stirng|Cell] ref The cell that this comment is attached to.
+ 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]
+ options[:author_index] = @authors.index(options[:author]) || @authors << options[:author]
+ @comment_list << Comment.new(self, options)
+ @comment_list.last
+ end
+
+ def to_xml_string(str="")
+ str << '<?xml version="1.0" encoding="UTF-8"?>'
+ str << '<comments xmlns="' << XML_NS << '">'
+ str << '<authors>'
+ authors.each do |author|
+ str << '<author>' << author.to_s << '</author>'
+ end
+ str << '</authors>'
+ str << '<commentList>'
+ comment_list.each do |comment|
+ comment.to_xml_string str
+ end
+ str << '<commentList></comments>'
+
+ end
+
+ end
+
+ class Comment
+
+ attr_reader :text
+
+ attr_reader :author_index
+
+ attr_reader :comments
+
+ attr_reader :ref
+ # TODO
+ # r (Rich Text Run)
+ # rPh (Phonetic Text Run)
+ # phoneticPr (Phonetic Properties)
+ def initialize(comments, options={})
+ raise ArgumentError, "A comment needs a parent comments object" unless comments.is_a?(Comments)
+ @comments = comments
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ yield self if block_given?
+ end
+
+ def pn
+ "#{COMMENT_PN % (index+1)}"
+ end
+
+ # The index of this comment
+ # @return [Integer]
+ def index
+ @comments.comment_list.index(self)
+ end
+
+ 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
+
+ def text=(v)
+ Axlsx::validate_string(v)
+ @text = v
+ end
+
+ def author_index=(v)
+ Axlsx::validate_unsigned_int(v)
+ @author_index = v
+ end
+
+ def to_xml_string(str = "")
+ str << '<comment ref="' << ref << '" authorId="' << author_index << '">'
+ str << '<t xml:space="preserve">' << text << '</t>'
+ str << '</comment>'
+ end
+
+ end
+
+end