Skip to content

Commit

Permalink
beginnings of comments w/o TDD (^ ^)/
Browse files Browse the repository at this point in the history
  • Loading branch information
Randy Morgan committed May 4, 2012
1 parent f6a2ddc commit a668167
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lib/axlsx/util/constants.rb
Expand Up @@ -184,6 +184,9 @@ module Axlsx
# chart part # chart part
IMAGE_PN = "media/image%d.%s" IMAGE_PN = "media/image%d.%s"


# comment part
COMMENT_PN = "xl/comments%d.xml"

# location of schema files for validation # location of schema files for validation
SCHEMA_BASE = File.dirname(__FILE__)+'/../../schema/' SCHEMA_BASE = File.dirname(__FILE__)+'/../../schema/'


Expand Down
14 changes: 14 additions & 0 deletions lib/axlsx/workbook/workbook.rb
Expand Up @@ -12,6 +12,7 @@ module Axlsx
require 'axlsx/workbook/worksheet/conditional_formatting_rule.rb' require 'axlsx/workbook/worksheet/conditional_formatting_rule.rb'
require 'axlsx/workbook/worksheet/row.rb' require 'axlsx/workbook/worksheet/row.rb'
require 'axlsx/workbook/worksheet/col.rb' require 'axlsx/workbook/worksheet/col.rb'
require 'axlsx/workbook/worksheet/comments.rb'
require 'axlsx/workbook/worksheet/worksheet.rb' require 'axlsx/workbook/worksheet/worksheet.rb'
require 'axlsx/workbook/shared_strings_table.rb' require 'axlsx/workbook/shared_strings_table.rb'
require 'axlsx/workbook/worksheet/table.rb' require 'axlsx/workbook/worksheet/table.rb'
Expand Down Expand Up @@ -90,6 +91,14 @@ def use_shared_strings=(v)
attr_reader :tables 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 # The styles associated with this workbook
# @note The recommended way to manage styles is Styles#add_style # @note The recommended way to manage styles is Styles#add_style
# @see Style#add_style # @see Style#add_style
Expand Down Expand Up @@ -123,7 +132,12 @@ def initialize(options={})
@drawings = SimpleTypedList.new Drawing @drawings = SimpleTypedList.new Drawing
@charts = SimpleTypedList.new Chart @charts = SimpleTypedList.new Chart
@images = SimpleTypedList.new Pic @images = SimpleTypedList.new Pic

# Are these even used????? Check package serialization parts
@tables = SimpleTypedList.new Table @tables = SimpleTypedList.new Table
@comments = SimpleTypedList.new Comments


@use_autowidth = true @use_autowidth = true


self.date1904= !options[:date1904].nil? && options[:date1904] self.date1904= !options[:date1904].nil? && options[:date1904]
Expand Down
115 changes: 115 additions & 0 deletions 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
23 changes: 16 additions & 7 deletions lib/axlsx/workbook/worksheet/worksheet.rb
Expand Up @@ -102,7 +102,7 @@ def initialize(wb, options={})
@merged_cells = [] @merged_cells = []
@auto_fit_data = [] @auto_fit_data = []
@conditional_formattings = [] @conditional_formattings = []

@comments = Comments.new(self)
@selected = false @selected = false
@show_gridlines = true @show_gridlines = true
self.name = "Sheet" + (index+1).to_s self.name = "Sheet" + (index+1).to_s
Expand Down Expand Up @@ -394,6 +394,12 @@ def add_table(ref, options={})
table table
end end



# Shortcut to comments#add_comment
def add_comment(options={})
@comments.add_comment(options)
end

# Adds a media item to the worksheets drawing # Adds a media item to the worksheets drawing
# @param [Class] media_type # @param [Class] media_type
# @option options [] unknown # @option options [] unknown
Expand Down Expand Up @@ -437,12 +443,15 @@ def to_xml_string
# The worksheet relationships. This is managed automatically by the worksheet # The worksheet relationships. This is managed automatically by the worksheet
# @return [Relationships] # @return [Relationships]
def relationships def relationships
r = Relationships.new r = Relationships.new
@tables.each do |table| @tables.each do |table|
r << Relationship.new(TABLE_R, "../#{table.pn}") r << Relationship.new(TABLE_R, "../#{table.pn}")
end end
r << Relationship.new(DRAWING_R, "../#{@drawing.pn}") if @drawing @comments.comment_list.each do |comment|
r r << Relationship.new(COMMENT_R, "#{comment.pn}")
end
r << Relationship.new(DRAWING_R, "../#{@drawing.pn}") if @drawing
r
end end


# Returns the cell or cells defined using excel style A1:B3 references. # Returns the cell or cells defined using excel style A1:B3 references.
Expand Down
6 changes: 6 additions & 0 deletions test/workbook/worksheet/tc_worksheet.rb
Expand Up @@ -43,6 +43,7 @@ def test_initialization_options
assert_equal(optioned.name, 'bob') assert_equal(optioned.name, 'bob')
assert_equal(optioned.selected, true) assert_equal(optioned.selected, true)
assert_equal(optioned.show_gridlines, false) assert_equal(optioned.show_gridlines, false)

end end




Expand Down Expand Up @@ -274,11 +275,16 @@ def test_valid_with_optional_elements
end end


def test_relationships def test_relationships
@ws.add_row [1,2,3]
assert(@ws.relationships.empty?, "No Drawing relationship until you add a chart") assert(@ws.relationships.empty?, "No Drawing relationship until you add a chart")
c = @ws.add_chart Axlsx::Pie3DChart c = @ws.add_chart Axlsx::Pie3DChart
assert_equal(@ws.relationships.size, 1, "adding a chart creates the relationship") assert_equal(@ws.relationships.size, 1, "adding a chart creates the relationship")
c = @ws.add_chart Axlsx::Pie3DChart c = @ws.add_chart Axlsx::Pie3DChart
assert_equal(@ws.relationships.size, 1, "multiple charts still only result in one relationship") 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 end




Expand Down

0 comments on commit a668167

Please sign in to comment.