Dynamic Arrays are a very powerful feature of SpreadJS that allows you to automatically calculate and display values into multiple cells based on a formula entered in a single cell.
SpreadJS supports the following built-in dynamic array formulas:
The Spill Range Operator # can reference the entire spill range:
**Along with built-in dynamic array formulas, SpreadJS also supports custom dynamic array formulas where a user can create a custom function and use it in a dynamic array formula.
---
When using SpreadJS you can activate the Dynamic Array functionality by setting the flag allowDynamicArray = true;
Below is the javascript code:
// 1st way:
var workbook = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 1, allowDynamicArray: true});
// 2rd way:
var workbook = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
workbook.options.allowDynamicArray = true;
And if you are using our Designer component, follow the steps below:
Learn more on data analysis and extractions with Dynamic Arrays in a JavaScript Spreadsheet Component here.
Below there are some examples of how you can bring volatility to your javascript applications using Dynamic Arrays on SpreadJS.
Loan Amortization calculations are very useful in a range of FinTech applications. However, it might be tricky to update an amortization schedule when the number of periods changes. That is, you may want to see it for a loan with 360 months, and then update it for a loan with 180 months, and so on. As the number of periods changes, so does the number of lines to be displayed. For this reason, there might be the need to have various conditional formatting rules. That's not the case when you are using dynamic arrays: You can create a Loan Amortization Schedule with just 5 formulas.
Consider the below scenario:
Without dynamic arrays, we needed to edit formulas, hide or unhide rows, or fill formulas down in every row depending on the time term. With SpreadJS Dynamic Arrays functionality we will have to use only one formula to calculate each of the term outputs, saving in this way memory and calculation power.
=SEQUENCE(months)
=-IPMT(rate/12,B7#,months,amount)
=-PPMT(rate/12,B7#,months,amount)
=amount+CUMPRINC(rate/12,months,amount,1,SEQUENCE(months),0)
=E7#+F7#
And in no time you will have ready your Amortization Schedule.
If you try to change the Amount, Interest Rate, or Time period (months), the result will change accordingly without you having to update anything.
This example is a pretty generic one. Basically what we want to achieve is retrieve N Top record from a data set based on a certain condition, a very useful thing when having to write interactive reports and dashboards. Dynamic arrays help us achieve this in an easy and quick way.
As a dataset, we have a table called tblSales. What we want to do is extract a list of N Salespersons based on their Yearly Sales. The user will enter the number N on F5.
=IFERROR(SORT(FILTER(tblSales,tblSales[[#Data], [ Sales ]]>=LARGE(tblSales[[#Data], [ Sales ]],F5)),2,-1),"")
This formula finds the N-th largest item on the Sales column and gets all the items from the table that have Sales larger or equal with it. In the end, everything is sorted in a descending order (-1) taking the second column as the sort index.
In addition, we will include a check using the COUNT function to determine if the user has selected more entries than the list can provide.
To make the error message more visible we have used RED as the text color.
Next, there is an example of how we can use dynamic arrays to create a dynamic chart that displays only what the user wants to see, without manual filtering and row hiding.
This is especially useful when creating dashboards that include interactive charts depending on a selection.
Below is the table that contains a Personal Expense Budget.
What we would do is have a dropdown at M4 that would contain all the categories and when the user chooses a particular category, the table located at cell F4 containing Expenses and Amounts changes as well.
We could create the dropdown using the Date Validation functionality.
To get the list of Expenses belonging to a particular category, we can use the FILTER function.
=IF($M$4="All",SORTBY(tblExpenses[Expense],tblExpenses[Amount],-1),SORTBY(FILTER(tblExpenses[Expense],tblExpenses[Category]=$M$4),FILTER(tblExpenses[Amount],tblExpenses[Category]=$M$4),-1))
What the formula does is: gets all Expenses by filtering those with FILTER function (when All selected, get all expenses) and sort the array by the AMount.
We use the VLOOKUP to get the value of the amount of the Expense located at column F.
=VLOOKUP(F5#,tblExpenses[[Expense]:[Amount]],2,FALSE)
Then we assign a name to the array containing the names and the amount of the Expenses, for our ease of use when making calculations. To do that follow the above steps:
In our example:
name: exNames; Refer To: =ANCHORARRAY('Interactive Chart'!$F$5) - we can use $F$5# instead of ANCHORARRAY.
name: exAmounts; Refer To: =ANCHORARRAY('Interactive Chart'!$G$5)
These name ranges would be our chart's data source (we could also directly use the formulas).
And voila, we have an interactive chart that changes depending on which Category of the Expenses we want to be shown.
When we have a drop-down list with multiple values, it might be difficult to find a certain one. A searchable drop-down list would be a great way to improve the efficiency of finding values.
As a use case, we have a table containing a list of products and their corresponding prices. What we want to achieve is to have a searchable drop-down list on E4 that gets updated every time we change the content there, and displays a list of items that contain those letters anywhere in the product name.
To make a searchable drop-down list, follow the steps below.
To do that select H3 and enter the formula: =IFERROR(SORT(FILTER(B3:B33,ISNUMBER(SEARCH(E4,B3:B33)),"not found")),"not found")
The formula uses SEARCH to search for the word on the product list. SEARCH function returns the index if it finds the word and !VALUEotherwise, that is why we have the list filtered by the ISNUMBER function that gets only the items where we have an index.
Name: ddValid; Refer To: =ANCHORARRAY('Searchable Drop Down List'!$H$3)
If we change the content of cell E4 the Data Validation List will change and by default the content of the list from which we choose the Product.
That's how we can have a searchable list by just using dynamic array formula and no javascript extra code.
The next sample is a pretty straightforward one. We have a table containing the dates and the conversion rate from USD to EUR. Our objective is to allow the user to select a “from” date (cell F5) and a “to” data (cell G5) and have those dates retrieve the correct dataset for the Currency Conversion chart.
The user would select two dates: fromDate(F5) and toDate (G5), from two dropdowns. TO calculate the dates that will fill the two dropdowns follow the next steps:
For ease of use we can add two name ranges like below:
These names will serve as the source for the fromDate and toDate dropdowns respectively.
We will use the FILTER function to calculate the dates and rates based on the dates selection from the user.
After we need to assign a name to the array containing the Dates and the Rates just like we did for the Dates array, as we would use these names ranges as datasource for our chart. As before select Name Manager and set the names as below:
name: chartDates ; Refer To: =ANCHORARRAY('From-To Date Chart'!$K$3)
name: chartRates ; Refer To: =ANCHORARRAY('From-To Date Chart'!$L$3)
These name ranges would be our chart's data source (we could also directly use the formulas).
The output would be a chart that would show the rates depending on the from and to date selection. Below is a screenshot:
To summarise:
Download a free trial of SpreadJs and discover how easily you can embed Excel-like JavaScript spreadsheets and other business-related functionalities into your enterprise applications.
Contact us to learn more: us.sales@grapecity.com