Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make size ignore formatted cells without content #52

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
72 changes: 47 additions & 25 deletions lib/rspreadsheet/worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
require 'helpers/class_extensions'
# require 'forwardable'

module Rspreadsheet
module Rspreadsheet

class Worksheet
include XMLTiedArray_WithRepeatableItems
attr_accessor :xmlnode
# @!group XMLTiedArray related methods
def subnode_options; {
:node_name => 'table-row',
:ignore_groupings => ['table-header-rows'],
:node_name => 'table-row',
:ignore_groupings => ['table-header-rows'],
:repeated_attribute => 'number-rows-repeated'
} end

Expand All @@ -29,80 +29,99 @@ def initialize(xmlnode_or_sheet_name,workbook) # workbook is here ONLY because o
else raise 'Provide name or xml node to create a Worksheet object'
end
end

# name of the worksheet
# @returns [String] the name of the worksheet
def name; Tools.get_ns_attribute_value(@xmlnode,'table','name') end
def name=(value); Tools.set_ns_attribute(@xmlnode,'table','name', value) end

def name
Tools.get_ns_attribute_value(@xmlnode,'table','name')
end

def name=(value)
Tools.set_ns_attribute(@xmlnode,'table','name', value)
end

def first_unused_row_index
first_unused_subitem_index
end

def add_row_above(arowi)
insert_new_empty_subitem_before(arowi)
end

def insert_cell_before(arowi,acoli) # TODO: maybe move this to row level
detach_row_in_xml(arowi)
rows(arowi).insert_new_item(acoli)
rows(arowi).insert_new_item(acoli)
end

def detach_row_in_xml(rowi)
return detach_my_subnode_respect_repeated(rowi)
end

def nonemptycells
used_rows_range.collect{ |rowi| rows(rowi).nonemptycells }.flatten
end

#@!group images
def worksheet_images
@worksheet_images ||= WorksheetImages.new(self)
end

def images_count
worksheet_images.size
end

def images(*params)
worksheet_images.subitems(*params)
end
worksheet_images.subitems(*params)
end

def insert_image(filename,mime='image/png')
worksheet_images.insert_image(filename,mime)
end

def insert_image_to(x,y,filename,mime='image/png')
img = insert_image(filename,mime)
img.move_to(x,y)
img
end

#@!group XMLTiedArray_WithRepeatableItems connected methods
def rows(*params); subitems(*params) end
alias :row :rows
def prepare_subitem(rowi); Row.new(self,rowi) end
def rowcache; @itemcache end

def rows(*params)
subitems(*params)
end
alias :row :rows # note that this is confusing terminology, deprecate?

def prepare_subitem(rowi)
Row.new(self,rowi)
end

def rowcache
@itemcache
end

#@!group How to get to cells? (syntactic sugar)
# Returns value of the cell given either by row,column integer coordinates of by address.
# @param [(Integer,Integer), String] either row and column of the cell (i.e. 3,5) or a string containing it address i.e. 'F12'
def [](*params)
cells(*params).andand.value
end
# Sets value of the cell given either by row,column integer coordinates of by address.

# Sets value of the cell given either by row,column integer coordinates of by address.
# It also sets the type of the cell according to type of the value. For details #see Cell.value=
# This also allows syntax like
#
# @sheet[1] = ['Jan', 'Feb', 'Mar']
def []=(*params)
if (params.size == 2) and params[0].kind_of?(Integer)
if (params.size == 2) and params[0].kind_of?(Integer)
rows(params[0]).cellvalues = params[1]
else
cells(*params[0..-2]).andand.value = params.last
cells(*params[0..-2]).andand.value = params.last
end
end

# Returns a Cell object placed in row and column or on a Cell on string address
# @param [(Integer,Integer), String] either row and column of the cell (i.e. 3,5) or a string containing it address i.e. 'F12'
def cells(*params)
case params.length
case params.length
when 0 then raise 'Not implemented yet' #TODO: return list of all cells
when 1..2
r,c = Rspreadsheet::Tools.a2c(*params)
Expand All @@ -111,11 +130,13 @@ def cells(*params)
else raise ArgumentError.new('Wrong number of arguments.')
end
end

alias :cell :cells
def column(param)
_,coli = Rspreadsheet::Tools.a2c(1,param)
Column.new(self,coli)
end

# Allows syntax like sheet.F15. TO catch errors easier, allows only up to three uppercase letters in colum part, althought it won't be necessarry to restrict.
def method_missing method_name, *args, &block
if method_name.to_s.match(/^([A-Z]{1,3})(\d{1,8})(=?)$/)
Expand All @@ -130,6 +151,7 @@ def method_missing method_name, *args, &block
super
end
end

alias :rowcount :size
def used_rows_range
1..self.rowcount
Expand Down
Loading