views:

377

answers:

1

I'm using http://spreadsheet.rubyforge.org to generate a spreadsheet and I'm having trouble with something. I'm opening an existing workbook and adding data to it.

I have manged to get number formatting working to some extent, at least excel is seeing this data as a number, but (very un-excel like) the client would like the number aligned in the center :(

My current code looks something like this:

nfmt = Spreadsheet::Format.new :number_format => '0.00'
row = sheet.row(1)
row[0] = "Result"
row[1] = 45.55
row.set_format 1, nfmt

Maybe a little far fetched but wondered if anybody can help?

  • Spreadsheet does not modify Formatting at present. That means in particular that if you set the Value of a Cell to a Date, it can only be read as a Date if its Format was set correctly prior to the change.

Edit for Trevoke

Thanks for helping with this. I've tried your code and it works fine. I guess the difference is I'm editing an existing spreadsheet, in which case the formatting is ignored. Try this:

require 'rubygems'
require 'spreadsheet'

Spreadsheet.client_encoding = 'UTF-8'

book = Spreadsheet.open "edit_me.xls" # Blank spreadsheet

sheet1 = book.worksheet 0

format = Spreadsheet::Format.new :horizontal_align => :centre
sheet1.row(0).default_format = format
sheet1.row(0).push 'I rule 2!', 43.56

book.write 'edited_you.xls'
+1  A: 

Well, here's the code for the library's format methods: format.rb. Last time I used that gem, it didn't do formatting very well, but it looks like it's been updated since.

You can try this:

  54     # Horizontal alignment    
  55     # Valid values: :default, :left, :center, :right, :fill, :justify, :merge,    
  56     #               :distributed    
  57     # Default:      :default    
  58     enum :horizontal_align, :default, :left, :center, :right, :fill, :justify,    
  59                             :merge, :distributed,    
  60          :center      => :centre,    
  61          :merge       => [ :center_across, :centre_across ],    
  62          :distributed => :equal_space

EDIT! Additional information

require 'rubygems'
require 'spreadsheet'

Spreadsheet.client_encoding = 'UTF-8'

book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet :name => 'test'

format = Spreadsheet::Format.new :horizontal_align => :centre
sheet1.row(0).default_format = format
sheet1.row(0).push 'I rule!'

book.write 'test.xls'

This worked for me. Try it - with both :centre and :center. Let me know.

Trevoke
Thanks, I've already tried setting that but it doesn't seem to work. I've added a comment I found in the guide to my question. Although I don't fully understand the meaning it says it doesn't modify formatting, even though my original file has centre alignment in it, writing new data seems to replace that.
tsdbrown
That's "data format" not "display format". Data format is date, currency, etc.Display is .. Well, broken, I guess. Time to send them a bug report!
Trevoke
Before I do that I better check (if you don't mind) that I've understood how to use this correctly! enum adds an instance method to the format class right? With the first arg being the default (:default). I've tried passing :horizontal_align => :center in the call to Format.new and also calling it after creating the format object (nmft), neither work.
tsdbrown
With the edit to this post, can you get it to work?
Trevoke
Please see my edited question. In fact I've found that by leaving the formatting alone completely and calling .to_f on any decimal values they align in the center as required. As long as the spreadsheet is setup that way beforehand.Looks like there is a problem with the library though.
tsdbrown