class GoogleDrive::List

Provides access to cells using column names. Use GoogleDrive::Worksheet#list to get GoogleDrive::List object.

Public Class Methods

new(worksheet) click to toggle source

@api private

# File lib/google_drive/list.rb, line 19
def initialize(worksheet)
  @worksheet = worksheet
end

Public Instance Methods

[](index) click to toggle source

Returns Hash-like object (GoogleDrive::ListRow) for the row with the index. Keys of the object are colum names (the first row). The second row has index 0.

Note that updates to the returned object are not sent to the server until you call GoogleDrive::Worksheet#save().

# File lib/google_drive/list.rb, line 34
def [](index)
  ListRow.new(self, index)
end
[]=(index, hash) click to toggle source

Updates the row with the index with the given Hash object. Keys of hash are colum names (the first row). The second row has index 0.

Note that update is not sent to the server until you call GoogleDrive::Worksheet#save().

# File lib/google_drive/list.rb, line 44
def []=(index, hash)
  self[index].replace(hash)
end
each() { |self| ... } click to toggle source

Iterates over Hash-like object (GoogleDrive::ListRow) for each row (except for the first row). Keys of the object are colum names (the first row).

# File lib/google_drive/list.rb, line 51
def each(&_block)
  for i in 0...size
    yield(self[i])
  end
end
get(index, key) click to toggle source

@api private

# File lib/google_drive/list.rb, line 95
def get(index, key)
  @worksheet[index + 2, key_to_col(key)]
end
input_value(index, key) click to toggle source

@api private

# File lib/google_drive/list.rb, line 105
def input_value(index, key)
  @worksheet.input_value(index + 2, key_to_col(key))
end
keys() click to toggle source

Column names i.e. the contents of the first row. Duplicates are removed.

# File lib/google_drive/list.rb, line 59
def keys
  (1..@worksheet.num_cols).map { |i| @worksheet[1, i] }.uniq
end
keys=(ary) click to toggle source

Updates column names i.e. the contents of the first row.

Note that update is not sent to the server until you call GoogleDrive::Worksheet#save().

# File lib/google_drive/list.rb, line 67
def keys=(ary)
  for i in 1..ary.size
    @worksheet[1, i] = ary[i - 1]
  end
  for i in (ary.size + 1)..@worksheet.num_cols
    @worksheet[1, i] = ''
  end
end
numeric_value(index, key) click to toggle source

@api private

# File lib/google_drive/list.rb, line 100
def numeric_value(index, key)
  @worksheet.numeric_value(index + 2, key_to_col(key))
end
push(hash) click to toggle source

Adds a new row to the bottom. Keys of hash are colum names (the first row). Returns GoogleDrive::ListRow for the new row.

Note that update is not sent to the server until you call GoogleDrive::Worksheet#save().

# File lib/google_drive/list.rb, line 82
def push(hash)
  row = self[size]
  row.update(hash)
  row
end
set(index, key, value) click to toggle source

@api private

# File lib/google_drive/list.rb, line 110
def set(index, key, value)
  @worksheet[index + 2, key_to_col(key)] = value
end
size() click to toggle source

Number of non-empty rows in the worksheet excluding the first row.

# File lib/google_drive/list.rb, line 24
def size
  @worksheet.num_rows - 1
end
to_hash_array() click to toggle source

Returns all rows (except for the first row) as Array of Hash. Keys of Hash objects are colum names (the first row).

# File lib/google_drive/list.rb, line 90
def to_hash_array
  map(&:to_hash)
end

Private Instance Methods

key_to_col(key) click to toggle source
# File lib/google_drive/list.rb, line 116
def key_to_col(key)
  key = key.to_s
  col = (1..@worksheet.num_cols).find { |c| @worksheet[1, c] == key }
  unless col
    raise(GoogleDrive::Error, format("Column doesn't exist: %p", key))
  end
  col
end