Skip to content

Commit

Permalink
feat: auto save report in directory in bulk mode (#104)
Browse files Browse the repository at this point in the history
* feat: auto save report in directory

* chore: missing entries
  • Loading branch information
kptdobe authored Feb 1, 2023
1 parent ac99072 commit 35f37c6
Showing 1 changed file with 61 additions and 44 deletions.
105 changes: 61 additions & 44 deletions js/import/import.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const BULK_URLS_LIST = document.querySelector('#import-result ul');
const IMPORT_FILE_PICKER_CONTAINER = document.getElementById('import-file-picker-container');

const DOWNLOAD_BINARY_TYPES = ['pdf'];
const REPORT_FILENAME = 'import-report.xlsx';

const ui = {};
const config = {};
Expand Down Expand Up @@ -193,7 +194,7 @@ const getProxyURLSetup = (url, origin) => {
};
};

const postImportProcess = async (results, originalURL) => {
const postSuccessfulStep = async (results, originalURL) => {
await asyncForEach(results, async ({
docx, filename, path, report,
}) => {
Expand Down Expand Up @@ -222,6 +223,59 @@ const postImportProcess = async (results, originalURL) => {
});
};

const autoSaveReport = () => dirHandle && IS_BULK;

const getReport = async () => {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Sheet 1');

const headers = ['URL', 'path', 'docx', 'status', 'redirect'].concat(importStatus.extraCols);

// create Excel auto Filters for the first row / header
worksheet.autoFilter = {
from: 'A1',
to: `${String.fromCharCode(65 + headers.length - 1)}1`, // 65 = 'A'...
};

worksheet.addRows([
headers,
].concat(importStatus.rows.map((row) => {
const {
url, path, docx, status, redirect, report,
} = row;
const extra = [];
if (report) {
importStatus.extraCols.forEach((col) => {
const e = report[col];
if (e) {
if (typeof e === 'string') {
if (e.startsWith('=')) {
extra.push({
formula: report[col].replace(/=/, '_xlfn.'),
value: '', // cannot compute a default value
});
} else {
extra.push(report[col]);
}
} else {
extra.push(JSON.stringify(report[col]));
}
}
});
}
return [url, path, docx || '', status, redirect || ''].concat(extra);
})));

return workbook.xlsx.writeBuffer();
};

const postImportStep = async () => {
if (autoSaveReport()) {
// save report file in the folder
await saveFile(dirHandle, REPORT_FILENAME, await getReport());
}
};

const createImporter = () => {
config.importer = new PollImporter({
origin: config.origin,
Expand Down Expand Up @@ -259,12 +313,13 @@ const attachListeners = () => {
const { originalURL } = frame.dataset;

updateImporterUI(results, originalURL);
postImportProcess(results, originalURL);
await postSuccessfulStep(results, originalURL);
await postImportStep();

alert.success(`Import of page ${originalURL} completed.`);
});

config.importer.addErrorListener(({ url, error: err, params }) => {
config.importer.addErrorListener(async ({ url, error: err, params }) => {
const frame = getContentFrame();
const { originalURL } = frame.dataset;

Expand All @@ -278,6 +333,7 @@ const attachListeners = () => {
});

updateImporterUI([{ status: 'error' }], originalURL);
await postImportStep();
});

IMPORT_BUTTON.addEventListener('click', (async () => {
Expand Down Expand Up @@ -439,50 +495,11 @@ const attachListeners = () => {
});

DOWNLOAD_IMPORT_REPORT_BUTTON.addEventListener('click', (async () => {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Sheet 1');

const headers = ['URL', 'path', 'docx', 'status', 'redirect'].concat(importStatus.extraCols);

// create Excel auto Filters for the first row / header
worksheet.autoFilter = {
from: 'A1',
to: `${String.fromCharCode(65 + headers.length - 1)}1`, // 65 = 'A'...
};

worksheet.addRows([
headers,
].concat(importStatus.rows.map((row) => {
const {
url, path, docx, status, redirect, report,
} = row;
const extra = [];
if (report) {
importStatus.extraCols.forEach((col) => {
const e = report[col];
if (e) {
if (typeof e === 'string') {
if (e.startsWith('=')) {
extra.push({
formula: report[col].replace(/=/, '_xlfn.'),
value: '', // cannot compute a default value
});
} else {
extra.push(report[col]);
}
} else {
extra.push(JSON.stringify(report[col]));
}
}
});
}
return [url, path, docx || '', status, redirect || ''].concat(extra);
})));
const buffer = await workbook.xlsx.writeBuffer();
const buffer = await getReport();
const a = document.createElement('a');
const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
a.setAttribute('href', URL.createObjectURL(blob));
a.setAttribute('download', 'import_report.xlsx');
a.setAttribute('download', REPORT_FILENAME);
a.click();
}));

Expand Down

0 comments on commit 35f37c6

Please sign in to comment.