Class: Workbook::Book
- Inherits:
-
Array
- Object
- Array
- Workbook::Book
- Includes:
- Modules::BookDiffSort, Readers::CsvReader, Readers::OdsReader, Readers::TxtReader, Readers::XlsReader, Readers::XlsShared, Readers::XlsxReader, Writers::HtmlWriter, Writers::XlsWriter, Writers::XlsxWriter
- Defined in:
- lib/workbook/book.rb
Constant Summary
Constants included from Readers::XlsShared
Readers::XlsShared::XLS_COLORS
Class Method Summary collapse
-
.open(filename, extension = nil) ⇒ Workbook::Book
Create an instance from a file, using open.
-
.read(stringio_or_string, filetype, options = {}) ⇒ Workbook::Book
Create an instance from the given stream or string, which should be in CSV or TXT format.
Instance Method Summary collapse
-
#<<(sheet = Workbook::Sheet.new) ⇒ Object
<< (like in array) a sheet to the workbook (parameter is optional, default is a new sheet).
-
#create_or_open_sheet_at(index) ⇒ Object
Create or open the existing sheet at an index value.
-
#file_extension(filename) ⇒ String
The file extension.
-
#has_contents? ⇒ Boolean
If the first sheet has any contents.
-
#import(filename, extension = nil, options = {}) ⇒ Workbook::Book
Loads an external file into an existing worbook.
- #initialize(sheet = nil) ⇒ Workbook::Book constructor
-
#open_binary(filename, extension = nil, options = {}) ⇒ Workbook::Book
Open the file in binary, read-only mode, do not read it, but pas it throug to the extension determined loaded.
-
#open_text(filename, extension = nil, options = {}) ⇒ Object
Open the file in non-binary, read-only mode, read it and parse it to UTF-8.
-
#push(sheet = Workbook::Sheet.new) ⇒ Object
Push (like in array) a sheet to the workbook (parameter is optional, default is a new sheet).
-
#read(stringio_or_string, filetype, options = {}) ⇒ Object
Load the CSV data contained in the given StringIO or String object.
-
#sheet ⇒ Workbook::Sheet
Sheet returns the first sheet of a workbook, or an empty one.
-
#template ⇒ Workbook::Template
Returns the template describing how the document should be/is formatted.
- #template=(template) ⇒ Object
-
#text_to_utf8(text) ⇒ Object
Helper method to convert text in a file to UTF-8.
-
#title ⇒ String
The title of the workbook.
- #title=(t) ⇒ Object
-
#write(filename, options = {}) ⇒ Object
Writes the book to a file.
Methods included from Modules::BookDiffSort
Methods included from Readers::TxtReader
Methods included from Readers::CsvReader
#csv_lib, #load_csv, #parse_csv
Methods included from Readers::XlsxReader
#extract_xlsx_backgrounds, #extract_xlsx_number_formats, #load_xlsm, #load_xlsx, #pad_xlsx_row, #parse_shared_string_file, #parse_xlsx, #parse_xlsx_cell, #parse_xlsx_column, #parse_xlsx_fonts, #parse_xlsx_row, #parse_xlsx_sheet, #parse_xlsx_styles
Methods included from Readers::XlsShared
#html_color_to_xls_color, #ms_formatting_to_strftime, #num_fmt_id_to_ms_formatting, #strftime_to_ms_format, #xls_number_to_date, #xls_number_to_time
Methods included from Readers::OdsReader
#get_column_count, #get_repeat, #load_ods, #parse_local_cell, #parse_local_row, #parse_local_table, #parse_local_value, #parse_ods, #parse_ods_style, #set_cell_attributes, #set_format_property
Methods included from Readers::XlsReader
#load_xls, #parse_xls, #parse_xls_cell, #parse_xls_format, #parse_xls_row
Methods included from Writers::HtmlWriter
Methods included from Writers::XlsxWriter
#format_to_xlsx_format, #formats_to_xlsx_format, #init_xlsx_spreadsheet_template, #make_sure_f_is_a_workbook_format, #stream_xlsx, #to_xlsx, #write_to_xlsx, #xlsx_sheet, #xlsx_template
Methods included from Writers::XlsWriter
#format_to_xls_format, #init_spreadsheet_template, #parse_font_family, #to_xls, #write_to_xls, #xls_sheet, #xls_template
Constructor Details
Class Method Details
.open(filename, extension = nil) ⇒ Workbook::Book
Create an instance from a file, using open.
211 212 213 214 215 |
# File 'lib/workbook/book.rb', line 211 def open filename, extension=nil wb = self.new wb.import filename, extension return wb end |
.read(stringio_or_string, filetype, options = {}) ⇒ Workbook::Book
Create an instance from the given stream or string, which should be in CSV or TXT format
222 223 224 225 226 |
# File 'lib/workbook/book.rb', line 222 def read stringio_or_string, filetype, ={} wb = self.new wb.read(stringio_or_string, filetype, ) wb end |
Instance Method Details
#<<(sheet = Workbook::Sheet.new) ⇒ Object
<< (like in array) a sheet to the workbook (parameter is optional, default is a new sheet)
95 96 97 98 99 |
# File 'lib/workbook/book.rb', line 95 def << sheet=Workbook::Sheet.new sheet = Workbook::Sheet.new(sheet) unless sheet.is_a? Workbook::Sheet super(sheet) sheet.book=(self) end |
#create_or_open_sheet_at(index) ⇒ Object
Create or open the existing sheet at an index value
198 199 200 201 202 203 |
# File 'lib/workbook/book.rb', line 198 def create_or_open_sheet_at index s = self[index] s = self[index] = Workbook::Sheet.new if s == nil s.book = self s end |
#file_extension(filename) ⇒ String
Returns The file extension.
178 179 180 181 182 |
# File 'lib/workbook/book.rb', line 178 def file_extension(filename) ext = File.extname(filename).gsub('.','').downcase if filename # for remote files which has asset id after extension ext.split('?')[0] end |
#has_contents? ⇒ Boolean
If the first sheet has any contents
113 114 115 |
# File 'lib/workbook/book.rb', line 113 def has_contents? sheet.has_contents? end |
#import(filename, extension = nil, options = {}) ⇒ Workbook::Book
Loads an external file into an existing worbook
122 123 124 125 126 127 128 129 |
# File 'lib/workbook/book.rb', line 122 def import filename, extension=nil, ={} extension = file_extension(filename) unless extension if ['txt','csv','xml'].include?(extension) open_text filename, extension, else open_binary filename, extension, end end |
#open_binary(filename, extension = nil, options = {}) ⇒ Workbook::Book
Open the file in binary, read-only mode, do not read it, but pas it throug to the extension determined loaded
136 137 138 139 140 |
# File 'lib/workbook/book.rb', line 136 def open_binary filename, extension=nil, ={} extension = file_extension(filename) unless extension f = open(filename) send("load_#{extension}".to_sym, f, ) end |
#open_text(filename, extension = nil, options = {}) ⇒ Object
Open the file in non-binary, read-only mode, read it and parse it to UTF-8
146 147 148 149 150 |
# File 'lib/workbook/book.rb', line 146 def open_text filename, extension=nil, ={} extension = file_extension(filename) unless extension t = text_to_utf8(open(filename).read) send("load_#{extension}".to_sym, t, ) end |
#push(sheet = Workbook::Sheet.new) ⇒ Object
Push (like in array) a sheet to the workbook (parameter is optional, default is a new sheet)
87 88 89 90 |
# File 'lib/workbook/book.rb', line 87 def push sheet=Workbook::Sheet.new super(sheet) sheet.book=(self) end |
#read(stringio_or_string, filetype, options = {}) ⇒ Object
Load the CSV data contained in the given StringIO or String object
188 189 190 191 192 193 |
# File 'lib/workbook/book.rb', line 188 def read(stringio_or_string, filetype, ={}) raise ArgumentError.new("The filetype parameter should be either :csv or :txt") unless [:csv, :txt].include?(filetype) t = stringio_or_string.respond_to?(:read) ? stringio_or_string.read : stringio_or_string.to_s t = text_to_utf8(t) send(:"parse_#{filetype}", t, ) end |
#sheet ⇒ Workbook::Sheet
Sheet returns the first sheet of a workbook, or an empty one.
105 106 107 108 |
# File 'lib/workbook/book.rb', line 105 def sheet push Workbook::Sheet.new unless first first end |
#template ⇒ Workbook::Template
Returns the template describing how the document should be/is formatted
63 64 65 |
# File 'lib/workbook/book.rb', line 63 def template @template ||= Workbook::Template.new end |
#template=(template) ⇒ Object
68 69 70 71 |
# File 'lib/workbook/book.rb', line 68 def template= template raise ArgumentError, "format should be a Workboot::Format" unless template.is_a? Workbook::Template @template = template end |
#text_to_utf8(text) ⇒ Object
Helper method to convert text in a file to UTF-8
165 166 167 168 169 170 171 172 173 |
# File 'lib/workbook/book.rb', line 165 def text_to_utf8 text unless text.valid_encoding? and text.encoding == "UTF-8" # TODO: had some ruby 1.9 problems with rchardet ... but ideally it or a similar functionality will be reintroduced source_encoding = text.valid_encoding? ? text.encoding : "US-ASCII" text = text.encode('UTF-8', source_encoding, {:invalid=>:replace, :undef=>:replace, :replace=>""}) text = text.gsub("\u0000","") # TODO: this cleanup of nil values isn't supposed to be needed... end text end |
#title ⇒ String
The title of the workbook
76 77 78 |
# File 'lib/workbook/book.rb', line 76 def title (defined?(@title) and !@title.nil?) ? @title : "untitled document" end |
#title=(t) ⇒ Object
80 81 82 |
# File 'lib/workbook/book.rb', line 80 def title= t @title = t end |
#write(filename, options = {}) ⇒ Object
Writes the book to a file. Filetype is based on the extension, but can be overridden
156 157 158 159 |
# File 'lib/workbook/book.rb', line 156 def write filename, ={} extension = file_extension(filename) send("write_to_#{extension}".to_sym, filename, ) end |