Skip to content

Instantly share code, notes, and snippets.

@gregory
Created April 26, 2012 11:56
Show Gist options
  • Select an option

  • Save gregory/2499101 to your computer and use it in GitHub Desktop.

Select an option

Save gregory/2499101 to your computer and use it in GitHub Desktop.
Axlsx::Worksheet.class_eval do
def add_row_with_position(values = [], options = {})
if options.try(:[], :start_at).present?
val = []
options[:start_at].times { val.push nil}
options.delete(:start_at)
values = val + values
end
add_row_without_position(values, options)
end
alias_method_chain :add_row, :position
end
@randym

randym commented Apr 27, 2012

Copy link
Copy Markdown

@metakungfu - sorry I missed you on IRC!
I assume you meant to alias :add_row, add_row_without_position?
Anyway, I really like the idea of adding :start_at as an option for Worksheet#add_row.

Fork and hack at will!

@gregory

gregory commented Apr 27, 2012

Copy link
Copy Markdown
Author

@randym hey dude, i taught it would be nice tough this bunch of code drops me some stack level too deep.
Their might be something breaking down deeper in your code, thats why i came to you :)

@randym

randym commented Apr 28, 2012

Copy link
Copy Markdown
#!/usr/bin/env ruby -w -s
# -*- coding: utf-8 -*-
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
require 'axlsx'

Axlsx::Worksheet.class_eval do
  def add_row_with_position(values = [], options = {})
    values = Array.new(options[:start_at] || 0) + values
    add_row(values, options)
  end
end

p = Axlsx::Package.new
wb = p.workbook
wb.add_worksheet do |ws|
ws.add_row_with_position [1,2,3,4,5]
ws.add_row_with_position [1,2,3,4,5], :start_at => 5

end
p.validate.each { |e| puts e.message }
p.serialize("stack.xlsx")

@randym

randym commented Apr 28, 2012

Copy link
Copy Markdown

greets mate- obviously you don't need the load path bit there - but I think this is what your looking todo.
Ill add an :offset option to add row that provides this functionality. definitely beats [nil, nil, nil, nil, 'real data'] stuff

@gregory

gregory commented Apr 28, 2012

Copy link
Copy Markdown
Author

yes it's sexier :D
I ll try this on monday when i m back at work and i ll let you know if ever i still get that stack too deep error message.

Anyway thanks dude! I ve been hacking into poi with monkey patching to get the kind of stuffs you bring trough your gem .. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment