Not able to style Excel with spreadsheet gem (Ruby

2019-09-15 08:00发布

Trying to style an excel following - ruby spreadsheet row background color but nothing is happening for me -

Here goes my code -

My formats:

pass_format = Spreadsheet::Format.new :color=> :blue, :pattern_fg_color => :green, :pattern => 1
fail_format = Spreadsheet::Format.new :color=> :blue, :pattern_fg_color => :red, :pattern => 1
skip_format = Spreadsheet::Format.new :color=> :blue, :pattern_fg_color => :yellow, :pattern => 1

Trying to use them here(just showing one rest are decided by if elses):

sheet1.row(counter).default_format = skip_format
sheet1[counter, 3] = 'Skipped'
sheet1.row(counter).default_format = skip_format
sheet1.row(counter).set_format(3, skip_format)

Counter is the row I am currently in. Here I am not sure whether I should format first or write first. What am I doing wrong? How to fix this?

Actually it's getting applied as I found from .inspect-

#<Spreadsheet::Format:0x007f082f9c1d58 @font=#<Spreadsheet::Font:0x007f082f9c1a88 @name="Arial", @color=:red, @previous_fast_key=nil, @size=nil, @weight=nil, @italic=nil, @strikeout=nil, @outline=nil, @shadow=nil, @escapement=nil, @underline=nil, @family=:swiss, @encoding=nil>, @number_format="GENERAL", @rotation=0, @pattern=1, @bottom_color=:black, @top_color=:black, @left_color=:black, @right_color=:black, @diagonal_color=:black, @pattern_fg_color=:yellow, @pattern_bg_color=:pattern_bg, @regexes={:date=>/[YMD]/, :date_or_time=>/[hmsYMD]/, :datetime=>/([YMD].*[HS])|([HS].*[YMD])/, :time=>/[hms]/, :number=>/[#]/}, @used_merge=0>

but even if it shows color red here in excel it's black. :(

I am editing the original file then writing in a new file as

 book.write "Result.xls"

Is it the wrong approach? I am going to try to make a new workbook before editing and update.

1条回答
叼着烟拽天下
2楼-- · 2019-09-15 08:37

Well, it was not possible to format the existing excel then write it as a new Excel. Formatting was lost in that. To overcome I created a new excel (populated with my existing data read from the old excel) formatted it as I want then used

book.write "xxx.xls"

查看更多
登录 后发表回答