Bug 159967 - Support importing semicolon separated csv file to address book. r=benc

Differential Revision: https://phabricator.services.mozilla.com/D141060

--HG--
extra : amend_source : 501b5143b8de76d367da6174b77ea46090e9751a
This commit is contained in:
Ping Chen 2022-03-22 11:59:20 +02:00
Родитель f21f8810e2
Коммит a3243ae9e8
6 изменённых файлов: 41 добавлений и 5 удалений

Просмотреть файл

@ -98,8 +98,13 @@ class AddrBookFileImporter {
let csvRows = d3.csv.parseRows(content);
let tsvRows = d3.tsv.parseRows(content);
let dsvRows = d3.dsv(";").parseRows(content);
// If we have more CSV columns, then it's a CSV file, otherwise a TSV file.
this._csvRows = csvRows[0].length > tsvRows[0].length ? csvRows : tsvRows;
// See if it's semicolon separated.
if (this._csvRows[0].length < dsvRows[0].length) {
this._csvRows = dsvRows;
}
let bundle = Services.strings.createBundle(
"chrome://messenger/locale/importMsgs.properties"

Просмотреть файл

@ -95,7 +95,13 @@
"DisplayName" : "John Doe",
"FirstName" : "John",
"LastName" : "Doe",
"PrimaryEmail" : "johndoe@host.invalid"
"PrimaryEmail" : "john@doe.invalid"
},
{
"DisplayName" : "Jane Doe",
"FirstName" : "Jane",
"LastName" : "Doe",
"PrimaryEmail" : "jane@doe.invalid"
}
],
"becky_addressbook" :

Просмотреть файл

@ -1,2 +1,3 @@
First Name,Last Name,Display Name,Nickname,Primary Email,Secondary Email,Work Phone,Home Phone,Fax Number,Pager Number,Mobile Number,Home Address,Home Address 2,Home City,Home State,Home ZipCode,Home Country,Work Address,Work Address 2,Work City,Work State,Work ZipCode,Work Country,Job Title,Department,Organization,Web Page 1,Web Page 2,Birth Year,Birth Month,Birth Day,Custom 1,Custom 2,Custom 3,Custom 4,Notes,Screen Name,
John,Doe,John Doe,,johndoe@host.invalid,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
First Name,Last Name,Display Name,Nickname,Primary Email,Secondary Email,Work Phone,Home Phone,Fax Number,Pager Number,Mobile Number,Home Address,Home Address 2,Home City,Home State,Home ZipCode,Home Country,Work Address,Work Address 2,Work City,Work State,Work ZipCode,Work Country,Job Title,Department,Organization,Web Page 1,Web Page 2,Birth Year,Birth Month,Birth Day,Custom 1,Custom 2,Custom 3,Custom 4,Notes,Screen Name
John,Doe,John Doe,,john@doe.invalid,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Jane,Doe,Jane Doe,,jane@doe.invalid,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

1 First Name First Name,Last Name,Display Name,Nickname,Primary Email,Secondary Email,Work Phone,Home Phone,Fax Number,Pager Number,Mobile Number,Home Address,Home Address 2,Home City,Home State,Home ZipCode,Home Country,Work Address,Work Address 2,Work City,Work State,Work ZipCode,Work Country,Job Title,Department,Organization,Web Page 1,Web Page 2,Birth Year,Birth Month,Birth Day,Custom 1,Custom 2,Custom 3,Custom 4,Notes,Screen Name Last Name Display Name Nickname Primary Email Secondary Email Work Phone Home Phone Fax Number Pager Number Mobile Number Home Address Home Address 2 Home City Home State Home ZipCode Home Country Work Address Work Address 2 Work City Work State Work ZipCode Work Country Job Title Department Organization Web Page 1 Web Page 2 Birth Year Birth Month Birth Day Custom 1 Custom 2 Custom 3 Custom 4 Notes Screen Name
2 John John,Doe,John Doe,,john@doe.invalid,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Doe John Doe johndoe@host.invalid
3 Jane,Doe,Jane Doe,,jane@doe.invalid,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Просмотреть файл

@ -0,0 +1,2 @@
John Doe,John,Doe,john@doe.invalid
Jane Doe,Jane,Doe,jane@doe.invalid
1 John Doe John Doe john@doe.invalid
2 Jane Doe Jane Doe jane@doe.invalid

Просмотреть файл

@ -0,0 +1,3 @@
Display Name;First Name;Last Name;Primary Email
John Doe;John;Doe;john@doe.invalid
Jane Doe;Jane;Doe;jane@doe.invalid
1 Display Name First Name Last Name Primary Email
2 John Doe John Doe john@doe.invalid
3 Jane Doe Jane Doe jane@doe.invalid

Просмотреть файл

@ -12,8 +12,9 @@ var { AddrBookFileImporter } = ChromeUtils.import(
* @param {string} type - A source file type supported by AddrBookFileImporter.
* @param {string} filePath - The path of a source file.
* @param {string} refDataKey - The key of an object in addressbook.json.
* @param {string[]} [csvFieldMap] - Map of CSV fields to address book fields.
*/
async function test_importAbFile(type, filePath, refDataKey) {
async function test_importAbFile(type, filePath, refDataKey, csvFieldMap) {
// Create an address book and init the importer.
let dirId = MailServices.ab.newAddressBook(
`tmp-${type}`,
@ -26,7 +27,10 @@ async function test_importAbFile(type, filePath, refDataKey) {
// Start importing.
let sourceFile = do_get_file(filePath);
if (type == "csv") {
await importer.parseCsvFile(sourceFile);
let unmatched = await importer.parseCsvFile(sourceFile);
if (unmatched.length) {
importer.setCsvFields(csvFieldMap);
}
}
await importer.startImport(sourceFile, targetDir);
@ -48,12 +52,27 @@ async function test_importAbFile(type, filePath, refDataKey) {
/** Test importing .csv file works. */
add_task(async function test_importCsvFile() {
// A comma separated file.
await test_importAbFile(
"csv",
"resources/basic_csv_addressbook.csv",
"csv_import"
);
// A semicolon separated file.
await test_importAbFile("csv", "resources/csv_semicolon.csv", "csv_import");
// A comma separated file without header row.
Services.prefs.setBoolPref("mail.import.csv.skipfirstrow", false);
await test_importAbFile("csv", "resources/csv_no_header.csv", "csv_import", [
2, // DisplayName
0, // FirstName
1, // LastName
4, // PrimaryEmail
]);
Services.prefs.clearUserPref("mail.import.csv.skipfirstrow");
// A comma separated file with some fields containing quotes.
await test_importAbFile("csv", "resources/quote.csv", "quote_csv");
});