From 4d16bfc43780e5d3f7368625700b583e3e98217a Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 27 Nov 2011 18:11:42 +0900 Subject: adding in row_style and col_style methods to worksheet and active record 'acts_as_axlsx' to provide to_xlsx. --- lib/axlsx.rb | 1 + lib/axlsx/ar.rb | 60 +++++++++++++++++++++++++++++++ lib/axlsx/package.rb | 7 +++- lib/axlsx/version.rb | 2 +- lib/axlsx/workbook/worksheet/row.rb | 7 ++++ lib/axlsx/workbook/worksheet/worksheet.rb | 29 ++++++++++++++- 6 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 lib/axlsx/ar.rb (limited to 'lib') diff --git a/lib/axlsx.rb b/lib/axlsx.rb index aee52b36..0a84a7e1 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -16,6 +16,7 @@ require 'axlsx/drawing/drawing.rb' require 'axlsx/workbook/workbook.rb' require 'axlsx/package.rb' +require 'axlsx/ar.rb' #required gems diff --git a/lib/axlsx/ar.rb b/lib/axlsx/ar.rb new file mode 100644 index 00000000..2864c95a --- /dev/null +++ b/lib/axlsx/ar.rb @@ -0,0 +1,60 @@ +# ActsAsAxlsx +require 'axlsx' +module Axlsx + + module Ar + + def self.included(base) + base.send :extend, ClassMethods + end + + module ClassMethods + + # we should do what ruport did and use only, exclude and methods hashes + def acts_as_axlsx(options={}) + include Axlsx::Ar::InstanceMethods + extend Axlsx::Ar::SingletonMethods + end + end + + module SingletonMethods + + def to_xlsx(number = :all, options = {}) + row_style = options.delete(:style) + header_style = options.delete(:header_style) || row_style + types = options.delete(:types) + + data = [*find(number, options)] + data.compact! + data.flatten! + columns = data.first.attributes.keys + p = Package.new + row_style = p.workbook.styles.add_style(row_style) unless row_style.nil? + header_style = p.workbook.styles.add_style(header_style) unless header_style.nil? + + p.workbook.add_worksheet(:name=>table_name.humanize) do |sheet| + sheet.add_row columns, :style=>header_style + data.each do |r| + sheet.add_row r.attributes.values, :style=>row_style, :types=>types + end + end + p + end + + end + + module InstanceMethods + end + + + end + +end +begin +require 'active_record' +ActiveRecord::Base.send :include, Axlsx::Ar +rescue Exception=>e + puts "Running without active record extensions" +end + + diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index d9455f14..921646da 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -15,7 +15,12 @@ module Axlsx @core.creator = options[:author] || @core.creator yield self if block_given? end - + + # Accepts a ruport table for serialization to xlsx + # @param [Table] table a ruport Table object + def ruport_table(table) + puts table + end # The workbook this package will serialize or validate. # @return [Workbook] If no workbook instance has been assigned with this package a new Workbook instance is returned. diff --git a/lib/axlsx/version.rb b/lib/axlsx/version.rb index 2ecaf2a9..92993f79 100644 --- a/lib/axlsx/version.rb +++ b/lib/axlsx/version.rb @@ -1,4 +1,4 @@ module Axlsx # version - VERSION="1.0.9" + VERSION="1.0.10" end diff --git a/lib/axlsx/workbook/worksheet/row.rb b/lib/axlsx/workbook/worksheet/row.rb index 96a1c3d1..442dcf7b 100644 --- a/lib/axlsx/workbook/worksheet/row.rb +++ b/lib/axlsx/workbook/worksheet/row.rb @@ -54,6 +54,13 @@ module Axlsx c end + # sets the style for every cell in this row + def style=(style) + cells.each_with_index do | cell, index | + s = style.is_a?(Array) ? style[index] : style + cell.style = s + end + end private diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 6dfbc060..65f620e4 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -89,6 +89,33 @@ module Axlsx @rows.last end + # Set the style for cells in a specific row + # @param [Integer] index the index of the row + # @param [Integer] the cellXfs index + # @option options [Integer] col_offset only cells after this column will be updated. + # @note You can also specify the style in the add_row call + # @see Worksheet#add_row + # @see README.md for an example + def row_style(index, style, options={}) + raise ArgumentError, "Invalid Row Index" unless index < @rows.size + offset = options.delete(:col_offset) || 0 + @rows[index].cells[(offset..-1)].each { |c| c.style = style } + end + + + # Set the style for cells in a specific column + # @param [Integer] index the index of the column + # @param [Integer] the cellXfs index + # @option options [Integer] row_offset only cells after this column will be updated. + # @note You can also specify the style for specific columns in the call to add_row by using an array for the :styles option + # @see Worksheet#add_row + # @see README.md for an example + def col_style(index, style, options={}) + raise ArgumentError, "Invalid Column Index" unless index < @rows.first.cells.size + offset = options.delete(:row_offset) || 0 + @rows[(offset..-1)].each { |r| r.cells[index].style = style } + end + # Adds a chart to this worksheets drawing. This is the recommended way to create charts for your worksheet. This method wraps the complexity of dealing with ooxml drawing, anchors, markers graphic frames chart objects and all the other dirty details. # @param [Class] chart_type # @option options [Array] start_at @@ -183,7 +210,7 @@ module Axlsx # From ECMA docs # Column width measured as the number of characters of the maximum digit width of the numbers 0 .. 9 as # rendered in the normal style's font. There are 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines. - # width = Truncate([!{Number of Characters} * !{Maximum Digit Width} + !{5 pixel padding}]/!{Maximum Digit Width}*256)/256 + # width = Truncate([!{Number of Characters} * !{Maximum Digit Width} + !{5 pixel padding}]/{Maximum Digit Width}*256)/256 # @return [Float] # @param [Hash] A hash of auto_fit_data def auto_width(col) -- cgit v1.2.3