我每天下载(使用mechanize& when / sidekiq + redis).xls文件包含两个工作表,想要将它们分成两个文件,每个文件都有一个工作表。我已经尝试了很多方法无济于事(在一个随机的delete
方法中抛出希望可行的方法,nope):
# 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
开始考虑“电子表格宝石处理工作簿,如数组 - 我可以使用数组方法吗?”......然后举手。
主电子表格 - 宝石资源:
答案 0 :(得分:0)
使用roo
与roo-xls
宝石(而不仅仅是spreadsheet
)有效。我开始使用.xls文件,无法改变这一事实。神奇的是Roo的default_sheet
和简单的first
和last
方法(因为.xls文件转换为.csv,只接受保存单个工作表)。我将此代码放在更广泛的.rb文件中,我也有require 'spreadsheet'
和require 'csv'
(不确定下面是否需要它们):
# in this example, there are only two sheets in the .xls file
class SomeClassName
require 'roo'
require 'roo-xls'
def split_file
# relative path to file
file_path = File.join(Rails.root, "lib", "assets", "downloaded_file.xls")
# retrieve original xls and save first sheet as .csv
oldxls = Roo::Spreadsheet.open(file_path)
oldxls.default_sheet = oldxls.sheets.first
oldxls.to_csv("#{file_path}/new_file_name1.csv")
# retrieve original xls and save second sheet as .csv
oldxls = Roo::Spreadsheet.open(file_path)
oldxls.default_sheet = oldxls.sheets.last
oldxls.to_csv("#{file_path}/new_file_name2.csv")
# delete original .xls
File.delete("#{file_path}/downloaded_file.xls")
end
end
SomeClassName.split_file