Median Function
Goal:
Calculate a median amount from numbers inserted to TextInputs in a Document or Bundled Documents.
Instructions:
- Create the Template tag Median and attach this tag to all TextInputs in your Template that should be calculated.
- Create the Template tag GetMedianValue and attach this tag to the TextInput in your Template where the sum amount should be inserted.
- Insert the below-mentioned script to the Median tag.
Script Example:
var finder = LEGITO.documentBuilder.event.createElementFinder(); var medianValues = finder.findElementsByTagsAuto(Tags); const Results = LEGITO.documentBuilder.getTagsByName("GetMedianValue"); var resultElement = finder.findElementsByTagsAuto(Results)[0]; let valuesArray = [] for(var i in medianValues) { if(medianValues[i].getValue() !== null) { valuesArray.push(parseInt(medianValues[i].getValue(), 10)); } } function median(values){ if(values.length ===0) return 0; values.sort(function(a,b){ return a-b; }); var half = Math.floor(values.length / 2); if (values.length % 2) return values[half]; return (values[half - 1] + values[half]) / 2.0; } resultElement.setValue(median(valuesArray).toString()); |