Skip to content

Instantly share code, notes, and snippets.

@davidworkman9
Created November 13, 2013 13:54
Show Gist options
  • Save davidworkman9/7449476 to your computer and use it in GitHub Desktop.
Save davidworkman9/7449476 to your computer and use it in GitHub Desktop.
Appends a file upload input to the screen. When you upload a csv file, it will create a user for each row in the csv file and give each one of them a uuid() for a password. By default it assumes the order of First Name, Last Name, Email, and Company but this is configurable in the first 4 lines of the gist. At the end it tells you the number of …
var EMAIL_INDEX = 2,
FIRSTNAME_INDEX = 0,
LASTNAME_INDEX = 1,
COMPANY_INDEX = 3;
$('#users').remove();
$('body').prepend('<input type="file" id="users" />');
$('#users').change(function () {
var csv = new FileReader();
csv.onload = function (e) {
var errs = [],
password = Meteor.uuid(),
rows = e.target.result.trim().split('\n'),
numDone = 0,
total = rows.length;
_.each(rows, function (r) {
var d = r.split(',');
(function () {
var newUser = {
email: d[EMAIL_INDEX],
password: password,
profile: {
firstName: d[FIRSTNAME_INDEX],
lastName: d[LASTNAME_INDEX],
company: d[COMPANY_INDEX]
}
};
Accounts.createUser(newUser, function (err) {
numDone++;
if(err) {
errs.push({ error: err, user: newUser });
}
if(numDone >= total) {
console.log('Users Created:', total - errs.length);
console.log('Password: ', password);
if (errs.length)
console.log('errors encountered: ', errs);
}
});
})();
});
};
csv.readAsText($(this)[0].files[0]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment