Class: Spreadsheet::Worksheet

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Encodings
Defined in:
lib/spreadsheet/worksheet.rb,
lib/spreadsheet/excel.rb

Overview

The Worksheet class. Contains most of the Spreadsheet data in Rows.

Interesting Attributes

#name

The Name of this Worksheet.

#default_format

The default format used for all cells in this Workhseet that have no format set explicitly or in Row#default_format.

#rows

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.

#columns

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.

#selected

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.

Direct Known Subclasses

Excel::Worksheet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Worksheet

Returns a new instance of Worksheet.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/spreadsheet/worksheet.rb', line 32

def initialize opts={}
  @default_format = nil
  @selected = opts[:selected]
  @dimensions = [0,0,0,0]
  @orientation = :portrait
  @name = opts[:name] || 'Worksheet'
  @workbook = opts[:workbook]
  @rows = []
  @columns = []
  @links = {}
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



31
32
33
# File 'lib/spreadsheet/worksheet.rb', line 31

def columns
  @columns
end

#fit_height_to_pagesObject

Returns the value of attribute fit_height_to_pages.



30
31
32
# File 'lib/spreadsheet/worksheet.rb', line 30

def fit_height_to_pages
  @fit_height_to_pages
end

#fit_width_to_pagesObject

Returns the value of attribute fit_width_to_pages.



30
31
32
# File 'lib/spreadsheet/worksheet.rb', line 30

def fit_width_to_pages
  @fit_width_to_pages
end

#nameObject

Returns the value of attribute name.



29
30
31
# File 'lib/spreadsheet/worksheet.rb', line 29

def name
  @name
end

#orientationObject

Returns the value of attribute orientation.



29
30
31
# File 'lib/spreadsheet/worksheet.rb', line 29

def orientation
  @orientation
end

#rowsObject (readonly)

Returns the value of attribute rows.



31
32
33
# File 'lib/spreadsheet/worksheet.rb', line 31

def rows
  @rows
end

#selectedObject

Returns the value of attribute selected.



29
30
31
# File 'lib/spreadsheet/worksheet.rb', line 29

def selected
  @selected
end

#workbookObject

Returns the value of attribute workbook.



29
30
31
# File 'lib/spreadsheet/worksheet.rb', line 29

def workbook
  @workbook
end

Instance Method Details

#[](row, column) ⇒ Object

Get the enriched value of the Cell at row, column. See also Worksheet#cell, Row#[].



247
248
249
# File 'lib/spreadsheet/worksheet.rb', line 247

def [] row, column
  row(row)[column]
end

#[]=(row, column, value) ⇒ Object

Set the value of the Cell at row, column to value. See also Row#[]=.



253
254
255
# File 'lib/spreadsheet/worksheet.rb', line 253

def []= row, column, value
  row(row)[column] = value
end

#activeObject

:nodoc:



43
44
45
46
# File 'lib/spreadsheet/worksheet.rb', line 43

def active # :nodoc:
  warn "Worksheet#active is deprecated. Please use Worksheet#selected instead."
  selected
end

#active=(selected) ⇒ Object

:nodoc:



47
48
49
50
# File 'lib/spreadsheet/worksheet.rb', line 47

def active= selected # :nodoc:
  warn "Worksheet#active= is deprecated. Please use Worksheet#selected= instead."
  self.selected = selected
end

#add_format(fmt) ⇒ Object

Add a Format to the Workbook. If you use Row#set_format, you should not need to use this Method.



54
55
56
# File 'lib/spreadsheet/worksheet.rb', line 54

def add_format fmt
  @workbook.add_format fmt if fmt
end

#cell(row, column) ⇒ Object

Get the enriched value of the Cell at row, column. See also Worksheet#[], Row#[].



60
61
62
# File 'lib/spreadsheet/worksheet.rb', line 60

def cell row, column
  row(row)[column]
end

#column(idx) ⇒ Object

Returns the Column at idx.



65
66
67
# File 'lib/spreadsheet/worksheet.rb', line 65

def column idx
  @columns[idx] || Column.new(idx, default_format, :worksheet => self)
end

#column_countObject

The number of columns in this Worksheet which contain data.



70
71
72
# File 'lib/spreadsheet/worksheet.rb', line 70

def column_count
  dimensions[3] - dimensions[2]
end

#column_updated(idx, column) ⇒ Object



73
74
75
# File 'lib/spreadsheet/worksheet.rb', line 73

def column_updated idx, column
  @columns[idx] = column
end

#default_formatObject

The default Format of this Worksheet, if you have set one. Returns the Workbook’s default Format otherwise.



86
87
88
# File 'lib/spreadsheet/worksheet.rb', line 86

def default_format
  @default_format || @workbook.default_format
end

#default_format=(format) ⇒ Object

Set the default Format of this Worksheet.



91
92
93
94
95
# File 'lib/spreadsheet/worksheet.rb', line 91

def default_format= format
  @default_format = format
  add_format format
  format
end

#delete_row(idx) ⇒ Object

Delete the Row at idx (0-based) from this Worksheet.



78
79
80
81
82
# File 'lib/spreadsheet/worksheet.rb', line 78

def delete_row idx
  res = @rows.delete_at idx
  updated_from idx
  res
end

#dimensionsObject

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. )



102
103
104
# File 'lib/spreadsheet/worksheet.rb', line 102

def dimensions
  @dimensions || recalculate_dimensions
end

#each(skip = , &block) ⇒ Object

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.



112
113
114
115
116
# File 'lib/spreadsheet/worksheet.rb', line 112

def each skip=dimensions[0], &block
  skip.upto(dimensions[1] - 1) do |idx|
    block.call row(idx)
  end
end

#encodingObject

:nodoc:



117
118
119
# File 'lib/spreadsheet/worksheet.rb', line 117

def encoding # :nodoc:
  @workbook.encoding
end

#format_column(idx, format = nil, opts = {}) ⇒ Object

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.



130
131
132
133
134
135
136
# File 'lib/spreadsheet/worksheet.rb', line 130

def format_column column, width=nil, format=nil
  if width.is_a? Format
    new_format_column column, width, format
  else
    new_format_column column, format, :width => width
  end
end

#format_dates!(format = nil) ⇒ Object

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/spreadsheet/worksheet.rb', line 150

def format_dates! format=nil
  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 || client('DD.MM.YYYY hh:mm:ss', 'UTF-8')
                 when Date
                   format || client('DD.MM.YYYY', 'UTF-8')
                 end
        case numfmt
        when Format
          row.set_format idx, numfmt
        when String
          fmt = row.format(idx).dup
          fmt.number_format = numfmt
          row.set_format idx, fmt
        end
      end
    end
  end
end

#insert_row(idx, cells = []) ⇒ Object

Insert a Row at idx (0-based) containing cells



174
175
176
177
178
# File 'lib/spreadsheet/worksheet.rb', line 174

def insert_row idx, cells=[]
  res = @rows.insert idx, Row.new(self, idx, cells)
  updated_from idx
  res
end

#inspectObject



179
180
181
182
183
184
185
186
187
# File 'lib/spreadsheet/worksheet.rb', line 179

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

#last_rowObject

The last Row containing any data



189
190
191
# File 'lib/spreadsheet/worksheet.rb', line 189

def last_row
  row(last_row_index)
end

#last_row_indexObject

The index of the last Row containing any data



193
194
195
# File 'lib/spreadsheet/worksheet.rb', line 193

def last_row_index
  [dimensions[1] - 1, 0].max
end

#new_format_columnObject



38
# File 'lib/spreadsheet/excel.rb', line 38

alias :new_format_column :format_column

#replace_row(idx, *cells) ⇒ Object

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.



199
200
201
202
203
204
# File 'lib/spreadsheet/worksheet.rb', line 199

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

#row(idx) ⇒ Object

The Row at idx or a new Row.



207
208
209
# File 'lib/spreadsheet/worksheet.rb', line 207

def row idx
  @rows[idx] || Row.new(self, idx)
end

#row_countObject

The number of Rows in this Worksheet which contain data.



212
213
214
# File 'lib/spreadsheet/worksheet.rb', line 212

def row_count
  dimensions[1] - dimensions[0]
end

#row_updated(idx, row) ⇒ Object

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.



218
219
220
221
# File 'lib/spreadsheet/worksheet.rb', line 218

def row_updated idx, row
  @dimensions = nil
  @rows[idx] = row
end

#update_row(idx, *cells) ⇒ Object

Updates the Row at idx with the following arguments.



224
225
226
227
228
229
230
231
232
233
# File 'lib/spreadsheet/worksheet.rb', line 224

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

#updated_from(index) ⇒ Object

Renumbers all Rows starting at idx and calls #row_updated for each of them.



237
238
239
240
241
242
243
# File 'lib/spreadsheet/worksheet.rb', line 237

def updated_from index
  index.upto(@rows.size - 1) do |idx|
    row = row(idx)
    row.idx = idx
    row_updated idx, row
  end
end

#write(row, col, data = nil, format = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
# 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

#write_column(row, col, data = nil, format = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

#write_row(row, col, data = nil, format = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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

#write_url(row, col, url, string = url, format = nil) ⇒ Object



84
85
86
# File 'lib/spreadsheet/excel.rb', line 84

def write_url row, col, url, string=url, format=nil
  row(row)[col] = Link.new url, string
end