Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

beginnings of comments w/o TDD (^ ^)/

  • Loading branch information...
commit a668167e555c0d69b6ac943695e0b5cf24e054a9 1 parent f6a2ddc
@randym randym authored
View
3  lib/axlsx/util/constants.rb
@@ -184,6 +184,9 @@ module Axlsx
# chart part
IMAGE_PN = "media/image%d.%s"
+ # comment part
+ COMMENT_PN = "xl/comments%d.xml"
+
# location of schema files for validation
SCHEMA_BASE = File.dirname(__FILE__)+'/../../schema/'
View
14 lib/axlsx/workbook/workbook.rb
@@ -12,6 +12,7 @@ module Axlsx
require 'axlsx/workbook/worksheet/conditional_formatting_rule.rb'
require 'axlsx/workbook/worksheet/row.rb'
require 'axlsx/workbook/worksheet/col.rb'
+require 'axlsx/workbook/worksheet/comments.rb'
require 'axlsx/workbook/worksheet/worksheet.rb'
require 'axlsx/workbook/shared_strings_table.rb'
require 'axlsx/workbook/worksheet/table.rb'
@@ -90,6 +91,14 @@ def use_shared_strings=(v)
attr_reader :tables
+ # A colllection of comments associated with this workbook
+ # @note The recommended way to manage comments is Worksheet#add_comment
+ # @see Worksheet#add_comment
+ # @see Comment
+ # @return [SimpleTypedList]
+ attr_reader :comments
+
+
# The styles associated with this workbook
# @note The recommended way to manage styles is Styles#add_style
# @see Style#add_style
@@ -123,7 +132,12 @@ def initialize(options={})
@drawings = SimpleTypedList.new Drawing
@charts = SimpleTypedList.new Chart
@images = SimpleTypedList.new Pic
+
+ # Are these even used????? Check package serialization parts
@tables = SimpleTypedList.new Table
+ @comments = SimpleTypedList.new Comments
+
+
@use_autowidth = true
self.date1904= !options[:date1904].nil? && options[:date1904]
View
115 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
View
23 lib/axlsx/workbook/worksheet/worksheet.rb
@@ -102,7 +102,7 @@ def initialize(wb, options={})
@merged_cells = []
@auto_fit_data = []
@conditional_formattings = []
-
+ @comments = Comments.new(self)
@selected = false
@show_gridlines = true
self.name = "Sheet" + (index+1).to_s
@@ -394,6 +394,12 @@ def add_table(ref, options={})
table
end
+
+ # Shortcut to comments#add_comment
+ def add_comment(options={})
+ @comments.add_comment(options)
+ end
+
# Adds a media item to the worksheets drawing
# @param [Class] media_type
# @option options [] unknown
@@ -437,12 +443,15 @@ def to_xml_string
# The worksheet relationships. This is managed automatically by the worksheet
# @return [Relationships]
def relationships
- r = Relationships.new
- @tables.each do |table|
- r << Relationship.new(TABLE_R, "../#{table.pn}")
- end
- r << Relationship.new(DRAWING_R, "../#{@drawing.pn}") if @drawing
- r
+ r = Relationships.new
+ @tables.each do |table|
+ r << Relationship.new(TABLE_R, "../#{table.pn}")
+ end
+ @comments.comment_list.each do |comment|
+ r << Relationship.new(COMMENT_R, "#{comment.pn}")
+ end
+ r << Relationship.new(DRAWING_R, "../#{@drawing.pn}") if @drawing
+ r
end
# Returns the cell or cells defined using excel style A1:B3 references.
View
6 test/workbook/worksheet/tc_worksheet.rb
@@ -43,6 +43,7 @@ def test_initialization_options
assert_equal(optioned.name, 'bob')
assert_equal(optioned.selected, true)
assert_equal(optioned.show_gridlines, false)
+
end
@@ -274,11 +275,16 @@ def test_valid_with_optional_elements
end
def test_relationships
+ @ws.add_row [1,2,3]
assert(@ws.relationships.empty?, "No Drawing relationship until you add a chart")
c = @ws.add_chart Axlsx::Pie3DChart
assert_equal(@ws.relationships.size, 1, "adding a chart creates the relationship")
c = @ws.add_chart Axlsx::Pie3DChart
assert_equal(@ws.relationships.size, 1, "multiple charts still only result in one relationship")
+ c = @ws.add_comment :text => 'builder', :author => 'bob', :ref => @ws.rows.last.cells.last
+ assert_equal(@ws.relationships.size, 2, "adding a comment adds a relationship")
+ c = @ws.add_comment :text => 'not that is a comment!', :author => 'travis', :ref => "A1"
+ assert_equal(@ws.relationships.size, 3, "adding multiple comments result in multiple relationships")
end
Please sign in to comment.
Something went wrong with that request. Please try again.