Class: OpenC3::ExcelSpreadsheet
Overview
Open an Excel spreadsheet and build an easily manipulated spreadsheet in ruby
Defined Under Namespace
Classes: ExcelWorksheet
Instance Attribute Summary collapse
-
#worksheets ⇒ Object
readonly
Returns the value of attribute worksheets.
Instance Method Summary collapse
-
#[](index) ⇒ ExcelWorksheet
Access a worksheet by passing in the name or index.
-
#initialize(filename, archive: nil) ⇒ ExcelSpreadsheet
constructor
A new instance of ExcelSpreadsheet.
-
#keys ⇒ Array<String>
Array of all the worksheet names.
Constructor Details
#initialize(filename, archive: nil) ⇒ ExcelSpreadsheet
Returns a new instance of ExcelSpreadsheet.
74 75 76 77 78 79 80 81 82 83 84 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/openc3/win32/excel.rb', line 74 def initialize(filename, archive: nil) if archive time = Time.now.sys = sprintf("%04u_%02u_%02u_%02u_%02u_%02u", time.year, time.month, time.mday, time.hour, time.min, time.sec) # If archive is true we use the system LOGS path if archive == true archive = OpenC3::System.paths['LOGS'] end archive = File.join(archive, "#{}_#{File.basename(filename)}") FileUtils.cp filename, archive File.chmod(0444, archive) # Mark read-only end begin excel = WIN32OLE.new('excel.application') excel.visible = false wb = excel.workbooks.open(filename) @worksheets = [] @lkup = {} count = wb.worksheets.count count.times do |index| ws = wb.worksheets(index + 1) @worksheets << ExcelWorksheet.new(ws) @lkup[ws.name] = @worksheets[-1] end ensure if excel excel.DisplayAlerts = false excel.quit end excel = nil GC.start end end |
Instance Attribute Details
#worksheets ⇒ Object (readonly)
Returns the value of attribute worksheets.
29 30 31 |
# File 'lib/openc3/win32/excel.rb', line 29 def worksheets @worksheets end |
Instance Method Details
#[](index) ⇒ ExcelWorksheet
Access a worksheet by passing in the name or index
119 120 121 122 123 124 125 |
# File 'lib/openc3/win32/excel.rb', line 119 def [](index) if index.is_a? String @lkup[index] else @worksheets[index] end end |