Created
January 8, 2022 18:36
-
-
Save HussainArif12/4afbba99e2c35187220d85634bce9bd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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