covard
covard

Reputation: 1807

Ruby Gem Spreadsheet - Format Issue

I am writing out data to an Excel sheet, from another Excel sheet. However, the data for a column could look like any of the following:

My problem is that when I write it to the file, Excel is marking the column as "General" for formatting, and that strips off the $ and % and makes it 0.2. Is there a way to set the format to be just a plain string so that it will leave the data intact when I write to the spreadsheet?

I am new to Ruby, so if you see anything in the code that should be done another way please let me know.

Code Example:

def self.add_data(new_book_sheet, old_book_sheet)
  row_index = 9
  header_row = 0
  old_book_sheet.each do |row|
    if header_row > 0
      column_index = 0
      row.each do |column|
        new_book_sheet.row(row_index).insert(column_index, column)
        column_index += 1
      end
      row_index += 1
    end
    header_row = 1
  end
end

Upvotes: 1

Views: 1063

Answers (2)

covard
covard

Reputation: 1807

So the problem is that when it is reading the data out of the original excel file if it is just a "number" type it is reading it that way so the $0.00 it is reading it as 0. I thought the problem was with the writing of the data but it is actually when it reads the data.

Upvotes: 2

bta
bta

Reputation: 45057

Add an apostrophe before the output and Excel will interpret it as text. I tested the following in Excel (directly in a spreadsheet, not using the gem), and all were interpreted as strings:

  • '$0.00
  • 'The amount owed at time of service is $33
  • '20%
  • '

Upvotes: 1

Related Questions