如何使用 Spreadsheet gem 在 Ruby 中创建新的电子表格工作表?
具体来说,我想做的是在已有的工作表旁边添加新的工作表。我尝试使用 book.create_worksheet :name => 'new_sheet' 但它会覆盖以前的工作表。
我在这里搜索了该网站,看到有些人使用不同的 gem 允许“book.add_worksheet”(电子表格 gem 应该支持其他 gem,就像它应该是 3 个 gem 合 1 之类的......)并且这几乎也有效,但在执行 sheet = book.add_worksheet("new_sheet")< 行时,出现错误
undefined method 'workbook=' for "new_sheet":String (NoMethodError)
/代码>。
我尝试的另一件事是 sheet = Spreadsheet::Worksheet.new
,我在 Spreadsheet rubyforge 页面,其中有公共类方法 new(opts={})
,如果您单击查看代码,其中包含行 @name =选择[:名称] || Worksheet
这让我相信我应该能够使用它来创建和命名新的工作表,但我无法找出正确的语法。
我正在尝试的事情可能吗?看起来我已经很接近了,但还没有完全实现。
Specifically what I am trying to do is add new worksheets alongside ones already there. I've tried to use book.create_worksheet :name => 'new_sheet'
but it overwrites the previous worksheet.
I searched the site here and saw some people using a different gem that allowed "book.add_worksheet" (the Spreadsheet gem is supposed to have support for other gems, like it's supposed to be like 3 gems in 1 or something...) and that almost worked as well but I get the error undefined method 'workbook=' for "new_sheet":String (NoMethodError)
when doing the line sheet = book.add_worksheet("new_sheet")
.
Another thing I tried was sheet = Spreadsheet::Worksheet.new
and I see on the Spreadsheet rubyforge page that there is the public class method new(opts={})
which, if you click to see the code, includes the line @name = opts[:name] || Worksheet
which leads me to believe I should be able to use this to create and name a new worksheet, but I can't figure out the correct syntax.
Is what I am attempting possible? It seems like I'm getting close but I'm not quite hitting it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
book.create_worksheet(:name => 'unique_name')
适合我!上面的内容几乎是逐字逐句地从文档中提取出来的,这真是一种享受。
book.create_worksheet(:name => 'unique_name')
works for me!The above, lifted pretty much verbatim from the documentation works a treat.
我发现您可以使用相同的变量创建许多工作表,您只需更改其上的名称即可。例如:
articles.each do |art|
sheet1 = book.create_worksheet :name =>;艺术代码
结尾
I found out that you can create many sheets with the same variable, you just have to change the name you put on it. For example:
articles.each do |art|
sheet1 = book.create_worksheet :name => art.code
end