这是我目前的工作代码:
require 'csv'
require 'spreadsheet'
folder_to_analyze = ARGV.first
folder_path = File.join(Dir.pwd, folder_to_analyze)
unless File.directory?(folder_path)
puts "Error: #{folder_path} no es un folder valido."
exit
end
def get_csv_file_paths(path)
Dir.glob(path + '/**/*.csv').each do |f|
yield f
end
end
def get_xls_file_path(path)
Dir.glob(path + '/**/*.xls').each do |f|
yield f
end
end
csv_files = []
excel_files = []
get_csv_file_paths(folder_path) { |f| csv_files << f }
get_xls_file_path(folder_path) { |f| excel_files << f }
puts "Se encontro #{csv_files.length + excel_files.length} archivos para procesar."
puts '==========================================='
puts 'Archivos CSV:'
puts '==========================================='
csv_files.each do |f|
count = IO.readlines(f).size
puts "Archivo: #{File.basename(f)} - Correos: #{count}"
end
puts '==========================================='
puts 'Archivos Excel:'
puts '==========================================='
Spreadsheet.client_encoding = 'UTF-8'
excel_files.each do |f|
count = 0
book = Spreadsheet.open f
book.worksheets.each do |sheet|
sheet.each do |row|
count = count + 1
end
end
puts "Archivo: #{File.basename(f)} - Correos: #{count}"
end
电子表格行数计算非常慢,每个excel文件大约需要4秒才能计算。
有没有办法加快速度?是否隐藏了row_count
属性?
答案 0 :(得分:1)
没有必要迭代工作表的行,只需在每张工作表上调用 count ,如下所示:
excel_files.each do |f|
count = 0
book = Spreadsheet.open f
book.worksheets.each do |sheet|
count += sheet.count
end
puts "Archivo: #{File.basename(f)} - Correos: #{count}"
end