Getting a perspective of your business' expenses and sales is critical when making important financial and investment planning choices. For example, viewing the expenses of your company can help you judge the ROI of expanding your business.
SpreadJS can be used to create a financial application to help you keep track of your business' expenses and sales. This post will demonstrate how to create a financial application in JavaScript.
Follow along and download the sample used in this post.
Creating a Template
To make the application even easier to design, we can use the SpreadJS Designer to set cell types, styles, and spacing. For the purposes of this tutorial, I have already created a template we can use:
This template has a few different sheets:
- CompanyDashboard: the main view for the application, summarizing the other sheets with some charts
- Banking: a view of all of the payments the company makes as well as the current balance for the account
- Expenses: a list of daily expenses that the company makes
- SalesTransactions: a list of all of the sales that the company makes to their customers
- CheckingTemplate: the range template for the checking account balance on the "Banking" sheet
- DashboardBalanceTemplate: the range template for the checking account balance on the "CompanyDashboard" sheet
Setup JavaScript Application
To start off, we will need to create an HTML file to load all of the required libraries and files, including the SJS references, JS data files, and template:
In this case, I'm using the SpreadJS files from NPM, which can be installed in your app with the following command run from within the application's folder:
npm install @grapecity/spread-excelio @grapecity/spread-sheets @grapecity/spread-sheets-charts
Next we can initialize the workbook with the template we just created, as well as setting some variables for the sheets:
We will also create some functions for loading each sheet to set data and formulas later on, but for now we will just call those functions:
The "sheetsInitialized" array is used in some logic to determine if charts need to be set for the first time on a sheet. The charts in this workbook are going to be initialized using the pixel locations of specific cells, so we need to initialize each sheet individually and then the sheet's charts. The functions for setting these charts can be called in the ActiveSheetChanged event, using the "sheetsInitialized" array:
We'll also hide the range template sheets and set the scrollbar settings for the workbook:
Initializing a Sheet
Most of the sheets in this workbook have similar structures: 1 or more charts, a range template, and a data-bound table:
Before using the range template or setting the charts, we will need to set the data in the table. In this demo, I have put together some sample data that is contained within 3 different JS files, which we loaded earlier in this blog. From each file, we can create an array of table columns to initialize a table with, as well as formatting the data within the table:
Sorting Data for Charts
To more easily create the charts, we can use formulas to sort the data and use only values without formatting in the charts:
Create Range Template
With the template sheet for ranges defined in the CheckingTemplate sheet, we can create the range template celltype and set it in the sheet:
The range template will use the value of the cell that uses the celltype, which in this case will be an object that only has one property: CurrentBalance. The template sheet just has a data source template defined in it with the CurrentBalance property set in one of the cells.
Create Charts
Once we have that sorted data, we can create the charts from that data. These charts are just initialized at the location 0, 0 in the sheet with a default size of 100 px by 100 px. The activeSheetChanged event we bound earlier is what will be used along with custom functions for changing the size and position of each chart after the sheet is initially loaded:
The other sheets follow the same sort of logic for setting a table, range template, and charts.
That is all that is needed to create your own financial application for your business but the possibilities are endless with what can be done with SpreadJS. Please leave us your thoughts in the comments below, and happy coding!