如何使用 Spreadsheet gem 在 Ruby 中创建新的电子表格工作表?

发布于 2024-09-19 13:08:15 字数 745 浏览 12 评论 0原文

具体来说,我想做的是在已有的工作表旁边添加新的工作表。我尝试使用 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 ruby​​forge 页面,其中有公共类方法 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如歌彻婉言 2024-09-26 13:08:16

book.create_worksheet(:name => 'unique_name') 适合我!

book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet(:name => 'AAA')
sheet2 = book.create_worksheet(:name => 'BBB')


sheet1.row(0).concat %w{Name Country Acknowlegement}
sheet1[1,0] = 'Japan'
row = sheet1.row(1)
row.push 'Creator of Ruby'
row.unshift 'Yukihiro Matsumoto'
sheet1.row(2).replace [ 'Daniel J. Berger', 'U.S.A.',
                  'Author of original code for Spreadsheet::Excel' ]
sheet1.row(3).push 'Charles Lowe', 'Author of the ruby-ole Library'
sheet1.row(3).insert 1, 'Unknown'
sheet1.update_row 4, 'Hannes Wyss', 'Switzerland', 'Author'

sheet2.row(0).concat %w{NAME COUNYRY ACK}
sheet2[1,0] = 'JAPAN'
row = sheet2.row(1)
row.push 'CREATOR OF RUBY'
row.unshift 'YUKIHIRO MATSUMOTO'
sheet2.row(2).replace [ 'DANIEL J. BERGER', 'U.S.A.',
                  'AUTHOR OF ORIGINAL CODE FOR  Spreadsheet::Excel' ]
sheet2.row(3).push 'CHARLES LOWE', 'AUTHOR OF THE RUBY-OLE LIBRARY'
sheet2.row(3).insert 1, 'UNKNOWN'
sheet2.update_row 4, 'HANNES WYSS', 'SWITZERLAND', 'AUTHOR'

book.write '/Users/stephen/tmp/test2.xls'

上面的内容几乎是逐字逐句地从文档中提取出来的,这真是一种享受。

book.create_worksheet(:name => 'unique_name') works for me!

book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet(:name => 'AAA')
sheet2 = book.create_worksheet(:name => 'BBB')


sheet1.row(0).concat %w{Name Country Acknowlegement}
sheet1[1,0] = 'Japan'
row = sheet1.row(1)
row.push 'Creator of Ruby'
row.unshift 'Yukihiro Matsumoto'
sheet1.row(2).replace [ 'Daniel J. Berger', 'U.S.A.',
                  'Author of original code for Spreadsheet::Excel' ]
sheet1.row(3).push 'Charles Lowe', 'Author of the ruby-ole Library'
sheet1.row(3).insert 1, 'Unknown'
sheet1.update_row 4, 'Hannes Wyss', 'Switzerland', 'Author'

sheet2.row(0).concat %w{NAME COUNYRY ACK}
sheet2[1,0] = 'JAPAN'
row = sheet2.row(1)
row.push 'CREATOR OF RUBY'
row.unshift 'YUKIHIRO MATSUMOTO'
sheet2.row(2).replace [ 'DANIEL J. BERGER', 'U.S.A.',
                  'AUTHOR OF ORIGINAL CODE FOR  Spreadsheet::Excel' ]
sheet2.row(3).push 'CHARLES LOWE', 'AUTHOR OF THE RUBY-OLE LIBRARY'
sheet2.row(3).insert 1, 'UNKNOWN'
sheet2.update_row 4, 'HANNES WYSS', 'SWITZERLAND', 'AUTHOR'

book.write '/Users/stephen/tmp/test2.xls'

The above, lifted pretty much verbatim from the documentation works a treat.

油焖大侠 2024-09-26 13:08:16

我发现您可以使用相同的变量创建许多工作表,您只需更改其上的名称即可。例如:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文