Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 562493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:33:38+00:00 2026-05-13T12:33:38+00:00

I’m using http://spreadsheet.rubyforge.org to generate a spreadsheet and I’m having trouble with something. I’m

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:33:38+00:00Added an answer on May 13, 2026 at 12:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 411k
  • Answers 411k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For a nice solution, why don't you create a Fibonacci… May 15, 2026 at 7:55 am
  • Editorial Team
    Editorial Team added an answer In this line: $raw=$this->data You're just assigning $this->data by value… May 15, 2026 at 7:55 am
  • Editorial Team
    Editorial Team added an answer Tried with something like SDL_Event e; while( SDL_WaitEvent(&e) ) {… May 15, 2026 at 7:55 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.