Module: RubyXL::LegacyWorksheet

Includes:
Enumerable
Included in:
Worksheet
Defined in:
lib/rubyXL/worksheet.rb

Instance Method Summary collapse

Instance Method Details

#[](row = 0) ⇒ Object

allows for easier access to sheet_data



18
19
20
# File 'lib/rubyXL/worksheet.rb', line 18

def [](row = 0)
  sheet_data[row]
end

#add_cell(row_index = 0, column_index = 0, data = '', formula = nil, overwrite = true) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rubyXL/worksheet.rb', line 85

def add_cell(row_index = 0, column_index = 0, data = '', formula = nil, overwrite = true)
  validate_workbook
  validate_nonnegative(row_index)
  validate_nonnegative(column_index)
  row = sheet_data.rows[row_index] || add_row(row_index)

  c = row.cells[column_index]

  if overwrite || c.nil?
    c = RubyXL::Cell.new
    c.worksheet = self
    c.row = row_index
    c.column = column_index
    c.raw_value = data
    c.datatype = RubyXL::DataType::RAW_STRING unless formula || data.is_a?(Numeric)
    c.formula = RubyXL::Formula.new(:expression => formula) if formula

    range = cols && cols.locate_range(column_index)
    c.style_index = row.style_index || (range && range.style_index) || 0
    row.cells[column_index] = c
  end

  c
end

#add_row(row_index = 0, params = {}) ⇒ Object



79
80
81
82
83
# File 'lib/rubyXL/worksheet.rb', line 79

def add_row(row_index = 0, params = {})
  new_row = RubyXL::Row.new(params)
  new_row.worksheet = self
  sheet_data.rows[row_index] = new_row
end

#change_column_fill(column_index, color_index = 'ffffff') ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubyXL/worksheet.rb', line 58

def change_column_fill(column_index, color_index='ffffff')
  validate_workbook
  Color.validate_color(color_index)
  ensure_cell_exists(0, column_index)

  cols.get_range(column_index).style_index = @workbook.modify_fill(get_col_style(column_index), color_index)

  sheet_data.rows.each { |row|
    c = row[column_index]
    c.change_fill(color_index) if c
  }
end

#change_column_width(column_index, width_in_chars = RubyXL::ColumnRange::DEFAULT_WIDTH) ⇒ Object



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

def change_column_width(column_index, width_in_chars = RubyXL::ColumnRange::DEFAULT_WIDTH)
  change_column_width_raw(column_index, ((width_in_chars + (5.0 / RubyXL::Font::MAX_DIGIT_WIDTH)) * 256).to_i / 256.0)
end

#change_column_width_raw(column_index, width) ⇒ Object

Set raw column width value



44
45
46
47
48
49
50
# File 'lib/rubyXL/worksheet.rb', line 44

def change_column_width_raw(column_index, width)
  validate_workbook
  ensure_cell_exists(0, column_index)
  range = cols.get_range(column_index)
  range.width = width
  range.custom_width = true
end

#delete_column(column_index = 0) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rubyXL/worksheet.rb', line 161

def delete_column(column_index = 0)
  validate_workbook
  validate_nonnegative(column_index)

  #delete column
  sheet_data.rows.each { |row| row.cells.delete_at(column_index) }

  # Change column numbers for cells to the right of the deleted column
  sheet_data.rows.each_with_index { |row, row_index|
    row.cells.each_with_index { |c, column_index|
      c.column = column_index if c.is_a?(Cell)
    }
  }

  cols.each { |range| range.delete_column(column_index) }
end

#delete_row(row_index = 0) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rubyXL/worksheet.rb', line 110

def delete_row(row_index=0)
  validate_workbook
  validate_nonnegative(row_index)

  deleted = sheet_data.rows.delete_at(row_index)

  # Change cell row numbers
  row_index.upto(sheet_data.size - 1) { |index|
    sheet_data[index].cells.each{ |c| c.row -= 1 unless c.nil? }
  }

  return deleted
end

#eachObject



22
23
24
# File 'lib/rubyXL/worksheet.rb', line 22

def each
  sheet_data.rows.each { |row| yield(row) }
end

#initialize(params = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rubyXL/worksheet.rb', line 5

def initialize(params = {})
  super
  self.workbook   = params[:workbook]
  self.sheet_name = params[:sheet_name]
  self.sheet_id   = params[:sheet_id]
  self.sheet_data = RubyXL::SheetData.new
  self.cols = RubyXL::ColumnRanges.new
  @comments = [] # Do not optimize! These are arrays, so they will share the pointer!
  @printer_settings = []
  @generic_storage = []
end

#insert_column(column_index = 0) ⇒ Object

Inserts column at column_index, pushes everything right, takes styles from column to left NOTE: use of this method will break formulas which reference cells which are being “pushed right”



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/rubyXL/worksheet.rb', line 180

def insert_column(column_index = 0)
  validate_workbook
  ensure_cell_exists(0, column_index)

  old_range = cols.get_range(column_index)

  #go through each cell in column
  sheet_data.rows.each_with_index { |row, row_index|
    old_cell = row[column_index]
    c = nil

    if old_cell && old_cell.style_index != 0 &&
         old_range && old_range.style_index != old_cell.style_index then

      c = RubyXL::Cell.new(:style_index => old_cell.style_index, :worksheet => self,
                           :row => row_index, :column => column_index,
                           :datatype => RubyXL::DataType::SHARED_STRING)
    end

    row.insert_cell_shift_right(c, column_index)
  }

  cols.insert_column(column_index)

  # TODO: update column numbers
end

#insert_row(row_index = 0) ⇒ Object

Inserts row at row_index, pushes down, copies style from the row above (that’s what Excel 2013 does!) NOTE: use of this method will break formulas which reference cells which are being “pushed down”



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rubyXL/worksheet.rb', line 126

def insert_row(row_index = 0)
  validate_workbook
  ensure_cell_exists(row_index)

  old_row = new_cells = nil

  if row_index > 0 then
    old_row = sheet_data.rows[row_index - 1]
    if old_row then
      new_cells = old_row.cells.collect { |c|
                    if c.nil? then nil
                    else nc = RubyXL::Cell.new(:style_index => c.style_index)
                         nc.worksheet = self
                         nc
                    end
                  }
    end
  end

  row0 = sheet_data.rows[0]
  new_cells ||= Array.new((row0 && row0.cells.size) || 0)

  sheet_data.rows.insert(row_index, nil)
  new_row = add_row(row_index, :cells => new_cells, :style_index => old_row && old_row.style_index)

  # Update row values for all rows below
  row_index.upto(sheet_data.rows.size - 1) { |i|
    row = sheet_data.rows[i]
    next if row.nil?
    row.cells.each { |c| c.row = i unless c.nil? }
  }

  return new_row
end

#merge_cells(row1 = 0, col1 = 0, row2 = 0, col2 = 0) ⇒ Object

merges cells within a rectangular range



72
73
74
75
76
77
# File 'lib/rubyXL/worksheet.rb', line 72

def merge_cells(row1 = 0, col1 = 0, row2 = 0, col2 = 0)
  validate_workbook

  self.merged_cells ||= RubyXL::MergedCells.new
  merged_cells << RubyXL::MergedCell.new(:ref => RubyXL::Reference.new(row1, row2, col1, col2))
end