Class: XLSXDrone::Workbook

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

Overview

XLSX Workbook.

Constant Summary collapse

@@opened_workbooks =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xlsx_workbook_mpointer) ⇒ Workbook

Parameters:

  • xlsx_workbook_mpointer (FFI::MemoryPointer)


18
19
20
21
# File 'lib/xlsx_drone/workbook.rb', line 18

def initialize(xlsx_workbook_mpointer)
  @native_workbook = XLSXDrone::NativeBinding::XLSXWorkbookT.new(xlsx_workbook_mpointer)
  @@opened_workbooks << self
end

Class Method Details

.close_workbooksObject

You could use this method to close all opened workbooks at the same time.



10
11
12
13
14
# File 'lib/xlsx_drone/workbook.rb', line 10

def self.close_workbooks
  @@opened_workbooks.each do |wb|
    wb.close
  end
end

Instance Method Details

#closeTrueClass, FalseClass

Should-call method, once you finish working with the workbook.

Returns:

  • (TrueClass, FalseClass)

    depending on if the close was successful or not



66
67
68
69
70
71
72
73
74
# File 'lib/xlsx_drone/workbook.rb', line 66

def close
  if(XLSXDrone::NativeBinding.xlsx_close(@native_workbook) == 1)
    @@opened_workbooks.delete(self)
    @native_workbook = nil
    true
  else
    false
  end
end

#load_sheet(reference) ⇒ XLSXDrone::Sheet

Sheets aren’t loaded by default. You have to load them one by one, once you need them. You can reference a sheet passing its name or its index (first one is 1). Raises an exception if it can’t for some reason.

Parameters:

  • reference (String, Integer)

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xlsx_drone/workbook.rb', line 26

def load_sheet(reference)
  if(@native_workbook)
    loaded_sheet = \
      case reference
        when String
          XLSXDrone::NativeBinding.xlsx_load_sheet(@native_workbook, 0, reference)
        when Integer
          XLSXDrone::NativeBinding.xlsx_load_sheet(@native_workbook, reference, nil)
        else
          raise XLSXDrone::LogicError::ClientError::MalformedParams, "Pass a valid index as an #Integer (> 0 && <= #sheets_amount()), or a valid sheet name as a #String."
      end
    if(!loaded_sheet.null?)
      XLSXDrone::Sheet.new(loaded_sheet)
    else
      # no sheet was loaded
      case XLSXDrone::NativeBinding.xlsx_get_xlsx_errno()
        when -11
          raise XLSXDrone::LogicError::ClientError::MalformedParams, "Pass a valid index (> 0 && <= #sheets_amount()), or a valid sheet name."
        when -12
          raise NoMemoryError
        when -13
          raise XLSXDrone::UserError::IndexOutOfBounds, "If you pass an integer as parameter, note that can't surpass #sheets_amount()."
        when -14
          raise XLSXDrone::LogicError::InternalError::XMLParsingError, "The XLSX may be corrupted or it belongs to a version unsupported by this library."
        when -15
          raise XLSXDrone::UserError::NonExistent, "There's not such sheet with that name."
      end
    end
  else
    raise XLSXDrone::UserError::WorkbookClosed, "The workbook you're trying to access was already closed."
  end
end

#sheets_amountInteger

Returns the amount of sheets contained on this workbook.

Returns:

  • (Integer)

    the amount of sheets contained on this workbook



60
61
62
# File 'lib/xlsx_drone/workbook.rb', line 60

def sheets_amount
  @native_workbook[:n_sheets]
end