Reputation: 1
I have a bunch of data stored in an array of objects and now want to fill it into an already existing Excel file. Each property has its column where it needs to be filled into and it should start filling onwards from and including row 25. A sample array of that data would look like this:
const XLSX = require("xlsx");
const puppeteer = require("puppeteer");
const path = require("path");
const filePathKfz = path.join(__dirname, "testFile.xlsx");
const testWeirdCompDetails = [{
name: "Blind Coders Academy",
telephone: "08000000000",
email: "[email protected]",
website: "https://www.blindcodersacademy.com",
},
{
name: "Drunken Priests Inc.",
telephone: "00112233885577",
email: "[email protected]",
website: "http://www.drunkenpriests.com",
},
{
name: "Ministry of Honeybadgers",
telephone: "01849723452",
email: "[email protected]",
website: "http://www.ministryofhoneybadgers.grr",
},
];
async function updateExcelWeirdComp() {
const workbook = XLSX.readFile(filePathKfz); // Read the file
const sheetName = workbook.SheetNames[0]; // Get first sheet
const worksheet = workbook.Sheets[sheetName]; // Get worksheet
console.log("This is my worksheet:", worksheet);
let startRow = 2;
testWeirdCompDetails.forEach((company, index) => {
const currentRow = startRow + index;
worksheet[`B${currentRow}`] = {
t: "s",
v: company.name
}; // Company Name
worksheet[`D${currentRow}`] = {
t: "s",
v: company.website
}; // Website
worksheet[`F${currentRow}`] = {
t: "s",
v: company.email
}; // Email
worksheet[`G${currentRow}`] = {
t: "s",
v: company.telephone
}; // Telephone
});
try {
XLSX.writeFile(workbook, filePathKfz);
console.log("File has been written successfully!");
} catch (error) {
console.error("Error writing the file:", error);
}
console.log("Test Details:", testCompDetails);
console.log("Excel file updated, Your Highness 👑");
}
// Call the update function
updateExcelKfz().catch(console.error);
I do not know what is wrong with that code since the code runs all the way without any errors showing in the terminal, yet the testFile is not being written into.
Upvotes: -2
Views: 28