NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import { MONTHLY_INPUT_HEADERS } from "@/config/constants";
import { BusinessValidationException } from "@/exceptions";
import { Injectable } from "@tsed/di";
import * as XLSX from "xlsx";
import * as fs from "fs/promises";

@Injectable()
export class XLSXReaderServices {
constructor() {}

async readXLSX(contractorId: number, month: number, year: number) {
// Read XLSX file

const workbook = XLSX.readFile("src/services/test.xlsx");
const fifthSheet = workbook.Sheets["monthlyfill"];
const jsonFormat = XLSX.utils.sheet_to_json(fifthSheet, {
header: 1,
}) as string[][];

const headers = jsonFormat[0];
const rows = jsonFormat.slice(1);

const date = new Date(year, month, 0).getDate();

const requiredHeaders = [...MONTHLY_INPUT_HEADERS];

const periodEndDateIndex = requiredHeaders.indexOf("Period End Date");
requiredHeaders.splice(
periodEndDateIndex + 1,
0,
...Array.from({ length: date }, (_, i) => `Day ${i + 1}`)
);

if (
headers.length !== requiredHeaders.length ||
!headers.every((header, index) => header === requiredHeaders[index])
) {
throw new BusinessValidationException("Invalid Headers");
}

const newRows: any[] = [];

rows.forEach((row) => {
const totalPresent = row.slice(4, 4 + date).filter((x) => x.toLowerCase() === "p").length;
const totalAbsent = row.slice(4, 4 + date).filter((x) => x.toLowerCase() === "a").length;
const totalLeave = row.slice(4, 4 + date).filter((x) => x.toLowerCase() === "l").length;
const totalRest = row.slice(4, 4 + date).filter((x) => x.toLowerCase() === "r").length;
const totalWeeklyOff = row.slice(4, 4 + date).filter((x) => x.toLowerCase() === "w").length;
const totalEstablishmentClosed = row.slice(4, 4 + date).filter((x) => x.toLowerCase() === "e").length;

newRows.push([
...row,
totalPresent,
totalAbsent,
totalLeave,
totalRest,
totalWeeklyOff,
totalEstablishmentClosed,
]);
});

const wb = XLSX.utils.book_new();

// Information above the table
const info = [
["Name of Establishment :", "XYZ Corporation"],
["Name of Owner :", "John Doe"],
["Labour Identification No. :", "12345"],
["***Name and Address of Principal Employer :", "1234 Main St, City, Country"],
["***Labour Identification No. of Principal Employer :", "67890"],
];

let startingRow = 1;

// Create a worksheet
const ws = XLSX.utils.aoa_to_sheet([]);

// Write non-tabular information
info.forEach((item, index) => {
const row = startingRow + index;
item.forEach((value, colIndex) => {
const address = XLSX.utils.encode_cell({ r: row - 1, c: colIndex });
ws[address] = { t: "s", v: value };
});
});

// Update starting row for headers
startingRow += info.length + 1;

// Write headers
requiredHeaders.forEach((header, index) => {
const address = XLSX.utils.encode_cell({ r: startingRow - 1, c: index });
ws[address] = { t: "s", v: header };
});

// Update starting row for data
startingRow += 1;

// Write data
newRows.forEach((row, rowIndex) => {
row.forEach((value, colIndex) => {
const address = XLSX.utils.encode_cell({ r: startingRow + rowIndex, c: colIndex });
ws[address] = { t: "s", v: value };
});
});

// Add the worksheet to the workbook
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

// Convert the workbook to a binary string
const wbout = XLSX.write(wb, { bookType: "xlsx", type: "binary" });

// Function to convert a binary string to a buffer
function s2ab(s: string) {
const buf = new ArrayBuffer(s.length);
const view = new Uint8Array(buf);
for (let i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xff;
return Buffer.from(buf); // Convert ArrayBuffer to Buffer
}

// Write the file asynchronously
await fs.writeFile('EmployeeDetails1.xlsx', s2ab(wbout));

console.log(wb.SheetNames);
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.