Created
January 19, 2023 14:16
-
-
Save ppulwey/79e1a592a310b1bb5f759f30442f9454 to your computer and use it in GitHub Desktop.
Create multiple vCards with Name, Address, Phone and Email from a CSV file
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
const fs = require('fs'); | |
const csvFile = fs.readFileSync('contacts.csv', 'utf8'); | |
const csvData = csvFile.split('\n').map(row => row.split(';')); | |
const vcardData = []; | |
for (let i = 1; i < csvData.length; i++) { | |
vcardData.push('BEGIN:VCARD'); | |
vcardData.push('VERSION:4.0'); | |
const contact = csvData[i]; | |
vcardData.push(`FN;CHARSET=UTF-8:${contact[1]} ${contact[0]}`); | |
vcardData.push(`N;CHARSET=UTF-8:${contact[0]};${contact[1]};;;`); | |
if (contact[2] && contact[3]) { | |
const street = contact[2].split(" "); | |
const location = contact[3].split(" "); | |
vcardData.push(`ADR;TYPE=HOME;CHARSET=UTF-8:;;${street[0]} ${street[1]};${location[1]};;${location[0]};Deutschland;`); | |
} | |
if (contact[4]) { | |
let cleanedMobile = contact[4].replace(/-/g, ""); | |
cleanedMobile = cleanedMobile.replace(/^0+/,''); | |
cleanedMobile = cleanedMobile.replace(/\s+/g, ""); | |
vcardData.push(`TEL;TYPE=CELL:+49${cleanedMobile}`); | |
} | |
if (contact[5]) { | |
let cleanedPhone = contact[5].replace(/-/g, ""); | |
cleanedPhone = cleanedPhone.replace(/^0+/,''); | |
cleanedPhone = cleanedPhone.replace(/\s+/g, ""); | |
vcardData.push(`TEL;TYPE=HOME:+49${cleanedPhone}`); | |
} | |
if (contact[6]) { | |
const lowerEmail = contact[6].toLowerCase(); | |
vcardData.push(`EMAIL;CHARSET=UTF-8:${lowerEmail}`); | |
} | |
vcardData.push('END:VCARD'); | |
} | |
fs.writeFileSync('contacts.vcf', vcardData.join('\n')); | |
console.log('vCard-Datei erfolgreich erstellt!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CSV ist separated with semicolon and is in the form: Lastname;Firstname;Street with Number;City;Mobile;Phone;Mail;
You can replace the Country in Line 18 and the phone country code in line 31 to any value you need.