diff --git a/src/utils/xls.js b/src/utils/xls.js index 83c11c20..4ab8ce05 100644 --- a/src/utils/xls.js +++ b/src/utils/xls.js @@ -54,12 +54,22 @@ export const createXlsFromSequelizeResults = (rows, model, hex = false, csv = fa let mainXlsRow = []; // Populate main sheet values - for (const [mainColName, mainCol] of columnsInMainSheet.entries()) { - if (!associations.map(singular => singular + 's').includes(mainColName)) { - if (row[mainCol] === null) { - row[mainCol] = 'null'; + for (const [i, mainColName] of columnsInMainSheet.entries()) { + if (row[mainColName] === null) { + row[mainColName] = 'null'; + } + + if (Object.keys(row).includes(mainColName) && Object.keys(row[mainColName]).includes('id')) { + if (!Object.keys(sheets).includes(mainColName + 's')) { + sheets[mainColName + 's'] = { name: mainColName + 's', data: [Object.keys(row[mainColName]).concat([model.name.split('_').join('') + 'Id'])], }; + } + sheets[mainColName + 's'].data.push(Object.values(row[mainColName]).map(val1 => encodeValue(val1, hex)).concat([encodeValue(row[mainColName].id, hex)])); + } + if (!associations.map(singular => singular + 's').includes(i)) { // Todo: change to colNames[i], but also filter column headings first (for skipping assoc cols) + if (row[mainColName] === null) { + row[mainColName] = 'null'; } - mainXlsRow.push(encodeValue(row[mainCol], hex)); + mainXlsRow.push(encodeValue(row[mainColName], hex)); } }