Object
The Worksheet class. Contains most of the Spreadsheet data in Rows.
Interesting Attributes
The Name of this Worksheet. | |
The default format used for all cells in this Workhseet that have no format set explicitly or in Row#default_format. | |
The Rows in this Worksheet. It is not recommended to Manipulate this Array directly. If you do, call updated_from with the smallest modified index. | |
The Column formatting in this Worksheet. Column instances may appear at more than one position in columns. If you modify a Column directly, your changes will be reflected in all those positions. | |
When a user chooses to print a Workbook, Excel will include all selected Worksheets. If no Worksheet is selected at Workbook#write, then the first Worksheet is selected by default. |
# File lib/spreadsheet/worksheet.rb, line 32 def initialize opts={} @default_format = nil @selected = opts[:selected] @dimensions = [0,0,0,0] @name = opts[:name] || 'Worksheet' @workbook = opts[:workbook] @rows = [] @columns = [] @links = {} @merged_cells = [] @protected = false @password_hash = 0 end
Get the enriched value of the Cell at row, column. See also Worksheet#cell, Row#[].
# File lib/spreadsheet/worksheet.rb, line 274 def [] row, column row(row)[column] end
Set the value of the Cell at row, column to value. See also Row#[]=.
# File lib/spreadsheet/worksheet.rb, line 280 def []= row, column, value row(row)[column] = value end
Add a Format to the Workbook. If you use Row#set_format, you should not need to use this Method.
# File lib/spreadsheet/worksheet.rb, line 56 def add_format fmt @workbook.add_format fmt if fmt end
Get the enriched value of the Cell at row, column. See also Worksheet#[], Row#[].
# File lib/spreadsheet/worksheet.rb, line 62 def cell row, column row(row)[column] end
Returns the Column at idx.
# File lib/spreadsheet/worksheet.rb, line 67 def column idx @columns[idx] || Column.new(idx, default_format, :worksheet => self) end
The number of columns in this Worksheet which contain data.
# File lib/spreadsheet/worksheet.rb, line 72 def column_count dimensions[3] - dimensions[2] end
# File lib/spreadsheet/worksheet.rb, line 75 def column_updated idx, column @columns[idx] = column end
Dimensions |
[ first used row, first unused row, first used column, first unused column ] ( First used means that all rows or columns before that are empty. First unused means that this and all following rows or columns are empty. ) |
# File lib/spreadsheet/worksheet.rb, line 121 def dimensions @dimensions || recalculate_dimensions end
If no argument is given, each iterates over all used Rows (from the first used Row until but omitting the first unused Row, see also dimensions).
If the argument skip is given, each iterates from that row until but omitting the first unused Row, effectively skipping the first skip Rows from the top of the Worksheet.
# File lib/spreadsheet/worksheet.rb, line 131 def each skip=dimensions[0] skip.upto(dimensions[1] - 1) do |idx| yield row(idx) end end
Sets the default Format of the column at idx.
idx may be an Integer, or an Enumerable that iterates over a number of Integers.
format is a Format, or nil if you want to remove the Formatting at idx
Returns an instance of Column if idx is an Integer, an Array of Columns otherwise.
# File lib/spreadsheet/worksheet.rb, line 149 def format_column idx, format=nil, opts={} opts[:worksheet] = self res = case idx when Integer column = nil if format column = Column.new(idx, format, opts) end @columns[idx] = column else idx.collect do |col| format_column col, format, opts end end shorten @columns res end
Formats all Date, DateTime and Time cells with format or the default formats:
'DD.MM.YYYY' for Date
'DD.MM.YYYY hh:mm:ss' for DateTime and Time
# File lib/spreadsheet/worksheet.rb, line 169 def format_dates! format=nil new_formats = {} fmt_str_time = client('DD.MM.YYYY hh:mm:ss', 'UTF-8') fmt_str_date = client('DD.MM.YYYY', 'UTF-8') each do |row| row.each_with_index do |value, idx| unless row.formats[idx] || row.format(idx).date_or_time? numfmt = case value when DateTime, Time format || fmt_str_time when Date format || fmt_str_date end case numfmt when Format row.set_format idx, numfmt when String existing_format = row.format(idx) new_formats[existing_format] ||= {} new_format = new_formats[existing_format][numfmt] if !new_format new_format = new_formats[existing_format][numfmt] = existing_format.dup new_format.number_format = numfmt end row.set_format idx, new_format end end end end end
Insert a Row at idx (0-based) containing cells
# File lib/spreadsheet/worksheet.rb, line 201 def insert_row idx, cells=[] res = @rows.insert idx, Row.new(self, idx, cells) updated_from idx res end
# File lib/spreadsheet/worksheet.rb, line 206 def inspect names = instance_variables names.delete '@rows' variables = names.collect do |name| "%s=%s" % [name, instance_variable_get(name)] end.join(' ') sprintf "#<%s:0x%014x %s @rows[%i]>", self.class, object_id, variables, row_count end
The last Row containing any data
# File lib/spreadsheet/worksheet.rb, line 216 def last_row row(last_row_index) end
The index of the last Row containing any data
# File lib/spreadsheet/worksheet.rb, line 220 def last_row_index [dimensions[1] - 1, 0].max end
Merges multiple cells into one.
# File lib/spreadsheet/worksheet.rb, line 285 def merge_cells start_row, start_col, end_row, end_col # FIXME enlarge or dup check @merged_cells.push [start_row, end_row, start_col, end_col] end
Set worklist protection
# File lib/spreadsheet/worksheet.rb, line 105 def protect! password = '' @protected = true password = password.to_s if password.size == 0 @password_hash = 0 else @password_hash = Excel::Password.password_hash password end end
Is the worksheet protected?
# File lib/spreadsheet/worksheet.rb, line 100 def protected? @protected end
Replace the Row at idx with the following arguments. Like update_row, but truncates the Row if there are fewer arguments than Cells in the Row.
# File lib/spreadsheet/worksheet.rb, line 226 def replace_row idx, *cells if(row = @rows[idx]) && cells.size < row.size cells.concat Array.new(row.size - cells.size) end update_row idx, *cells end
The number of Rows in this Worksheet which contain data.
# File lib/spreadsheet/worksheet.rb, line 239 def row_count dimensions[1] - dimensions[0] end
Tell Worksheet that the Row at idx has been updated and the dimensions need to be recalculated. You should not need to call this directly.
# File lib/spreadsheet/worksheet.rb, line 245 def row_updated idx, row @dimensions = nil @rows[idx] = row end
Updates the Row at idx with the following arguments.
# File lib/spreadsheet/worksheet.rb, line 251 def update_row idx, *cells res = if row = @rows[idx] row[0, cells.size] = cells row else Row.new self, idx, cells end row_updated idx, res res end
Renumbers all Rows starting at idx and calls row_updated for each of them.
# File lib/spreadsheet/worksheet.rb, line 264 def updated_from index index.upto(@rows.size - 1) do |idx| row = row(idx) row.idx = idx row_updated idx, row end end
# File lib/spreadsheet/excel.rb, line 47 def write row, col, data=nil, format=nil if data.is_a? Array write_row row, col, data, format else row = row(row) row[col] = data row.set_format col, format end end
# File lib/spreadsheet/excel.rb, line 56 def write_column row, col, data=nil, format=nil if data.is_a? Array data.each do |token| if token.is_a? Array write_row row, col, token, format else write row, col, token, format end row += 1 end else write row, col, data, format end end
# File lib/spreadsheet/excel.rb, line 70 def write_row row, col, data=nil, format=nil if data.is_a? Array data.each do |token| if token.is_a? Array write_column row, col, token, format else write row, col, token, format end col += 1 end else write row, col, data, format end end
Generated with the Darkfish Rdoc Generator 2.