Skip to content

Instantly share code, notes, and snippets.

View SimonMayerhofer's full-sized avatar
🕶️
'sup?

Simon Mayerhofer SimonMayerhofer

🕶️
'sup?
View GitHub Profile
@SimonMayerhofer
SimonMayerhofer / Fifo.gs
Created August 21, 2023 10:30 — forked from iassael/Fifo.gs
FIFO for Investments Inventory Accounting [Google Sheets - Apps Script]
/**
* Calculates the remaining inventory, current average buy price, total cost and total gains using FIFO (First In First Out) method.
* The function expects three parameters: transactionsType, transactionQuantities and transactionPrices sorted by date.
*
* Inspired by: https://www.allstacksdeveloper.com/2022/09/fifo-stock-portfolio-google-sheets.html
*
* @param {string[] | string} transactionsType - An array or a single string representing transaction types. Each string should be either 'B' for buy or 'S' for sell.
* @param {number[] | number} transactionQuantities - An array or a single number representing the quantities for each transaction.
* @param {number[] | number} transactionPrices - An array or a single number representing the prices for each transaction.
* @throws Will throw an error if transactionPrices and transactionQuantities are not arrays or if their lengths are not equal.