Class: Xcellus::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/xcellus.rb

Overview

Instance represents a XLSX file, and exposes methods to manipulate data on the file

Instance Method Summary collapse

Constructor Details

#initialize(handle) ⇒ Instance

Internal: Creates a new instance with the provided handle



69
70
71
# File 'lib/xcellus.rb', line 69

def initialize(handle)
  @handle = handle
end

Instance Method Details

#append(data) ⇒ Object

Appends sheets and rows to the loaded file. This method expects the same structure of Xcellus::transform, with the difference that it creates (when necessary) sheets, and appends data to them.



127
128
129
130
131
132
# File 'lib/xcellus.rb', line 127

def append(data)
  unless data.kind_of? Array
    raise ArgumentError, 'Xcellus.append only accepts Arrays'
  end
  Xcellus::_append(@handle, data.to_json)
end

#closeObject

Close ensures that all resources allocated when ‘load` is called are freed. This method MUST be called after you’re done handling data.



111
112
113
# File 'lib/xcellus.rb', line 111

def close
  Xcellus::_close(@handle)
end

#find_in_column(sheet_name, column_index, value) ⇒ Object

Searches a given sheet for a provided value in a specific column. sheet_name: Name of the sheet to lookup for ‘value`. Immediately returns

-1 when the sheet cannot be found.

column_index: Index of the column to lookup for the provided value. value: Value to lookup for. Automatically converted and compared

as an String.


79
80
81
82
83
84
85
86
87
# File 'lib/xcellus.rb', line 79

def find_in_column(sheet_name, column_index, value)
  unless sheet_name.kind_of? String
    raise ArgumentError, 'Invalid sheet name'
  end
  unless column_index.kind_of? Integer
    raise ArgumentError, 'Invalid column index'
  end
  Xcellus::_find_in_column(@handle, sheet_name, column_index, value.to_s)
end

#replace_row(sheet_name, index, value) ⇒ Object

Replaces the row at ‘index` in the provided `sheet_name`. sheet_name: Name of the sheet in which the row must be replaced. Throws

a StandardException when a sheet with the provided name
cannot be found.

index: Index of the row to be replaced. value: An array with values to be replaced. Passing ‘nil` prevents

values of the cell in the same index from being changed.


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/xcellus.rb', line 96

def replace_row(sheet_name, index, value)
  unless sheet_name.kind_of? String
    raise ArgumentError, 'Invalid sheet name'
  end
  unless index.kind_of? Integer
    raise ArgumentError, 'Invalid column index'
  end
  unless value.kind_of? Array
    raise ArgumentError, 'Invalid value: should be an array'
  end
  Xcellus::_replace_row(@handle, sheet_name, value.to_json, index)
end

#save(path) ⇒ Object

Saves the current modifications to the provided path.



116
117
118
119
120
121
122
# File 'lib/xcellus.rb', line 116

def save(path)
  unless path.kind_of? String
    raise ArgumentError, 'save expects a string path'
  end

  Xcellus::_save(@handle, path)
end