const xlsx = require("xlsx"); const fs = require("fs"); function appendToExcel(newResult, filename) { let workbook; // Load existing file if it exists if (fs.existsSync(filename)) { workbook = xlsx.readFile(filename); } else { workbook = xlsx.utils.book_new(); } const sheetName = "Results"; let worksheet = workbook.Sheets[sheetName]; // If the worksheet doesn't exist, create it if (!worksheet) { worksheet = xlsx.utils.json_to_sheet([]); xlsx.utils.book_append_sheet(workbook, worksheet, sheetName); } // Get existing data from the worksheet const data = xlsx.utils.sheet_to_json(worksheet); data.push(newResult); // Add new result // Update worksheet with new data const updatedWorksheet = xlsx.utils.json_to_sheet(data); workbook.Sheets[sheetName] = updatedWorksheet; // Write updated workbook to file xlsx.writeFile(workbook, filename); } module.exports = appendToExcel;