Skip to content

Instantly share code, notes, and snippets.

@ppulwey
Created January 19, 2023 14:16
Show Gist options
  • Save ppulwey/79e1a592a310b1bb5f759f30442f9454 to your computer and use it in GitHub Desktop.
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
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!');
@ppulwey
Copy link
Author

ppulwey commented Jan 19, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment