Skip to content

Commit

Permalink
Merge pull request #1097 from microbiomedata/issue-1047
Browse files Browse the repository at this point in the history
Fix XLSX import when columns are reordered or missing
  • Loading branch information
pkalita-lbl authored Dec 12, 2023
2 parents a2f719d + 50ca682 commit 910c92d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions web/src/views/SubmissionPortal/HarmonizerView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,19 @@ export default defineComponent({
if (!template || !template.sampleDataSlot || !template.schemaClass) {
return;
}
// The spreadsheet has slot names as the header row. So `sheet_to_json` will produce array
// of objects with slot names as keys. But we want the imported data to be keyed on slot
// IDs. This code reads the worksheet data and remaps the keys from slot names to IDs.
const slotIdToNameMap = harmonizerApi.getHeaderRow(template.schemaClass);
const slotNameToIdMap = Object.fromEntries(Object.entries(slotIdToNameMap).map(([k, v]) => [v, k]));
const worksheetData: Record<string, string>[] = utils.sheet_to_json(worksheet);
const remappedData = worksheetData.map((row) => Object.fromEntries(Object.entries(row)
.filter(([slotName]) => slotNameToIdMap[slotName] !== undefined)
.map(([slotName, value]) => [slotNameToIdMap[slotName], value])));
imported[template.sampleDataSlot] = harmonizerApi.unflattenArrayValues(
utils.sheet_to_json(worksheet, {
header: harmonizerApi.getOrderedAttributeNames(template.schemaClass),
range: 1,
}),
remappedData,
template.schemaClass,
);
});
Expand Down

0 comments on commit 910c92d

Please sign in to comment.