Class: BerkeleyLibrary::Util::ODS::Spreadsheet

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/berkeley_library/util/ods/spreadsheet.rb

Instance Method Summary collapse

Instance Method Details

#add_table(name, protected: true) ⇒ BerkeleyLibrary::Util::ODS::XML::Table::Table

Adds a table ('worksheet') to the spreadsheet.

Parameters:

  • name (String)

    the table name

  • protected (Boolean) (defaults to: true)

    whether to protect the table

Returns:



22
23
24
# File 'lib/berkeley_library/util/ods/spreadsheet.rb', line 22

def add_table(name, protected: true)
  content.document_content.add_table(name, protected: protected)
end

#auto_stylesBerkeleyLibrary::Util::ODS::XML::Office::AutomaticStyles

Gets the document styles



53
54
55
# File 'lib/berkeley_library/util/ods/spreadsheet.rb', line 53

def auto_styles
  content.document_content.automatic_styles
end

#contentXML::ContentDoc

Returns the content document

Returns:



31
32
33
# File 'lib/berkeley_library/util/ods/spreadsheet.rb', line 31

def content
  @content ||= XML::ContentDoc.new
end

#manifestXML::ManifestDoc

Returns the container manifest

Returns:



43
44
45
46
47
48
# File 'lib/berkeley_library/util/ods/spreadsheet.rb', line 43

def manifest
  @manifest ||= XML::ManifestDoc.new.tap do |mf_doc|
    manifest = mf_doc.manifest
    manifest_docs.each { |doc| manifest.add_entry_for(doc) }
  end
end

#stylesXML::StylesDoc

Returns the container styles

Returns:



37
38
39
# File 'lib/berkeley_library/util/ods/spreadsheet.rb', line 37

def styles
  @styles ||= XML::StylesDoc.new
end

#write_exploded_to(dir) ⇒ Array<String>

Writes this spreadsheet as an exploded set of pretty-printed XML files. NOTE: OpenOffice itself and many other tools get confused by the extra text nodes in the pretty-printed files and won't read them properly; this method is mostly for debugging.

Returns:

  • (Array<String>)

    a list of files written.

Raises:

  • (ArgumentError)


112
113
114
115
116
117
118
119
120
121
122
# File 'lib/berkeley_library/util/ods/spreadsheet.rb', line 112

def write_exploded_to(dir)
  raise ArgumentError, "Not a directory: #{dir.inspect}" unless File.directory?(dir)

  [].tap do |files_written|
    each_document do |doc|
      output_path = write_exploded(doc, dir)
      files_written << File.absolute_path(output_path)
      logger.debug("Wrote #{files_written.last}")
    end
  end
end

#write_toString #write_to(out) ⇒ Object #write_to(path) ⇒ Object

noinspection RubyYardReturnMatch

Overloads:

  • #write_toString

    Writes to a new string.

    Returns:

    • (String)

      a binary string containing the spreadsheet data.

  • #write_to(out) ⇒ Object

    Writes to the specified output stream. @return[void]

    Parameters:

    • out (IO)

      the output stream

  • #write_to(path) ⇒ Object

    Writes to the specified file. If path denotes a directory, the spreadsheet will be written as exploded, pretty-printed XML. @return[void]

    Parameters:

    • path (String, Pathname)

      the path to the output file

    See Also:



74
75
76
77
78
79
80
# File 'lib/berkeley_library/util/ods/spreadsheet.rb', line 74

def write_to(out = nil)
  return write_to_string unless out
  return write_to_stream(out) if io_like?(out)
  return write_exploded_to(out) if File.directory?(out)

  write_to_file(out)
end

#write_to_file(path) ⇒ Object

Writes to the specified file.

Parameters:

  • path (String, Pathname)


102
103
104
# File 'lib/berkeley_library/util/ods/spreadsheet.rb', line 102

def write_to_file(path)
  File.open(path, 'wb') { |f| write_to_stream(f) }
end

#write_to_stream(out) ⇒ Object

Writes to the specified output stream.

Parameters:

  • out (IO)


90
91
92
93
94
95
96
97
98
# File 'lib/berkeley_library/util/ods/spreadsheet.rb', line 90

def write_to_stream(out)
  zip64_orig = Zip.write_zip64_support
  begin
    Zip.write_zip64_support = true
    write_zipfile(out)
  ensure
    Zip.write_zip64_support = zip64_orig
  end
end

#write_to_stringObject

Writes to a new string.



83
84
85
86
# File 'lib/berkeley_library/util/ods/spreadsheet.rb', line 83

def write_to_string
  # noinspection RubyYardParamTypeMatch
  StringIO.new.tap { |out| write_to_stream(out) }.string
end