Class: Workbook::Book

Inherits:
Array
  • Object
show all
Includes:
Readers::CsvReader, Readers::TxtReader, Readers::XlsReader, Writers::XlsWriter
Defined in:
lib/workbook/book.rb

Constant Summary

Constant Summary

Constants included from Readers::XlsReader

Readers::XlsReader::XLS_COLORS

Constants included from Writers::XlsWriter

Writers::XlsWriter::XLS_COLORS

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Readers::TxtReader

#load_txt, #parse_txt

Methods included from Readers::CsvReader

#load_csv, #parse_csv

Methods included from Readers::XlsReader

#load_xls, #parse_xls

Methods included from Writers::XlsWriter

#format_to_xls_format, #html_color_to_xls_color, #init_spreadsheet_template, #strftime_to_ms_format, #to_xls, #write_to_xls, #xls_sheet, #xls_template

Constructor Details

- (Workbook::Book) initialize(sheet = Workbook::Sheet.new([], self, options={}))

Parameters:

  • Create (Workbook::Sheet, Array)

    a new workbook based on an existing sheet, or initialize a sheet based on the array



20
21
22
23
24
25
26
# File 'lib/workbook/book.rb', line 20

def initialize sheet=Workbook::Sheet.new([], self, options={})
  if sheet.is_a? Workbook::Sheet
    push sheet
  else
    push Workbook::Sheet.new(sheet, self, options)
  end
end

Instance Attribute Details

- (Object) default_rewrite_header

Returns the value of attribute default_rewrite_header



16
17
18
# File 'lib/workbook/book.rb', line 16

def default_rewrite_header
  @default_rewrite_header
end

- (Workbook::Format) template

Returns the template describing how the document should be/is formatted

Returns:

  • (Workbook::Format)

    returns the template describing how the document should be/is formatted



29
30
31
# File 'lib/workbook/book.rb', line 29

def template
  @template ||= Workbook::Template.new
end

- (String) title

Returns the title of the workbook

Returns:

  • (String)

    the title of the workbook



40
41
42
# File 'lib/workbook/book.rb', line 40

def title
  @title ? @title : "untitled document"
end

Class Method Details

+ (Workbook::Book) open(filename, ext = nil)

Create an instance from a file, using open.

Returns:



99
100
101
102
103
# File 'lib/workbook/book.rb', line 99

def self.open filename, ext=nil
  wb = self.new
  wb.open filename, ext
  return wb
end

Instance Method Details

- (Object) create_or_open_sheet_at(index)



105
106
107
108
109
110
# File 'lib/workbook/book.rb', line 105

def create_or_open_sheet_at index
  s = self[index]
  s = self[index] = Workbook::Sheet.new if s == nil
  s.book = self
  s 
end

- (Boolean) default_rewrite_header?

Returns:

  • (Boolean)


116
117
118
119
# File 'lib/workbook/book.rb', line 116

def default_rewrite_header?
  return true if default_rewrite_header.nil?
  default_rewrite_header
end

- (String) file_extension(filename)

Returns The file extension

Parameters:

  • The (String)

    full filename, or path

Returns:

  • (String)

    The file extension



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

def file_extension(filename)
  File.extname(filename).gsub('.','')
end

- (Boolean) has_contents?

Returns:

  • (Boolean)


54
55
56
# File 'lib/workbook/book.rb', line 54

def has_contents?
  sheet.has_contents?
end

- (Object) open(filename, ext = nil)

Loads an external file into an existing worbook

Parameters:

  • a (String)

    string with a reference to the file to be opened

  • an (String)

    optional string enforcing a certain parser (based on the file extension, e.g. 'txt', 'csv' or 'xls')



61
62
63
64
65
66
67
68
# File 'lib/workbook/book.rb', line 61

def open filename, ext=nil
  ext = file_extension(filename) unless ext
  if ['txt','csv','xml'].include?(ext)
    open_text filename, ext
  else
    open_binary filename, ext
  end
end

- (Object) open_binary(filename, ext = nil)

Open the file in binary, read-only mode, do not read it, but pas it throug to the extension determined loaded

Parameters:

  • a (String)

    string with a reference to the file to be opened

  • an (String)

    optional string enforcing a certain parser (based on the file extension, e.g. 'txt', 'csv' or 'xls')



73
74
75
76
77
# File 'lib/workbook/book.rb', line 73

def open_binary filename, ext=nil
  ext = file_extension(filename) unless ext
  f = File.open(filename,'rb')
  send("load_#{ext}".to_sym,f)
end

- (Object) open_text(filename, ext = nil)

Open the file in non-binary, read-only mode, read it and parse it to UTF-8

Parameters:

  • a (String)

    string with a reference to the file to be opened

  • an (String)

    optional string enforcing a certain parser (based on the file extension, e.g. 'txt', 'csv' or 'xls')



82
83
84
85
86
87
88
89
# File 'lib/workbook/book.rb', line 82

def open_text filename, ext=nil
  ext = file_extension(filename) unless ext
  f = File.open(filename,'r')
  t = f.read
  detected_encoding = CharDet.detect(t)['encoding']
  t = Iconv.conv("UTF-8//TRANSLIT//IGNORE",detected_encoding,t)
  send("load_#{ext}".to_sym,t)
end

- (Object) push(sheet = Workbook::Sheet.new)



44
45
46
# File 'lib/workbook/book.rb', line 44

def push sheet=Workbook::Sheet.new
  super(sheet)
end

- (Workbook::Sheet) sheet

Returns The first sheet, and creates an empty one if one doesn't exists

Returns:

  • (Workbook::Sheet)

    The first sheet, and creates an empty one if one doesn't exists



49
50
51
52
# File 'lib/workbook/book.rb', line 49

def sheet
  push Workbook::Sheet.new unless first
  first
end

- (Object) sort



112
113
114
# File 'lib/workbook/book.rb', line 112

def sort
  raise Exception("Books can't be sorted")
end