我有一个每日下载的(使用 mechanize &when/sidekiq+redis).xls 文件,其中包含两个工作表,并希望将它们拆分为两个文件,每个文件都有一张。我尝试了很多方法都无济于事(以随机delete
方法折腾,希望能奏效,不):
# goal: start with 1 file w/2 sheets and split into 2 files with 1 sheet each
def split_file
open_xls = Spreadsheet.open 'my_file.xls'
open_xls.write 'destination1.xls'
open_xls.write 'destination2.xls'
File.delete('my_file.xls')
# open first new file and try to delete one sheet
open_xls1 = Spreadsheet.open 'destination1.xls'
sheet1 = open_xls1.worksheet(0)
open_xls1.sheet1.delete # from log: "NoMethodError: undefined method `delete'"
open_xls1.write 'destination_only_sheet2.xls'
# "" other sheet
# repeat on 2nd file to remove other sheet
end
开始思考“电子表格 gem 是否像数组一样对待工作簿——我可以使用数组方法吗?”...然后举手。
主要的电子表格宝石资源: