Created
October 5, 2019 12:03
-
-
Save AaronC81/35f79105a42a5f29d1c402745192a3e1 to your computer and use it in GitHub Desktop.
Convert a CSV of email addresses into Google Contacts' import format
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'csv' | |
# This will convert a CSV with a column of email addresses into a valid Google | |
# Contacts CSV which can be imported. | |
# Usage: ./converter.rb <input> <output> | |
# The constants below can be used to configure this script. | |
# The column of the input CSV, starting from zero, which contains email | |
# addresses. | |
INPUT_CSV_EMAIL_COLUMN = 1 | |
# Whether the input CSV has headers. If this is true, the first line of input is | |
# skipped. | |
INPUT_CSV_HAS_HEADERS = true | |
# ----------------------- | |
def die(msg) | |
puts msg | |
exit 1 | |
end | |
if ARGV.length != 2 | |
die 'Usage: ./converter.rb <input> <output>' | |
end | |
INPUT_FILE, OUTPUT_FILE = ARGV | |
# Read the input file and extract the email addresses | |
input_csv = CSV.new(File.read(INPUT_FILE)) | |
relevant_rows = INPUT_CSV_HAS_HEADERS ? input_csv.read[1..-1] : input_csv.read | |
email_addresses = relevant_rows.map do |row| | |
row[INPUT_CSV_EMAIL_COLUMN] or die "Missing column value on row: #{row}" | |
end | |
# Generate the output CSV | |
# We'll use the email address as the contact name | |
output_csv = CSV.generate do |csv| | |
# This is a lot of fields, and the Google Contacts CSV needs all of them! | |
csv << ["Name", "Given Name", "Additional Name", "Family Name", "Yomi Name", "Given Name Yomi", "Additional Name Yomi", "Family Name Yomi", "Name Prefix", "Name Suffix", "Initials", "Nickname", "Short Name", "Maiden Name", "Birthday", "Gender", "Location", "Billing Information", "Directory Server", "Mileage", "Occupation", "Hobby", "Sensitivity", "Priority", "Subject", "Notes", "Language", "Photo", "Group Membership", "E-mail 1 - Type", "E-mail 1 - Value"] | |
email_addresses.each do |email_address| | |
# Fields: name group type email | |
csv << [email_address, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "* myContacts", "* ", email_address] | |
end | |
end | |
# Write output | |
File.write(OUTPUT_FILE, output_csv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment