Class: Rspreadsheet::Workbook
- Inherits:
-
Object
- Object
- Rspreadsheet::Workbook
- Defined in:
- lib/rspreadsheet/workbook.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#worksheets ⇒ Object
readonly
Returns the value of attribute worksheets.
Instance Method Summary collapse
- #create_worksheet(node = nil) ⇒ Object
-
#initialize(afilename = nil) ⇒ Workbook
constructor
A new instance of Workbook.
- #save(new_filename = nil) ⇒ Object
- #worksheet_names ⇒ Object
- #worksheets_count ⇒ Object
Constructor Details
#initialize(afilename = nil) ⇒ Workbook
Returns a new instance of Workbook.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rspreadsheet/workbook.rb', line 7 def initialize(afilename=nil) @worksheets={} @filename = afilename if filename.nil? else @content_xml = Zip::File.open(filename) do |zip| LibXML::XML::Document.io zip.get_input_stream('content.xml') end ndx = 0 @content_xml.find_first('//office:spreadsheet').each_element { |node| sheet = Worksheet.new(node) @worksheets[ndx]=sheet @worksheets[node.name]=sheet ndx+=1 } end end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
6 7 8 |
# File 'lib/rspreadsheet/workbook.rb', line 6 def filename @filename end |
#worksheets ⇒ Object (readonly)
Returns the value of attribute worksheets.
6 7 8 |
# File 'lib/rspreadsheet/workbook.rb', line 6 def worksheets @worksheets end |
Instance Method Details
#create_worksheet(node = nil) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/rspreadsheet/workbook.rb', line 39 def create_worksheet(node=nil) sheet = Worksheet.new(node) @worksheets[worksheets_count]=sheet @worksheets[node.name]=sheet unless node.nil? return sheet end |
#save(new_filename = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rspreadsheet/workbook.rb', line 25 def save(new_filename=nil) if @filename.nil? and new_filename.nil? then raise 'New file should be named on first save.' end # if the filename has changed than first copy the original file to new location (or template if it is a new file) if new_filename FileUtils.cp(@filename || './lib/rspreadsheet/empty_file_template.ods', new_filename) @filename = new_filename end Zip::File.open(@filename) do |zip| # it is easy, because @content_xml in in sync with contents all the time zip.get_output_stream('content.xml') do |f| f.write @content_xml end end end |
#worksheet_names ⇒ Object
51 52 53 |
# File 'lib/rspreadsheet/workbook.rb', line 51 def worksheet_names @worksheets.keys.reject{ |k| k.kind_of? Numeric } end |
#worksheets_count ⇒ Object
48 49 50 |
# File 'lib/rspreadsheet/workbook.rb', line 48 def worksheets_count @worksheets.keys.select{ |k| k.kind_of? Numeric }.size #TODO: ?? max end |