Managing business cash flow is a critical function of the operation of a business. Tracking how money enters and leaves accounts ensures financial stability and helps keep a close business health audit.
A cash flow calendar presents your cash flow in an intuitive and easy-to-use daily summary. It provides a view of each day of the month with highlighted positive and negative inflows and outflows.
This blog will learn how to create a Cashflow Calendar in JavaScript using our Excel-like JavaScript spreadsheet solution, SpreadJS. This calendar will make extensive use of the following powerful features:
Download the sample and follow along.
To create our Cashflow Calendar, we will need to create three sheets which are described below:
The data source of our example is a list of transactions.
We have created a more dynamic table, and we can reference Table1 when we need the data instead of the range of cells.
This table contains information regarding the TransactionID, type of transaction, date of the transaction, name of the company, name of the account, amount of the deposit, and withdrawal.
This page contains the template range that we will use to present the transactions happening in our cash flow calendar.
This range of cells here will be used as a template for the cells that will contain the wanted information in the cash flow calendar.
The first thing we do is arrange the cells and then set the binding path for the cells.
It could be done via Javascript by using the SpreadJS setBindingPathmethod.
templateSheet.setBindingPath(0, 1, "month");
templateSheet.setBindingPath(1, 2, "date");
templateSheet.setBindingPath(2, 2, "start");
templateSheet.setBindingPath(3, 2, "withdrawals");
templateSheet.setBindingPath(4, 2, "deposits");
templateSheet.setBindingPath(5, 2, "end");
If we are using SpreadJS Designer, which is included in the download and can be installed from the "\SpreadJS.Release.x.x.x\Designer\Designer Runtime" folder, we have to follow these steps:
To make it possible for the days with cash shortfalls (negative ending balance) to be colored with RED, the days with a positive ending balance to be colored with GREEN, and the neutral ones with BLACK, we can use Conditional Formatting. On the designer:
The first element of our calendar is a changeable month element. To add it, use the MonthPicker, a type of drop-down cell style in SpreadJS.
JavaScript:
var monthPickerStyle = new GC.Spread.Sheets.Style();
monthPickerStyle.dropDowns = [
{
type: GC.Spread.Sheets.DropDownType.monthPicker,
option: {
startYear: 2019,
stopYear: 2021,
height: 300,
}
}
];
sheet.setStyle(2, 5, monthPickerStyle);
Designer:
Select the cell (in our case B2)
We then assign a name to the cell containing the month when making calculations.
First, we create the design of the calendar, which should look something like this:
Use the SEQUENCE(rows,columns,start,step) function to assign the dates in our calendar. This allows us to retrieve later the cell value on CellClick. The formula for the B4 cell is:
=SEQUENCE(6,7,currentMonth-WEEKDAY(currentMonth)+1,1)
JavaScript:
cashflowSheet.setFormula(3, 1, '=SEQUENCE(6,7,currentMonth-WEEKDAY(currentMonth)+1,1)');
We have not yet used a formatter for these cells.
The next step is to use the conditional formatting to make it possible for the dates belonging to other months but the selected one be blank:
The following step includes using the RANGEBLOCKSPARKLINE that will use the range of cells in the TemplateSheet as a single cell type and the OBJECT function to apply the template in all the cells representing the dates in our cashflow calendar.
As we have used SEQUENCE to set values for these cells, we will use RANGEBLOCKSPARKLINE as a format.
As a first argument, it takes a cell range as the template from TemplateSheet.
As a second argument, it takes an OBJECT that takes data from the Table1, located on the Datasource Sheet.
Using the formula is to bind and return a range template to make working with range templates easier.
This is the final output:
As shown in the picture above, the cells containing days of the calendar give information regarding the Start/End Balance, Sum of Deposits, and Sum of Withdrawals.
If we want to extract from the DataSource page the list of all the transactions, we can do with the help of the SelectionChanged event. Worksheets in SpreadJS have their events bound to specific actions when those events occur.
In our example, we have used this handy SpreadJS functionality to extract the list of all the transactions when the user selects a date from the calendar.
We assign a name to the cell that will contain the selected date, deposits, and withdrawals since it's easier to make calculations and a table that will contain the information regarding the transactions. The steps when creating a name range for currentMonth are:
In our example:
name: currentSelection; refer to: ='Cash-Flow'!$B$11
name: currentDeposits; refer to: =FILTER(tblTransactions[Type]:tblTransactions[Withdrawal],(tblTransactions[Date]=CurrentSelection)*(tblTransactions[Deposit]>0))
name: currentWithdrawals; refer to: =FILTER(tblTransactions[Type]:tblTransactions[Withdrawal],(tblTransactions[Date]=CurrentSelection)*(tblTransactions[Withdrawal]>0))
Set different formulas that will get the list of all deposits, the list of all withdrawals, end, and start balance.
Where D13 is the Start Balance:
For the moment the currentSelection is manually inserted. To have it changed as per user date selection, follow the next step.
Create the event handler function in JavaScript (see below):
// on day selection, update a cell used in filtering the data to show detailed transaction list
cashflowSheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (sender, args) {
const sheet = args.sheet;
const row = args.newSelections[0].row;
const col = args.newSelections[0].col;
if ((row < 3 || row >= 3 + 6)
|| (col < 1 || col >= 1 + 7))
return;
// set the current date cell so that FILTER would update.
sheet.setValue(10, 1, sheet.getValue(row, col));
});
Once a user click cell, the above code checks if the cell is inside the calendar boundaries (B4:H9). Otherwise, it updates the currentSelection, and as a consequence, all the formulas used to get the balances and information regarding the transactions give the right results as they point in the changed selected date.
The above example is one of the many ways to use the SpreadJS features to enhance your applications and transform your content from a simple set of data to an engaging, ultra-useful Excel-like dashboard.
This JavaScript component offers, above else, hundreds of statistical and financial functions and formulas that will help you create effortlessly various elements in your financial applications.