Skip to content

Commit

Permalink
implementing clone_above_row and other support cloning procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
gorn committed Sep 18, 2019
1 parent 54edef7 commit 98386b1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
5 changes: 5 additions & 0 deletions lib/rspreadsheet/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def nonemptycellsindexes
def add_row_above
parent.add_row_above(rowi)
end
alias :insert_row_above :add_row_above

def next_row; relative(+1) end
alias :next :next_row
Expand All @@ -117,6 +118,10 @@ def relative(rowi_offset)
worksheet.row(self.rowi+rowi_offset)
end

def clone_above_row(target_rowi)
parent.clone_item_before(rowi, target_rowi)
end

# @!group Private methods, which should not be called directly
# @private
# shifts internal represetation of row by diff. This should not be called directly
Expand Down
3 changes: 3 additions & 0 deletions lib/rspreadsheet/tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ def self.get_ns_attribute(node,ns_prefix,key,default=:undefined_default)
node.nil? ? default : node.attributes.get_attribute_ns(Tools.get_namespace(ns_prefix).href,key) || default
end
end
def self.delete_ns_attribute(node,ns_prefix,key)
set_ns_attribute(node,ns_prefix,key,nil,nil)
end
def self.get_ns_attribute_value(node,ns_prefix,key,default=:undefined_default)
if default==:undefined_default
Tools.get_ns_attribute(node,ns_prefix,key).andand.value
Expand Down
2 changes: 1 addition & 1 deletion lib/rspreadsheet/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Rspreadsheet
VERSION = "0.5.1"
VERSION = "0.5.2"
end
4 changes: 2 additions & 2 deletions lib/rspreadsheet/xml_tied_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def how_many_times_node_is_repeated(node); 1 end

# # @!group accessing subnodes
# returns xmlnode with index
# DOES not respect repeated_attribute
# does NOT respect repeated_attribute
def my_subnode(aindex)
raise 'Using method which does not respect repeated_attribute with options that are using it. You probably donot want to do that.' unless subnode_options[:repeated_attribute].nil?
return xmlsubnodes[aindex-1]
Expand All @@ -157,7 +157,7 @@ def insert_new_empty_subnode_before(aindex)
else
raise IndexError.new("Index #{aindex} out of bounds (1..#{self.size})")
end
end
end

def prepare_empty_subnode
Tools.prepare_ns_node(
Expand Down
29 changes: 19 additions & 10 deletions lib/rspreadsheet/xml_tied_repeatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ def find_subnode_with_range(aindex)
end

# @!group inserting new subnodes

def insert_new_empty_subnode_before(aindex)
insert_new_empty_subnode_before_respect_repeatable(aindex)

def insert_new_empty_subnode_before_respect_repeatable(aindex)
new_subnode = prepare_repeated_subnode(1, subnode_options)
insert_subnode_before_respect_repeatable(new_subnode, aindex)
end
alias :insert_new_empty_subnode_before :insert_new_empty_subnode_before_respect_repeatable

def insert_new_empty_subnode_before_respect_repeatable(aindex)
def insert_subnode_before_respect_repeatable(subnode,aindex)
axmlnode = xmlnode
options = subnode_options
node,index_range = find_subnode_with_range(aindex)
Expand All @@ -58,15 +60,16 @@ def insert_new_empty_subnode_before_respect_repeatable(aindex)
[index_range.begin..aindex-1,aindex..index_range.end].reject {|range| range.size<1}.each do |range| # split original node by cloning
clone_before_and_set_repeated_attribute(node,range.size,options)
end
node.prev.prev = prepare_repeated_subnode(1, options) # insert new node
node.remove! # remove the original node
node.prev.prev = subnode # insert subnode
node.remove! # remove the original node
else # insert outbound xmlnode
[index+1..aindex-1,aindex..aindex].reject {|range| range.size<1}.each do |range|
axmlnode << prepare_repeated_subnode(range.size, options)
end
number_of_preceeding_items = aindex-1-(index+1)+1
axmlnode << prepare_repeated_subnode(number_of_preceeding_items, options) unless number_of_preceeding_items<1 # insert preceeding cells
axmlnode << subnode # insert subnode
end #TODO: Out of bounds indexes handling
return my_subnode(aindex)
end
alias :insert_subnode_before :insert_subnode_before_respect_repeatable

def prepare_repeated_subnode(times_repeated,options)
result = prepare_empty_subnode
Expand All @@ -80,6 +83,13 @@ def clone_before_and_set_repeated_attribute(node,times_repeated,options)
node.prev = newnode
end

# takes item on source_index, clones it and inserts it before target_index
def clone_item_before(source_index, target_index)
newnode = my_subnode(source_index).copy(true)
Tools.delete_ns_attribute(newnode,'table',subnode_options[:repeated_attribute])
insert_subnode_before_respect_repeatable(newnode,target_index)
end

# detaches subnode with aindex
def detach_my_subnode_respect_repeated(aindex)
axmlnode = xmlnode
Expand Down Expand Up @@ -109,7 +119,6 @@ def how_many_times_node_is_repeated(node) # adding respect to repeated nodes
(node.attributes[subnode_options[:repeated_attribute]] || 1).to_i
end


# clean up item from xml (handle possible detachments) and itemcache. leave the object invalidation on the object
# this should not be called from nowhere but XMLTiedItem.delete
def delete_subitem(aindex)
Expand Down
15 changes: 15 additions & 0 deletions spec/row_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@
@sheet.A1.should_not == 'This is first nonheader row, but it is in colheader'
@sheet.B3.should == 'First completely nonheader cell'
end
it 'can be cloned to other row' do
@sheet2.row(5)[1].should == 5
@sheet2.row(6)[1].should == 6

@sheet2.row(6)[1].should_not == 4
@sheet2.row(4).clone_above_row(6)
@sheet2.row(4)[1].should == 4
@sheet2.row(5)[1].should == 5
@sheet2.row(6)[1].should == 4
@sheet2.row(7)[1].should == 6
@sheet2.row(6)[1].should == @sheet2.row(4)[1]
@sheet2.row(6)[2].should == @sheet2.row(4)[2]
@sheet2.row(6)[3].should == @sheet2.row(4)[3]
@sheet2.row(6).cell(2).formula.should == @sheet2.row(4).cell(2).formula
end
end


0 comments on commit 98386b1

Please sign in to comment.