gpt4 book ai didi

ruby-on-rails - Axlsx - 格式化单元格内的文本

转载 作者:数据小太阳 更新时间:2023-10-29 07:26:46 33 4
gpt4 key购买 nike

我似乎找不到任何关于是否可以用多个格式选项填充单个单元格的信息。

例如,我希望单元格 A1 填充文本:“你好世界,这是excel

这可能吗?如果可以,我应该使用什么语法来做到这一点?

最佳答案

对于内联样式,请使用富文本。 Here is an example from the axlsx page:

  p = Axlsx::Package.new
p.use_shared_strings = true
wb = p.workbook
wrap_text = wb.styles.add_style({:alignment => {:horizontal => :center, :vertical => :center, :wrap_text => true}} )
rt = Axlsx::RichText.new
rt.add_run('I\'m bold, ', :b => true)
rt.add_run('I\'m italic, ', :i => true)
rt.add_run('I\'m strike' + "\n", :strike => true)
rt.add_run('I\'m bold, italic and strike' + "\n", :b => true, :i => true, :strike => true)
rt.add_run('I\'m style-less :D')
wb.add_worksheet(:name => "RichText") do | sheet |
sheet.add_row [rt], :style => wrap_text
end
p.serialize 'rich_text.xlsx'

如果您只是想对一个单元格应用多种样式,则需要不同的样式。有两种选择:

首先,手动完成。基本上你声明你的样式类型是 :dxf。默认为 :xf。其他一切都一样。 From the docs on styles.rb:

p = Axlsx::Package.new
wb = p.workbook
ws = wb.add_worksheet

# define your styles
profitable = wb.styles.add_style(:bg_color => "FFFF0000",
:fg_color=>"#FF000000",
:type => :dxf)

ws.add_row ["Genreated At:", Time.now], :styles=>[nil, date_time]
ws.add_row ["Previous Year Quarterly Profits (JPY)"], :style=>title
ws.add_row ["Quarter", "Profit", "% of Total"], :style=>title
ws.add_row ["Q1", 4000, 40], :style=>[title, currency, percent]
ws.add_row ["Q2", 3000, 30], :style=>[title, currency, percent]
ws.add_row ["Q3", 1000, 10], :style=>[title, currency, percent]
ws.add_row ["Q4", 2000, 20], :style=>[title, currency, percent]

ws.add_conditional_formatting("A1:A7", { :type => :cellIs, :operator => :greaterThan, :formula => "2000", :dxfId => profitable, :priority => 1 })
f = File.open('example_differential_styling', 'w')
p.serialize(f)

其次,使用axlsx_styler gem 。这使得应用多种样式变得非常容易。

关于ruby-on-rails - Axlsx - 格式化单元格内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33896324/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com