Created
December 13, 2019 16:57
-
-
Save abdul-shajin/7b4eb9f5addab1224bc4e774e09f0b79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'roo' | |
require 'fileutils' | |
require 'csv' | |
require 'vcardio' | |
xlsx = Roo::Excelx.new('./cnt.xlsx') | |
nothing = [] | |
@vcf_elements = [{key: :food, values: [], regex: /FOOD/i}, | |
{key: :cold_store, values: [], regex: /COLD/i}, | |
{key: :resturants, values: [], regex: /REST|KITCHEN|COFFEE|SANDWICH|SUBWAY|PASTRIES|PIZZA|SHAWARMA|GRILLS|MACDONALD|GRILL|FALAFEL|SANDWICHES|DAIRY|CHICKEN|BURGER/i}, | |
{key: :sweets, values: [], regex: /SWEET/i}, | |
{key: :markets, values: [], regex: /MARKET/i}, | |
{key: :cafeterias, values: [], regex: /CAFE|LOUNGE/i}, | |
{key: :trading, values: [], regex: /TRAD/i}, | |
{key: :groceries, values: [], regex: /GROCERY|VEGETABLE|FRUITS/}] | |
xlsx.each_row_streaming do |row| | |
match = false | |
@vcf_elements.each do |vcf_elem| | |
if row[1].to_s =~ vcf_elem[:regex] | |
match = true | |
vcf_elem[:values] << row.to_a.map(&:to_s) | |
end | |
end | |
nothing << row.to_a.map(&:to_s).join(',') unless match | |
end | |
grouped_file = File.open('grouped_file.txt', 'w') | |
FileUtils.mkdir_p './excel_files' | |
FileUtils.mkdir_p './vcards' | |
@vcf_elements.each do |vcf_elem| | |
vcf_file = File.open("./excel_files/#{vcf_elem[:key]}.csv",'w') | |
vcf_file.close | |
FileUtils.mkdir_p "./vcards/#{vcf_elem[:key]}" | |
CSV.open("./excel_files/#{vcf_elem[:key]}.csv", "w") do |csv| | |
vcf_elem[:values].each do |row| | |
csv << row | |
vcard = VCardio::VCard.new('3.0') do | |
name = row[0] | |
address = row[3] | |
contact_name = row[4] | |
mobile_no = row[5] | |
fn row[1] | |
org row[4] | |
tel row[5] | |
end | |
time = Time.now.to_i | |
vcard.to_file("./vcards/#{vcf_elem[:key]}/#{row[4].to_s.gsub(/[^0-9A-Za-z]/, '')}-#{time}.vcf") | |
end | |
end | |
grouped_file.puts("-----#{vcf_elem[:key].upcase}-------") | |
vcf_elem[:values].each {|elem| grouped_file.puts(elem) } | |
grouped_file.puts("-----#{vcf_elem[:key].upcase} END-------") | |
end | |
grouped_file.close | |
onnilum_pedatha = File.open('onnilum_pedatha.txt', 'w') | |
nothing.each {|noth| onnilum_pedatha.puts(noth) } | |
onnilum_pedatha.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment