Class: Rspreadsheet::Workbook

Inherits:
Object
  • Object
show all
Defined in:
lib/rspreadsheet/workbook.rb

Direct Known Subclasses

WorkbookFlat

Instance Attribute Summary collapse

Worksheet methods collapse

Loading and saving related methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(afilename = nil) ⇒ Workbook

Returns a new instance of Workbook.


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rspreadsheet/workbook.rb', line 56

def initialize(afilename=nil)
  @worksheets=[]
  @filename = afilename
  @content_xml = Zip::File.open(@filename || TEMPLATE_FILE_NAME) do |zip|
    LibXML::XML::Document.io zip.get_input_stream(CONTENT_FILE_NAME)
  end
  @xmlnode = @content_xml.find_first('//office:spreadsheet')
  @xmlnode.find('./table:table').each do |node|
    create_worksheet_from_node(node)
  end
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename


7
8
9
# File 'lib/rspreadsheet/workbook.rb', line 7

def filename
  @filename
end

#xmlnodeObject (readonly)

debug


8
9
10
# File 'lib/rspreadsheet/workbook.rb', line 8

def xmlnode
  @xmlnode
end

Instance Method Details

#[](index_or_name) ⇒ Object


47
# File 'lib/rspreadsheet/workbook.rb', line 47

def [](index_or_name); self.worksheets(index_or_name) end

#create_worksheet(name = "Sheet#{worksheets_count+1}") ⇒ Object Also known as: add_worksheet


17
18
19
20
21
# File 'lib/rspreadsheet/workbook.rb', line 17

def create_worksheet(name = "Sheet#{worksheets_count+1}")
  sheet = Worksheet.new(name,self)
  register_worksheet(sheet)
  return sheet
end

#create_worksheet_from_node(source_node) ⇒ Object


12
13
14
15
16
# File 'lib/rspreadsheet/workbook.rb', line 12

def create_worksheet_from_node(source_node)
  sheet = Worksheet.new(source_node,self)
  register_worksheet(sheet)
  return sheet
end

#flat_format?Boolean

Returns:

  • (Boolean)

111
# File 'lib/rspreadsheet/workbook.rb', line 111

def flat_format?; false end

#mimeObject

Returns Mime of the file.

Returns:

  • Mime of the file


51
# File 'lib/rspreadsheet/workbook.rb', line 51

def mime; 'application/vnd.oasis.opendocument.spreadsheet'.freeze end

#mime_preferred_extensionString Also known as: mime_default_extension

Returns Prefered file extension.

Returns:

  • (String)

    Prefered file extension


53
# File 'lib/rspreadsheet/workbook.rb', line 53

def mime_preferred_extension; 'ods'.freeze end

#normal_format?Boolean

Returns:

  • (Boolean)

112
# File 'lib/rspreadsheet/workbook.rb', line 112

def normal_format?; true end

#save(io = nil) ⇒ Object Also known as: save_to_io, save_as

Saves the worksheet. Optionally you can provide new filename or IO stream to which the file should be saved.

Parameters:

  • Optional (String)

    new filename


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rspreadsheet/workbook.rb', line 70

def save(io=nil)
  case
    when @filename.nil? && io.nil?
      raise 'New file should be named on first save.'
    when @filename.kind_of?(String) && io.nil?
      Tools.output_to_zip_stream(@filename) do |input_and_output_zip|                  # open old file
        update_zip_manifest_and_content_xml(input_and_output_zip,input_and_output_zip) # input and output are identical
      end
    when (@filename.kind_of?(String) && (io.kind_of?(String) || io.kind_of?(File)))
      io = io.path if io.kind_of?(File)                                           # convert file to its filename
      FileUtils.cp(@filename , io)                                                # copy file externally
      @filename = io                                                              # remember new name
      save_to_io(nil)                                                             # continue modyfying file on spot
    when io.kind_of?(IO) || io.kind_of?(String) || io.kind_of?(StringIO)
      Tools.output_to_zip_stream(io) do |output_io|                               # open output stream of file
        write_ods_to_io(output_io)
      end
      io.rewind if io.kind_of?(StringIO)
    else raise 'Ivalid combinations of parameter types in save'
  end
end

#to_ioObject


93
94
95
# File 'lib/rspreadsheet/workbook.rb', line 93

def to_io
  WorkbookIO.new(self)
end

#worksheet_namesString

Returns names of sheets in the workbook.

Returns:

  • (String)

    names of sheets in the workbook


27
# File 'lib/rspreadsheet/workbook.rb', line 27

def worksheet_names; @worksheets.collect{ |ws| ws.name } end

#worksheets(index_or_name) ⇒ Worksheet Also known as: worksheet, sheet, sheets

Returns worksheet with given index or name.

Parameters:

  • (Integer, String)

Returns:

  • (Worksheet)

    worksheet with given index or name


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rspreadsheet/workbook.rb', line 30

def worksheets(index_or_name)
  case index_or_name
    when Integer then begin
      case index_or_name
        when 0 then nil
        when 1..Float::INFINITY then @worksheets[index_or_name-1]
        when -Float::INFINITY..-1 then @worksheets[index_or_name]    # zaporne indexy znamenaji pocitani zezadu
      end
    end
    when String then @worksheets.select{|ws| ws.name == index_or_name}.first
    when NilClass then nil
    else raise 'method worksheets requires Integer index of the sheet or its String name'
  end
end

#worksheets_countInteger Also known as: worksheet_count

Returns number of sheets in the workbook.

Returns:

  • (Integer)

    number of sheets in the workbook


24
# File 'lib/rspreadsheet/workbook.rb', line 24

def worksheets_count; @worksheets.length end

#write_ods_to_io(io) ⇒ Object


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rspreadsheet/workbook.rb', line 97

def write_ods_to_io(io)
  if @filename.nil?
    Zip::File.open(TEMPLATE_FILE_NAME) do |empty_template_zip|         # open empty_template file
      copy_internally_without_content(empty_template_zip,io)           # copy empty_template internals
      update_zip_manifest_and_content_xml(empty_template_zip,io)           # update xmls + pictures
    end
  else
    Zip::File.open(@filename) do | old_zip |                           # open old file
      copy_internally_without_content(old_zip,io)                      # copy the old internals
      update_zip_manifest_and_content_xml(old_zip,io)                      # update xmls + pictures
    end
  end
end

#xmldocObject


9
# File 'lib/rspreadsheet/workbook.rb', line 9

def xmldoc; @xmlnode.doc end