diff options
| author | Randy Morgan <[email protected]> | 2013-03-16 11:40:54 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2013-03-16 11:40:54 +0900 |
| commit | 38e1638fa715398429797276058f2c18e9e21e9b (patch) | |
| tree | 30fd423d54033d2a525ebd6092f6da7a1342efbd | |
| parent | 0cb6eec06b6e13df28a6ce9e0b90e4e1ed8d5b28 (diff) | |
| download | caxlsx-38e1638fa715398429797276058f2c18e9e21e9b.tar.gz caxlsx-38e1638fa715398429797276058f2c18e9e21e9b.zip | |
First run at 'hidden' comments WIP
The xml is genrated correcty, but the comment still shows until it is
selected once in the excel ui - so I must be missing something.
| -rwxr-xr-x | examples/example.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/drawing/vml_shape.rb | 95 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/comment.rb | 33 | ||||
| -rw-r--r-- | test/drawing/tc_vml_shape.rb | 12 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_comment.rb | 6 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_comments.rb | 4 |
6 files changed, 46 insertions, 106 deletions
diff --git a/examples/example.rb b/examples/example.rb index c9b5564e..70f003fc 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -583,6 +583,8 @@ if examples.include? :comments wb.add_worksheet(:name => 'comments') do |sheet| sheet.add_row ['Can we build it?'] sheet.add_comment :ref => 'A1', :author => 'Bob', :text => 'Yes We Can!' + sheet.add_comment :ref => 'A2', :author => 'Bob', :text => 'Yes We Can! - but I dont think you need to know about it!', :visible => false + end end diff --git a/lib/axlsx/drawing/vml_shape.rb b/lib/axlsx/drawing/vml_shape.rb index 701456f3..8dc83c3b 100644 --- a/lib/axlsx/drawing/vml_shape.rb +++ b/lib/axlsx/drawing/vml_shape.rb @@ -4,95 +4,35 @@ module Axlsx class VmlShape include Axlsx::OptionsParser + include Axlsx::Accessors # Creates a new VmlShape - # @option options [Integer|String] left_column - # @option options [Integer|String] left_offset - # @option options [Integer|String] top_row - # @option options [Integer|String] top_offset - # @option options [Integer|String] right_column - # @option options [Integer|String] right_offset - # @option options [Integer|String] bottom_row - # @option options [Integer|String] bottom_offset + # @option options [Integer] row + # @option options [Integer] column + # @option options [Integer] left_column + # @option options [Integer] left_offset + # @option options [Integer] top_row + # @option options [Integer] top_offset + # @option options [Integer] right_column + # @option options [Integer] right_offset + # @option options [Integer] bottom_row + # @option options [Integer] bottom_offset def initialize(options={}) @row = @column = @left_column = @top_row = @right_column = @bottom_row = 0 @left_offset = 15 @top_offset = 2 @right_offset = 50 @bottom_offset = 5 + @visible = true @id = (0...8).map{65.+(rand(25)).chr}.join parse_options options yield self if block_given? end - # The row anchor position for this shape determined by the comment's ref value - # @return [Integer] - attr_reader :row + unsigned_int_attr_accessor :row, :column, :left_column, :left_offset, :top_row, :top_offset, + :right_column, :right_offset, :bottom_row, :bottom_offset - # The column anchor position for this shape determined by the comment's ref value - # @return [Integer] - attr_reader :column - - # The left column for this shape - # @return [Integer] - attr_reader :left_column - - # The left offset for this shape - # @return [Integer] - attr_reader :left_offset - - # The top row for this shape - # @return [Integer] - attr_reader :top_row - - # The top offset for this shape - # @return [Integer] - attr_reader :top_offset - - # The right column for this shape - # @return [Integer] - attr_reader :right_column - - # The right offset for this shape - # @return [Integer] - attr_reader :right_offset - - # The botttom row for this shape - # @return [Integer] - attr_reader :bottom_row - - # The bottom offset for this shape - # @return [Integer] - attr_reader :bottom_offset - - # @see column - def column=(v); Axlsx::validate_integerish(v); @column = v.to_i end - - # @see row - def row=(v); Axlsx::validate_integerish(v); @row = v.to_i end - # @see left_column - def left_column=(v); Axlsx::validate_integerish(v); @left_column = v.to_i end - - # @see left_offset - def left_offset=(v); Axlsx::validate_integerish(v); @left_offset = v.to_i end - - # @see top_row - def top_row=(v); Axlsx::validate_integerish(v); @top_row = v.to_i end - - # @see top_offset - def top_offset=(v); Axlsx::validate_integerish(v); @top_offset = v.to_i end - - # @see right_column - def right_column=(v); Axlsx::validate_integerish(v); @right_column = v.to_i end - - # @see right_offset - def right_offset=(v); Axlsx::validate_integerish(v); @right_offset = v.to_i end - - # @see bottom_row - def bottom_row=(v); Axlsx::validate_integerish(v); @bottom_row = v.to_i end - - # @see bottom_offset - def bottom_offset=(v); Axlsx::validate_integerish(v); @bottom_offset = v.to_i end + boolean_attr_accessor :visible # serialize the shape to a string # @param [String] str @@ -100,7 +40,8 @@ module Axlsx def to_xml_string(str ='') str << <<SHAME_ON_YOU -<v:shape id="#{@id}" type="#_x0000_t202" fillcolor="#ffffa1 [80]" o:insetmode="auto"> +<v:shape id="#{@id}" type="#_x0000_t202" fillcolor="#ffffa1 [80]" o:insetmode="auto" + style="visiblity:#{@visible ? 'visible' : 'hidden'}"> <v:fill color2="#ffffa1 [80]"/> <v:shadow on="t" obscured="t"/> <v:path o:connecttype="none"/> @@ -115,7 +56,7 @@ str << <<SHAME_ON_YOU <x:AutoFill>False</x:AutoFill> <x:Row>#{row}</x:Row> <x:Column>#{column}</x:Column> - <x:Visible/> + #{@visible ? '<x:Visible/>' : ''} </x:ClientData> </v:shape> SHAME_ON_YOU diff --git a/lib/axlsx/workbook/worksheet/comment.rb b/lib/axlsx/workbook/worksheet/comment.rb index 3e54d2b3..0c885fda 100644 --- a/lib/axlsx/workbook/worksheet/comment.rb +++ b/lib/axlsx/workbook/worksheet/comment.rb @@ -4,35 +4,33 @@ module Axlsx class Comment include Axlsx::OptionsParser + include Axlsx::Accessors # Creates a new comment object - # @param [Comments] comments + # @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 - # The text to render - # @return [String] - attr_reader :text - - # The author of this comment - # @see Comments - # @return [String] - attr_reader :author + string_attr_accessor :text, :author + boolean_attr_accessor :visible - # The owning Comments object + # 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] + # @return [String|Cell] attr_reader :ref # TODO @@ -60,17 +58,6 @@ module Axlsx @ref = v.r if v.is_a?(Cell) end - # @see text - def text=(v) - Axlsx::validate_string(v) - @text = v - end - - # @see author - def author=(v) - @author = v - end - # serialize the object # @param [String] str # @return [String] @@ -93,7 +80,7 @@ module Axlsx # 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]) do |vml| + @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 diff --git a/test/drawing/tc_vml_shape.rb b/test/drawing/tc_vml_shape.rb index 719beca7..94ad6e9f 100644 --- a/test/drawing/tc_vml_shape.rb +++ b/test/drawing/tc_vml_shape.rb @@ -6,8 +6,8 @@ class TestVmlShape < Test::Unit::TestCase p = Axlsx::Package.new wb = p.workbook @ws = wb.add_worksheet - @ws.add_comment :ref => 'A1', :text => 'penut machine', :author => 'crank' - @ws.add_comment :ref => 'C3', :text => 'rust bucket', :author => 'PO' + @ws.add_comment :ref => 'A1', :text => 'penut machine', :author => 'crank', :visible => true + @ws.add_comment :ref => 'C3', :text => 'rust bucket', :author => 'PO', :visible => false @comments = @ws.comments end @@ -84,11 +84,17 @@ class TestVmlShape < Test::Unit::TestCase assert(shape.top_row == 3) assert_raise(ArgumentError) { shape.top_row = [] } end - + def test_visible + shape = @comments.first.vml_shape + shape.visible = false + assert(shape.visible == false) + assert_raise(ArgumentError) { shape.visible = 'foo' } + end def test_to_xml_string str = @comments.vml_drawing.to_xml_string() doc = Nokogiri::XML(str) assert_equal(doc.xpath("//v:shape").size, 2) + assert_equal(1, doc.xpath("//x:Visible").size, 'ClientData/x:Visible element rendering') @comments.each do |comment| shape = comment.vml_shape assert(doc.xpath("//v:shape/x:ClientData/x:Row[text()='#{shape.row}']").size == 1) diff --git a/test/workbook/worksheet/tc_comment.rb b/test/workbook/worksheet/tc_comment.rb index 0cb79bcc..eacce376 100644 --- a/test/workbook/worksheet/tc_comment.rb +++ b/test/workbook/worksheet/tc_comment.rb @@ -5,7 +5,7 @@ class TestComment < Test::Unit::TestCase p = Axlsx::Package.new wb = p.workbook @ws = wb.add_worksheet - @c1 = @ws.add_comment :ref => 'A1', :text => 'penut machine', :author => 'crank' + @c1 = @ws.add_comment :ref => 'A1', :text => 'penut machine', :author => 'crank', :visible => false @c2 = @ws.add_comment :ref => 'C3', :text => 'rust bucket', :author => 'PO' end @@ -28,6 +28,10 @@ class TestComment < Test::Unit::TestCase assert_equal(@c2.author_index, 0) end + def test_visible + assert_equal(false, @c1.visible) + assert_equal(true, @c2.visible) + end def test_ref assert(@c1.ref == 'A1') assert(@c2.ref == 'C3') diff --git a/test/workbook/worksheet/tc_comments.rb b/test/workbook/worksheet/tc_comments.rb index 665f3598..acadf73d 100644 --- a/test/workbook/worksheet/tc_comments.rb +++ b/test/workbook/worksheet/tc_comments.rb @@ -25,9 +25,9 @@ class TestComments < Test::Unit::TestCase end def test_authors assert_equal(@ws.comments.authors.size, @ws.comments.size) - @ws.add_comment(:text => 'Yes We Can!', :author => :bob, :ref => 'F1') + @ws.add_comment(:text => 'Yes We Can!', :author => 'bob', :ref => 'F1') assert_equal(@ws.comments.authors.size, 3) - @ws.add_comment(:text => 'Yes We Can!', :author => :bob, :ref => 'F1') + @ws.add_comment(:text => 'Yes We Can!', :author => 'bob', :ref => 'F1') assert_equal(@ws.comments.authors.size, 3, 'only unique authors are returned') end def test_pn |
