How can I create new spreadsheet worksheets in Ruby using the Spreadsheet gem?

后端 未结 2 1691
北荒
北荒 2021-01-24 17:17

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 ov

相关标签:
2条回答
  • 2021-01-24 17:49

    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

    0 讨论(0)
  • 2021-01-24 17:53

    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.

    0 讨论(0)
提交回复
热议问题