Select Page

Search Knowledge Base by Keyword

Average Function

Goal:

Calculate an average amount from numbers inserted to TextInputs in a Document or Bundled Documents.

Instructions:

  1. Create the Template tag Average and attach this tag to all TextInputs in your Template that should be calculated.
  2. Create the Template tag GetAverageValue and attach this tag to the TextInput in your Template where the sum amount should be inserted.
  3. Insert the below-mentioned script to the Average tag.

Script Example:

const Tags = LEGITO.documentBuilder.getTagsByName("Average");

var finder = LEGITO.documentBuilder.event.createElementFinder();
var averageValues = finder.findElementsByTagsAuto(Tags);

const Results = LEGITO.documentBuilder.getTagsByName("GetAverageValue");
var resultElement = finder.findElementsByTagsAuto(Results)[0];

let valuesArray = []
for(var i in averageValues) {
if(averageValues[i].getValue() !== null) {
valuesArray.push(averageValues[i].getValue());
}
}

let arrAvg = valuesArray.reduce((a,b) => parseInt(a) + parseInt(b), 0);
let res = arrAvg / valuesArray.length;

resultElement.setValue(res.toString());