1

这是我当前的工作代码:

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隐藏在某个地方的属性吗?

4

1 回答 1

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
于 2013-10-17T10:26:52.030 回答