Skip to content

Commit

Permalink
Make SessionPerformMappingJob understand merged cells
Browse files Browse the repository at this point in the history
Also remembered to commit the merged cells test fixture, and made
it a little more interesting (and updated tests to match)
  • Loading branch information
alaricsp committed Sep 10, 2024
1 parent 421c344 commit 5d350b8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
Binary file added fixtures/merged-cells.xlsx
Binary file not shown.
8 changes: 5 additions & 3 deletions lib/importer/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,13 @@ exports.SessionPerformMappingJob = (sid, range, mapping) => {
let errors = new Map(); // Maps input row number to list of errors
let warnings = new Map(); // Maps input row number to list of warnings

let data = sessionStore.get(sid).wb.Sheets[range.sheet]["!data"];
let attrMap = Object.entries(mapping.attributeMappings);
const sheet = sessionStore.get(sid).wb.Sheets[range.sheet];
const data = sheet["!data"];
const merges = sheet["!merges"];
const attrMap = Object.entries(mapping.attributeMappings);

for(let rowIdx=range.start.row; rowIdx <= range.end.row; rowIdx++) {
let row = data[rowIdx];
let row = getMergedRow(data, merges, rowIdx);
let record = {};
let foundSomeValues = false;
let rowWarnings = [];
Expand Down
49 changes: 44 additions & 5 deletions lib/importer/backend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ test('merged cells', () => {
const samples = backend.SessionGetInputSampleRows(sid, dataRange,
1, 1, 1);
expect(samples).toMatchObject([
[ [ 'Boris', undefined, '13', 'High' ] ],
[ [ 'Boris', undefined, '13',
// This cell is merged with the next row
{merge: {column: 0, columns: 1, row: 0, rows: 2}, value:"High"}] ],
[ [ 'Nelly', // These two cells are merged
{merge: {column: 0, columns: 2, row: 0, rows: 1}, value: '14'},
{merge: {column: 1, columns: 2, row: 0, rows: 1}, value: '14'},
'High' ] ],
// This cell is merged with the previous row
{merge: {column: 0, columns: 1, row: 1, rows: 2}, value:"High"} ] ],
[ [ 'Sid', undefined, '10', 'Medium' ] ]
]);

Expand Down Expand Up @@ -129,13 +132,15 @@ test('merged cells', () => {
"Boris",
undefined,
"13",
"High"],
{merge: {column: 0, columns: 1, row: 0, rows: 2}, value:"High"}
],
[{merge: {column: 0, columns: 2, row: 1, rows:2}, value:"Here are some random notes left by the user"},
{merge: {column: 1, columns: 2, row: 1, rows:2}, value:"Here are some random notes left by the user"},
"Nelly",
{merge: {column: 0, columns: 2, row: 0, rows: 1}, value: '14'},
{merge: {column: 1, columns: 2, row: 0, rows: 1}, value: '14'},
"High"],
{merge: {column: 0, columns: 1, row: 1, rows: 2}, value:"High"}
],
[{merge: {column: 0, columns: 1, row: 0, rows:2}, value:"And another note"},
undefined,
"Sid",
Expand All @@ -156,7 +161,41 @@ test('merged cells', () => {
{ inputValues: [ 'Boris', 'Nelly' ], hasMore: true },
{ inputValues: [ '14', undefined ], hasMore: false }, // The value 14 is merged across both these columns
{ inputValues: [ '13', '14' ], hasMore: true },
{ inputValues: [ 'High', 'Medium' ], hasMore: false }
{ inputValues: [ 'High', 'Medium' ], hasMore: false } // High egginess is merged between rows
]);

// Do a mapping

const mapping = {
attributeMappings: {
Name: 0,
Age: 2,
Egginess: 3
},
};

const jid = backend.SessionPerformMappingJob(sid, dataRange, mapping);

// Look at the job results

const jobSummary = backend.JobGetSummary(jid);
expect(jobSummary).toMatchObject({
recordCount: 3,
errorCount: 0,
warningCount: 0
});

const warnings = backend.JobGetWarnings(jid);
expect(warnings).toMatchObject({});

const errors = backend.JobGetErrors(jid);
expect(errors).toMatchObject({});

const sampleRecords = backend.JobGetSampleRecords(jid, 1, 1, 1);
expect(sampleRecords).toMatchObject([
[ { Name: 'Boris', Age: 13, Egginess: 'High' } ],
[ { Name: 'Nelly', Age: 14, Egginess: 'High' } ], // The 14 is merged across from a cell in column 1
[ { Name: 'Sid', Age: 10, Egginess: 'Medium' } ]
]);

});
Expand Down

0 comments on commit 5d350b8

Please sign in to comment.