Class: Spreadsheet::Workbook
- Inherits:
-
Object
- Object
- Spreadsheet::Workbook
- Defined in:
- lib/protk/spreadsheet_extensions.rb
Instance Method Summary collapse
-
#copyBook(numrows = 0) ⇒ Object
creates an output excel file (returning the workbook object), transcribing all original content up to the given number of rows Throws an error if the input contains more than 1 worksheet.
Instance Method Details
#copyBook(numrows = 0) ⇒ Object
creates an output excel file (returning the workbook object), transcribing all original content up to the given number of rows Throws an error if the input contains more than 1 worksheet
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/protk/spreadsheet_extensions.rb', line 34 def copyBook(numrows=0) if ( !numrows ) numrows=0 end # Create a new workbook from scratch for writing outputBook = Spreadsheet::Workbook.new outputSheet = outputBook.create_worksheet # There should only be one worksheet in the input workbook worksheets=self.worksheets if ( self.worksheets.length != 1 ) puts "More than one worksheet in this excel file. This script only operates on single worksheets" end # Get the worksheet inputSheet=self.worksheet 0 # Figure out how many rows to convert if not specified if ( numrows==0 || numrows > (inputSheet.row_count+1)) numrows=inputSheet.row_count end # Transcribe everything from the old worksheet to the new one puts "Creating new spreadsheet with #{numrows} rows" (0...[numrows,inputSheet.row_count].min).each { |r| outputSheet.insert_row(r,inputSheet.row(r)) newRow=outputSheet.row(r) # After inserting the row make sure it doesn't contain any nil values newRow.each_index { |ci| if ( newRow[ci]==nil) newRow[ci]="" end } } outputBook end |