Building a NodeJs, TypeScript Project For Reading Excel Files

Recap

Part 2

 npm i exceljs csvtojson
import * as csvToJson from 'csvtojson';
import * as ExcelJS from 'exceljs';
import * as fs from 'fs';
import * as path from 'path';

const tempFolderPath = path.join(__dirname, '../', 'temp');

const readFromFile = async (filePath: string) => {
return new Promise((resolve) => {
csvToJson()
.fromFile(filePath)
.then((jsonObj) => {
return resolve(jsonObj);
});
});
};

const writeToTempFile = async (
originalExcelFile: string,
tempCsvFilePath: string,
): Promise<void> => {
if (!fs.existsSync(tempFolderPath)) {
fs.mkdirSync(tempFolderPath);
}

const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(originalExcelFile);

const writeStream = fs.createWriteStream(tempCsvFilePath);
await workbook.csv.write(writeStream, { sheetName: 'books' });
};

export { tempFolderPath, readFromFile, writeToTempFile };
import * as fs from 'fs';
import * as path from 'path';
import { readFromFile, tempFolderPath, writeToTempFile } from './file-utils';

const run = async () => {
return new Promise(async (resolve) => {
const originalExcelFile = path.join(__dirname, '../', 'data', 'books.xlsx');
const tempCsvFilePath = path.join(tempFolderPath, 'temp_books.csv');
const jsonWriteFile = path.join(tempFolderPath, 'books.json');

console.log(`Reading file from: ${originalExcelFile}`);
await writeToTempFile(originalExcelFile, tempCsvFilePath);
const jsonObj = (await readFromFile(tempCsvFilePath)) as any[];

console.log(`Writing file ${jsonWriteFile}`);
const jsonWrite = fs.createWriteStream(jsonWriteFile);
jsonWrite.write(JSON.stringify(jsonObj, null, 2));

return resolve({
success: true,
message: `File wrote complete: Items Stored: ${jsonObj.length}`,
});
});
};

run()
.catch((e) => console.error(e))
.then((c) => console.log('Complete', c));

LETS GO!!!!!

Pull Request to merge back to main

Bonus

npm i -D dotenv-cli standard-version conventional-github-releaser
"release": "standard-version --no-verify",
"post:release": "npm run release:github && npm run release:tags",
"release:github": "dotenv -e .env -- conventional-github-releaser -p conventionalcommits",
CONVENTIONAL_GITHUB_RELEASER_TOKEN="<TOKEN_GOES_HERE>"
DEBUG=conventional-github-releaser
output from git release
git push --follow-tags origin develop
Release notes in github

Thoughts

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Allison

15 Years of Professionally putting characters in places that produces great software systems. A life time of hacking on computers.