Skip to content

Commit

Permalink
re-add sections to excel exports. empty files export both sections an…
Browse files Browse the repository at this point in the history
…d slot headers.
  • Loading branch information
kennethbruskiewicz committed Aug 22, 2024
1 parent 43d4791 commit 429af4a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
16 changes: 13 additions & 3 deletions lib/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
createWorkbookFromJSON,
exportWorkbook,
importJsonFile,
prependRowWithValuesAcrossSheets
prependToSheet,
} from '@/lib/utils/files';
import { nullValuesToString, isEmptyUnitVal } from '@/lib/utils/general';
import { MULTIVALUED_DELIMITER, titleOverText } from '@/lib/utils/fields';
Expand Down Expand Up @@ -493,7 +493,7 @@ class Toolbar {
})
.join(MULTIVALUED_DELIMITER);
} else {
if (!(v in merged_permissible_values)) console.warn(`${_v} not in merged_permissible_values ${Object.keys(merged_permissible_values)}`);
if (!(v in merged_permissible_values)) console.warn(`${v} not in merged_permissible_values ${Object.keys(merged_permissible_values)}`);
nv =
v in merged_permissible_values
? titleOverText(merged_permissible_values[v])
Expand Down Expand Up @@ -536,9 +536,19 @@ class Toolbar {
},
{}
);
const columnCoordinatesByClass = Object.values(this.context.dhs).reduce(
(acc, dh) => {
const columnIndexCoordinates = dh.getFields().reduce((acc, field, i) => Object.assign(acc, { [field.name]: i }), {});
return Object.assign(acc, { [rangeToContainerClass(schema_container, dh.class_assignment)]: invert(columnIndexCoordinates) });
}, {});

let MultiEntityWorkbook = createWorkbookFromJSON(JSONFormat.Container);
MultiEntityWorkbook = prependRowWithValuesAcrossSheets(MultiEntityWorkbook, sectionCoordinatesByClass);
MultiEntityWorkbook.SheetNames.forEach(sheetName => {
if (!(JSONFormat.Container[sheetName].length > 0)) {
prependToSheet(MultiEntityWorkbook, sheetName, columnCoordinatesByClass[sheetName]);
}
prependToSheet(MultiEntityWorkbook, sheetName, sectionCoordinatesByClass[sheetName]);
});

await this.context.runBehindLoadingScreen(exportWorkbook, [
MultiEntityWorkbook,
Expand Down
56 changes: 30 additions & 26 deletions lib/utils/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export function exportWorkbook(workbook, baseName, ext) {
baseName,
'csv',
',',
'text/plain;charset=UTF-8'
'text/plain;charset=UTF-8',
);
break;

Expand All @@ -179,7 +179,7 @@ export function exportWorkbook(workbook, baseName, ext) {
baseName,
'csv',
',',
'text/plain;charset=UTF-16LE'
'text/plain;charset=UTF-16LE',
);
break;

Expand All @@ -189,7 +189,7 @@ export function exportWorkbook(workbook, baseName, ext) {
baseName,
'tsv',
'\t',
'text/plain;charset=UTF-8'
'text/plain;charset=UTF-8',
);
break;

Expand All @@ -199,7 +199,7 @@ export function exportWorkbook(workbook, baseName, ext) {
baseName,
'tsv',
'\t',
'text/plain;charset=UTF-16LE'
'text/plain;charset=UTF-16LE',
);
break;

Expand Down Expand Up @@ -241,6 +241,8 @@ function processAndSave(

const worksheet = workbook.Sheets[sheetName];
const sheetData = XlsxUtils.sheet_to_json(worksheet, { header: 1 });

console.log(worksheet, sheetData);

const formattedData = sheetData
.map((row) => row.join(delimiter))
Expand Down Expand Up @@ -331,42 +333,44 @@ export function importJsonFile(jsonData) {
// Main function to handle the entire process of prepending row to sheets
// Useful for custom binning headers like sections
// NOTE: in-place function!
function prependRowForSheet(workbook, sheetName, valueCellPairs) {
// Function to read a worksheet and convert it to a 2D array
export const modifySheetRow = (workbook, sheetName, valueCellPairs, targetRow) => {

// Utility function to read a worksheet and convert it to a 2D array
const getSheetData = (workbook, sheetName) => {
const worksheet = workbook.Sheets[sheetName];
return XlsxUtils.sheet_to_json(worksheet, { header: 1 });
}
};

// Function to prepend an empty row to the data array
const prependEmptyRow = (data) => {
data.unshift([]);
}
// Utility function to update the worksheet in the workbook
const updateWorksheet = (workbook, sheetName, data) => {
const newWorksheet = XlsxUtils.aoa_to_sheet(data);
workbook.Sheets[sheetName] = newWorksheet;
};

// Function to populate or prepend a row in a worksheet
const populateOrPrependRow = (data, valueCellPairs, targetRow) => {
if (targetRow === undefined || targetRow < 0 || targetRow >= data.length) {
data.unshift([]);
targetRow = 0;
}

// Function to insert values into the top row based on cell-value pairs
function populateTopRow(data, valueCellPairs) {
for (const [colIndex, value] of Object.entries(valueCellPairs)) {
data[0][Number(colIndex)] = value; // Assume zero-based index
data[targetRow][Number(colIndex)] = value;
}
}
};

// Function to convert the 2D array back to a worksheet and update the workbook
const updateWorksheet = (workbook, sheetName, data) => {
const newWorksheet = XlsxUtils.aoa_to_sheet(data);
workbook.Sheets[sheetName] = newWorksheet;
}
const data = getSheetData(workbook, sheetName);
populateOrPrependRow(data, valueCellPairs, targetRow);
updateWorksheet(workbook, sheetName, data);
};

let data = getSheetData(workbook, sheetName); // Get the data array
prependEmptyRow(data); // Prepend an empty row
populateTopRow(data, valueCellPairs); // Populate the top row with values
updateWorksheet(workbook, sheetName, data); // Update the worksheet in the workbook
}
export const prependToSheet = (workbook, sheetName, valueCellPairs) => modifySheetRow(workbook, sheetName, valueCellPairs);

// Main function to handle the entire process across all sheets
export function prependRowWithValuesAcrossSheets(workbook, valueCellPairs) {
const newWorkbook = structuredClone(workbook);
newWorkbook.SheetNames.forEach(sheetName => {
prependRowForSheet(newWorkbook, sheetName, valueCellPairs[sheetName]);
prependToSheet(newWorkbook, sheetName, valueCellPairs[sheetName]);
});
return newWorkbook;
}

0 comments on commit 429af4a

Please sign in to comment.