Skip to content

Instantly share code, notes, and snippets.

@HussainArif12
Created January 8, 2022 18:36
Show Gist options
  • Save HussainArif12/4afbba99e2c35187220d85634bce9bd6 to your computer and use it in GitHub Desktop.
Save HussainArif12/4afbba99e2c35187220d85634bce9bd6 to your computer and use it in GitHub Desktop.
const xlsx = require("xlsx");
const path = require("path");
const fileLocation = path.join(__dirname, "Expenses.xlsx");
const fileContents = xlsx.readFile(fileLocation);
const firstSheet = fileContents.SheetNames[0];
const sheetValues = fileContents.Sheets[firstSheet];
const parsedJSON = xlsx.utils.sheet_to_json(sheetValues);
function getParsedJSON() {
return parsedJSON;
}
function addEntry(text) {
xlsx.utils.sheet_add_aoa(sheetValues, [text], { origin: -1 });
xlsx.writeFile(fileContents, fileLocation);
}
function getTotal() {
const allData = getParsedJSON();
const totalResult = allData
.map((item) => item.Amount)
.reduce((acc, a) => acc + a, 0);
return totalResult;
}
module.exports = { getParsedJSON, addEntry, getTotal };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment