How to Build a JavaScript Profit and Loss Analysis App
Tags: webdev, devops, javascript, tutorial
Published: 2023-11-10 20:32:32

---
canonical_url: https://developer.mescius.com/blogs/how-to-build-a-javascript-profit-and-loss-analysis-app
description: Learn how to build a JavaScript profit and loss analysis application using a SpreadJS pivot table. See more and build your app today.
---
SpreadJS can be used to create all sorts of spreadsheets, reports, statements, and much more. This blog will show you how to create a sample Financial JavaScript Profit and Loss (P&L) Statement with the SpreadJS Designer Runtime. This developer tool included with SpreadJS lets you have the same standard spreadsheet UI that you would expect when creating and changing a PivotTable.

**<u>To provide this Designer UI to your customers, you would need to purchase the</u> [SpreadJS Designer Component add-on.](https://developer.mescius.com/spreadjs/javascript-spreadsheet-designer)**

**[Download the sample](https://cdn.mescius.io/assets/developer/blogs/spread/20231027-how-to-build-a-javascript-profit-and-loss-analysis-app/P&L_PivotTable.zip) for this tutorial.**

## Creating the PivotTable

Before we can create a PivotTable, we must first create a table of data. We have already made a table on its own sheet in this case. To start the PivotTable in the Designer, select the entire table first and click the "PivotTable" button in the Insert tab.

![Pivot Table](//cdn.mescius.io/umb/media/o41babac/01-pivottable.png?rmode=max&width=760&height=543)

This will open the Create PivotTable dialog that lets you specify the data source and the location to put the PivotTable. In this case, we will use the existing table and create the PivotTable on a new worksheet.

![Insert](//cdn.mescius.io/umb/media/sjeletzl/02-insert.png?rmode=max&width=763&height=392)

This will create the PivotTable with some standard settings on another sheet.

## PivotPanel

To make this sample easier, we will only check the Account Group, Account, Fiscal Year Actual, and Fiscal Year Budget fields in the PivotPanel. This panel shows up on the right side of the screen when selecting a PivotTable.

![Panel](//cdn.mescius.io/umb/media/repp5pnw/03-panel.png?rmode=max&width=760&height=509)

Once you check those fields, the PivotTable will be shown with the selected fields.

![Set Up](//cdn.mescius.io/umb/media/m4qdmcs5/04-setup.png?rmode=max&width=765&height=492)

## Value Field Settings

By default, the values in the PivotTable are not formatted correctly, and some field headers have extra words like "Sum of," for example. We can change the formatting of the values and headers by right-clicking on the value field in the PivotPanel.

This will bring up the Value Field Settings dialog, which you can use to set a custom name for the header and a number format for the values, as shown [here](https://cdn.mescius.io/assets/developer/blogs/spread/20231027-how-to-build-a-javascript-profit-and-loss-analysis-app/05-settings.gif).

## Calculated Items

The next step is to add some calculated items to the PivotTable. These are essentially data fields that aren't present in the original data but reference those fields. We can see how these work by opening the Calculated Item dialog with the PivotTable Analyze tab.

![Calculate](//cdn.mescius.io/umb/media/lgekbso4/06-calculated.png?rmode=max&width=766&height=241)

With that dialog open, we can create a calculated item, in this case, "Gross Profit," which is simply the Revenue minus the Cost of Sales. To fill out this formula, we first need to double-click on the item’s field (Account Group) and then the item (Revenue or Cost of Sales).

This will populate the formula bar with the correctly formatted references. Now click on "Add." That calculated item will be added to the PivotTable.

![Dialog](//cdn.mescius.io/umb/media/k0tpvoxz/07-dialog.png?rmode=max&width=745&height=533)

We can also add a calculated field in the form of Variance, which is just the Fiscal Year Actual minus the Fiscal Year Budget.

![Profit and Loss](//cdn.mescius.io/umb/media/si5n0b5s/spread.png?rmode=max&width=760&height=506)

## Themes and Styling

To make the PivotTable a bit easier to understand, we can also collapse certain sections and apply a theme. Themes/Styles can be found in the Design Tab under the "PivotTable Styles" dropdown.

![Themes](//cdn.mescius.io/umb/media/rwpngmvt/08-themes.png?rmode=max&width=753&height=669)

We can also remove the gridlines from the sheet to make it look nicer by clicking on "GridLines" under the "Settings" tab and deselecting the Horizontal and Vertical Gridline options:

![Settings](//cdn.mescius.io/umb/media/hnjnc0fi/09-settings.png?rmode=max&width=760&height=684)

## Conditional Formatting

The final edit we can make is applying conditional formatting to specific fields to make the data stand out more.  In this case, we can add a formatting rule to the variance field to highlight whether it was above or below budget.  We first need to select the cells to which we want to apply conditional formatting, then use the Conditional Formatting drop-down in the Home tab and add an icon set rule.

![Profit and Loss](//cdn.mescius.io/umb/media/loyplj0x/spread2.png?rmode=max&width=763&height=557)

![Profit and Loss](//cdn.mescius.io/umb/media/zawprrzk/spread3.png?rmode=max&width=758&height=488)

Following this tutorial, you should have a complete JavaScript Profit and Loss PivotTable that you created entirely within the SpreadJS Designer! There are many more things that you can do with SpreadJS PivotTables, and we have only scratched the surface.

To open this file again or use it in an HTML page with SpreadJS, simply navigate to File>Save to save it as an SJS file:

![Save Files](//cdn.mescius.io/umb/media/swudgsoq/plnew1.png)