这是我当前的工作代码:
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
隐藏在某个地方的属性吗?