开发者社区> 问答> 正文

如何用 ruby 从 excel 中抽取 email?

如何用 ruby 从 excel 中抽取 email?

展开
收起
OSC开源社区 2024-06-12 16:53:18 24 0
1 条回答
写回答
取消 提交回答
  • 可用如下代码解决:

    $KCODE = 'u'
    require 'find'
    require 'win32ole'
    require 'pathname'
    
    class AutoFixMailPros
      def initialize
        @excel = WIN32OLE.new('Excel.Application')
        @excel.visible = false
        @excel.Application.DisplayAlerts = false
      end
    
      def run_excel(directory = 'D:/excelrun/temp')
        @excel_files = find_excel_files(directory)
        return unless @excel_files.any?
    
        @excel_files.each do |file_path|
          process_excel_file(file_path)
          rename_processed_file(file_path)
        end
    
        @excel.Quit
        @excel = nil
        GC.start
      end
    
      private
    
      def find_excel_files(dir)
        files = []
        Find.find(dir) do |path|
          next if File.basename(path).start_with?('@$$')
          files << path if File.extname(path).casecmp('.xls') == 0
        end
        files
      end
    
      def process_excel_file(file_path)
        workbook = @excel.Workbooks.Open(file_path)
        sheet_count = workbook.Sheets.Count
    
        (1..sheet_count).each do |sheet_num|
          extract_emails_to_file(workbook, sheet_num, file_path)
        end
    
        workbook.Close(true)
      end
    
      def extract_emails_to_file(workbook, sheet_num, base_path)
        worksheet = workbook.Worksheets(sheet_num)
        rows = worksheet.UsedRange.Rows.Count
        cols = worksheet.UsedRange.Columns.Count
    
        emails = find_emails_in_sheet(worksheet, rows, cols)
        save_emails_to_txt(base_path, emails, sheet_num) if emails.any?
      end
    
      def find_emails_in_sheet(worksheet, rows, cols)
        emails = []
        (1..rows).each do |row|
          (1..cols).each do |col|
            value = worksheet.Cells(row, col).Value.to_s
            emails << value if value.match?(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)/)
          end
        end
        emails
      end
    
      def save_emails_to_txt(base_path, emails, sheet_num)
        file_name = "#{File.basename(base_path, '.*')}_#{sheet_num}.txt"
        file_path = Pathname.new(File.dirname(base_path)).realpath + file_name
        File.open(file_path, 'w') { |file| emails.each { |email| file.puts(email) } }
      end
    
      def rename_processed_file(original_path)
        # Implement renaming logic if needed
        # new_name = "#{Pathname.new(original_path).dirname}(#{File.basename(original_path, '.*')}@$$$.xls)"
        # File.rename(original_path, new_name)
      end
    end
    
    mailProsTools = AutoFixMailPros.new
    mailProsTools.run_excel
    
    2024-06-13 17:16:59
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载