Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
common excel api. more generate options, specs
- Loading branch information
Thomas Statter
committed
Sep 25, 2012
1 parent
37757a4
commit 62583d9
Showing
23 changed files
with
434 additions
and
421 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright:: Autotelik Media Ltd | ||
# Author :: Tom Statter | ||
# Date :: July 2010 | ||
# License:: | ||
# | ||
# | ||
if(DataShift::Guards::jruby?) | ||
|
||
require 'java' | ||
require "poi-3.7-20101029.jar" | ||
|
||
# Extend the Poi classes with some syntactic sugar | ||
|
||
class Java::OrgApachePoiHssfUsermodel::HSSFSheet | ||
def name() | ||
getSheetName | ||
end | ||
|
||
def num_rows | ||
getPhysicalNumberOfRows | ||
end | ||
|
||
end | ||
|
||
class Java::OrgApachePoiHssfUsermodel::HSSFRow | ||
|
||
include RubyPoiTranslations | ||
|
||
include Enumerable | ||
|
||
def []( column) | ||
cell_value( get_or_create_cell( column ) ) | ||
end | ||
|
||
def []=( column, value ) | ||
get_or_create_cell(column, value).setCellValue((value.to_s || "")) | ||
end | ||
|
||
def get_or_create_cell( column, value = nil ) | ||
if(value) | ||
java_send(:getCell, [Java::int], column) || createCell(column, poi_cell_type(value)) | ||
else | ||
java_send(:getCell, [Java::int], column) || java_send(:createCell, [Java::int], column) | ||
end | ||
end | ||
|
||
def idx | ||
getRowNum() | ||
end | ||
|
||
# Iterate over each column in the row and yield on the cell | ||
def each(&block) | ||
cellIterator.each {|c| yield cell_value(c) } | ||
end | ||
|
||
# TODO | ||
# for min, max and sort from enumerable need <=> | ||
# def <=> end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# To change this template, choose Tools | Templates | ||
# and open the template in the editor. | ||
|
||
module ExcelBase | ||
|
||
def sanitize_sheet_name( name ) | ||
name.gsub(/[\[\]:\*\/\\\?]/, '') | ||
end | ||
|
||
# Helpers for dealing with Active Record models and collections | ||
|
||
def ar_to_headers( records ) | ||
return if( !records.first.is_a?(ActiveRecord::Base) || records.empty?) | ||
|
||
headers = records.first.class.columns.collect( &:name ) | ||
set_headers( headers ) | ||
end | ||
|
||
|
||
# Pass a set of AR records | ||
def ar_to_xls(records, options = {}) | ||
return if( ! records.first.is_a?(ActiveRecord::Base) || records.empty?) | ||
|
||
row_index = | ||
if(options[:no_headers]) | ||
0 | ||
else | ||
ar_to_headers( records ) | ||
1 | ||
end | ||
|
||
records.each do |record| | ||
create_row(row_index) | ||
|
||
ar_to_xls_row(0, record) | ||
|
||
row_index += 1 | ||
end | ||
end | ||
|
||
|
||
# Save data from an AR record to the current row, based on the record's columns [c1,c2,c3] | ||
# Returns the number of the final column written to | ||
def ar_to_xls_row(row, start_column, record) | ||
return unless( record.is_a?(ActiveRecord::Base)) | ||
|
||
column = start_column | ||
record.class.columns.each do |connection_column| | ||
ar_to_xls_cell(row, column, record, connection_column) | ||
column += 1 | ||
end | ||
column | ||
end | ||
|
||
def ar_to_xls_cell(row, column, record, connection_column) | ||
begin | ||
datum = record.send(connection_column.name) | ||
|
||
self[row, column] = datum | ||
rescue => e | ||
puts "Failed to export #{datum} from #{connection_column.inspect} to column #{column}" | ||
puts e, e.backtrace | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.