diff --git a/.gitignore b/.gitignore index 153875e..f778417 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules vars.js -.idea \ No newline at end of file +.idea +*.xlsx \ No newline at end of file diff --git a/appendResultsToExcel.js b/appendResultsToExcel.js new file mode 100644 index 0000000..7609cd9 --- /dev/null +++ b/appendResultsToExcel.js @@ -0,0 +1,35 @@ +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; \ No newline at end of file diff --git a/iterateOverDashboardTable.js b/iterateOverDashboardTable.js index 7ab67b3..81097dd 100644 --- a/iterateOverDashboardTable.js +++ b/iterateOverDashboardTable.js @@ -6,9 +6,10 @@ start dan het script */ const puppeteer = require("puppeteer"); const isStartPakketAvailable = require("./evaluateStartPakket"); -const xlsx = require("xlsx"); const fs = require("fs"); const saveResultsToExcel = require("./saveResultsToExcel"); +const appendToExcel = require("./appendResultsToExcel"); +const wait = require("./wait"); async function iterateOverDashboardTable() { /*connection to local host */ @@ -18,6 +19,7 @@ async function iterateOverDashboardTable() { //array to store results for Excel export let results = [] + const filename = "DashboardResults.xlsx" // Get all open pages (tabs) const pages = await browser.pages(); @@ -55,6 +57,12 @@ async function iterateOverDashboardTable() { ); console.log(`Found ${links.length} AA-links to process.`); + // Initialize Excel file if it doesn't exist + if (!fs.existsSync(filename)) { + saveResultsToExcel([], filename); // Create empty file + console.log(`Initialized new Excel file: ${filename}`); + } + // process links for (let i = 0; i < links.length; i++) { console.log(`Processing link ${i + 1}`); @@ -68,6 +76,8 @@ async function iterateOverDashboardTable() { `${tableBodySelector} tr span[title="AA-rapport"] a` ); + if ((i + 1) % 10 === 0) {await wait(10000)} + const [newPagePromise] = await Promise.all([ new Promise((resolve) => browser.once("targetcreated", async (target) => { @@ -77,6 +87,7 @@ async function iterateOverDashboardTable() { ), links[i].click(), // Simulate the click ]); + let result; const newPage = await newPagePromise; @@ -84,7 +95,7 @@ async function iterateOverDashboardTable() { const evaluationResult = await isStartPakketAvailable(newPage); //Save results for Excel - results.push({ + result = ({ LinkNumber: i +1, StudentName: evaluationResult.studName || 'N/A', StudentID: evaluationResult.studId || "N/A", @@ -97,7 +108,7 @@ async function iterateOverDashboardTable() { } catch (error) { console.error(`Error processing link ${i + 1}:`, error.message); // Save error for Excel - results.push({ + result = ({ LinkNumber: i + 1, StudentName: "Error", StudentID: "Error", @@ -109,6 +120,8 @@ async function iterateOverDashboardTable() { finally { await newPage.close(); } + results.push(result); + appendToExcel(result, filename); } console.log("All links processed."); diff --git a/saveResultsToExcel.js b/saveResultsToExcel.js index 4ecd7db..3b30840 100644 --- a/saveResultsToExcel.js +++ b/saveResultsToExcel.js @@ -1,3 +1,4 @@ +const xlsx = require("xlsx"); function saveResultsToExcel(data, filename) { const workbook = xlsx.utils.book_new(); // Create a new workbook const worksheet = xlsx.utils.json_to_sheet(data); // Convert data to worksheet diff --git a/wait.js b/wait.js new file mode 100644 index 0000000..de37621 --- /dev/null +++ b/wait.js @@ -0,0 +1,5 @@ +function wait(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +module.exports = wait; \ No newline at end of file