fix excel download issue

main
saif 2025-02-11 12:43:55 +05:00
parent e5a2260e4d
commit 175856217f
1 changed files with 35 additions and 20 deletions

View File

@ -221,20 +221,32 @@ const utils = require("./utils");
);
// Function to read Excel files and convert to JSON
const convertExcelToJson = async () => {
try {
const files = await fs.readdir(unprocessedFolderPath);
const convertExcelToJson = () => {
fs.readdir(unprocessedFolderPath, (err, files) => {
if (err) {
console.error("Error reading the directory:", err);
return;
}
// Filter Excel files (xlsx or xls)
const excelFiles = files.filter(
(file) => file.endsWith(".xlsx") || file.endsWith(".xls") || file.endsWith(".csv")
(file) =>
file.endsWith(".xlsx") ||
file.endsWith(".xls") ||
file.endsWith(".csv")
);
for (const file of excelFiles) {
// Process each Excel file
excelFiles.forEach((file) => {
const filePath = path.join(unprocessedFolderPath, file);
const workbook = xlsx.readFile(filePath);
const sheetName = workbook.SheetNames[0];
const sheetName = workbook.SheetNames[0]; // Use the first sheet
const worksheet = workbook.Sheets[sheetName];
// Convert the sheet to JSON
let jsonData = xlsx.utils.sheet_to_json(worksheet);
// Modify the column names by adding underscores
jsonData = jsonData.map((row) => {
const modifiedRow = {};
Object.keys(row).forEach((key) => {
@ -247,25 +259,28 @@ const utils = require("./utils");
});
return modifiedRow;
});
// write the JSON data to a file
const outputFile = path.join(
config.environment.temu_orders_path,
config[environment].temu_orders_path,
"data/unprocessed",
`${luxon.DateTime.now().toMillis()}.json`
);
await fs.writeFile(outputFile, JSON.stringify(jsonData, null, 2));
fs.writeFileSync(outputFile, JSON.stringify(jsonData, null, 2));
console.log(`Saved JSON to ${outputFile}`);
const processedPath = path.join(config.environment.temu_orders_path, "processed", file);
await fs.rename(filePath, processedPath);
console.log(`Moved Excel file ${filePath} to ${processedPath}`);
}
} catch (err) {
console.error("Error processing Excel files:", err);
}
console.log(
`Move Excel file ${filePath} to ${config[environment].temu_orders_path}/processed`
);
fs.renameSync(
filePath,
path.join(`${config[environment].temu_orders_path}/processed`, file)
);
});
});
};
await convertExcelToJson();
convertExcelToJson();
console.log(`==========< ENDED ${luxon.DateTime.now()} >==========`);
await page.close();
await browser.close();