Skip to main content
Inspiring
May 23, 2019
Answered

determine if field is to be calculated base on position and charge code

  • May 23, 2019
  • 2 replies
  • 3477 views

ok, so I'm trying to make a field be document level script based on whether it's the first/last field in a range of fields with a value. So "Field1" has charge code 8008 and "FieldA-G" has a $40. Then the FieldA and FieldG values should be calculated at 75% and the rest stay the same.

Could possibly work like this in powershell. But i need it in JS

$global:arrFieldsWithValues = @()

function TrackField($field) {

     if ($field > 0){

          $global:arrFieldsWithValues += $field

     }

}

if ($global:arrFieldsWithValues.Count -eq 1) {

     $Total = $global:arrFieldsWithValues[0] * .75

}

if ($global:arrFieldsWithValues.Count -eq 2) {

     $Total = $global:arrFieldsWithValues[0] * .75

     $Total += $global:arrFieldsWithValues[1] * .75

}

if ($global:arrFieldsWithValues.Count -ge 3)

{

     $Total = $global:arrFieldsWithValues[0] * .75

     $Total += $global:arrFieldsWithValues[$global:arrFieldsWithValues.Count - 1] * .75

     for($i = 1; i$ < ($global:arrFieldsWithValues.Count - 1); $i++)

     {

          $Total += $global:arrFieldsWithValues[$i]

     }

}

This topic has been closed for replies.
Correct answer Qloud

What I ended up with works but is different from what my original post suggest. Originally the charge code for the field I'm working with were part of a drop down. But I ended up making an area for this script static and removed it from the drop down list. Thanks goes to try67​ for his guidance to getting me to my goal. It can likely be cleaned up a little but it works.

function Calc2(ndays, nrates) {

    var travelDay = 0.75;

    for (var arrDays=ndays.length-1; arrDays>=0; arrDays--) {

        var f = this.getField(ndays[arrDays]);

        if (f.valueAsString=="") ndays.splice(arrDays, 1);

        var g = this.getField(nrates[arrDays]);

        if (g.valueAsString=="") nrates.splice(arrDays, 1);

    }

    if (ndays.length <= 0 || nrates.length <= 0)event.value = "";

    if (ndays.length == 1 && nrates.length == 1){

        var days = this.getField(ndays[0]);

       

        var tDays = days.value;

        if (tDays == 1){

            cdays = tDays * travelDay;

            event.value = cdays;

        }

        else if (tDays == 2){

            cDays = (tDays * travelDay) * 2;

            event.value = cDays;

        }

        else if (tDays >= 3){

            var oDays = tDays-2;

            var rates = this.getField(nrates[0]).value;

            var calcRate = oDays * rates;

            var calcFLDay = (2 * rates) * travelDay;

            event.value = calcRate+calcFLDay;

        }

    }

    if (ndays.length >= 2 && nrates.length >= 2){

       

        var FirstDay = this.getField(nrates[0]).value;

        var LastDay = this.getField(nrates[nrates.length-1]).value;

        var travelRate = (FirstDay+LastDay)*travelDay;

        var iFirst = this.getField(ndays[0]).value -1;

        var iLast = this.getField(ndays[ndays.length-1]).value -1;

        travelRate += iFirst * this.getField(nrates[0]).value;

        if (ndays.length > 2) travelRate += this.getField(ndays[1]).value * this.getField(nrates[1]).value;

        travelRate += iLast * this.getField(nrates[ndays.length-1]).value;

       

        event.value = travelRate;

    }

}

2 replies

QloudAuthorCorrect answer
Inspiring
June 20, 2019

What I ended up with works but is different from what my original post suggest. Originally the charge code for the field I'm working with were part of a drop down. But I ended up making an area for this script static and removed it from the drop down list. Thanks goes to try67​ for his guidance to getting me to my goal. It can likely be cleaned up a little but it works.

function Calc2(ndays, nrates) {

    var travelDay = 0.75;

    for (var arrDays=ndays.length-1; arrDays>=0; arrDays--) {

        var f = this.getField(ndays[arrDays]);

        if (f.valueAsString=="") ndays.splice(arrDays, 1);

        var g = this.getField(nrates[arrDays]);

        if (g.valueAsString=="") nrates.splice(arrDays, 1);

    }

    if (ndays.length <= 0 || nrates.length <= 0)event.value = "";

    if (ndays.length == 1 && nrates.length == 1){

        var days = this.getField(ndays[0]);

       

        var tDays = days.value;

        if (tDays == 1){

            cdays = tDays * travelDay;

            event.value = cdays;

        }

        else if (tDays == 2){

            cDays = (tDays * travelDay) * 2;

            event.value = cDays;

        }

        else if (tDays >= 3){

            var oDays = tDays-2;

            var rates = this.getField(nrates[0]).value;

            var calcRate = oDays * rates;

            var calcFLDay = (2 * rates) * travelDay;

            event.value = calcRate+calcFLDay;

        }

    }

    if (ndays.length >= 2 && nrates.length >= 2){

       

        var FirstDay = this.getField(nrates[0]).value;

        var LastDay = this.getField(nrates[nrates.length-1]).value;

        var travelRate = (FirstDay+LastDay)*travelDay;

        var iFirst = this.getField(ndays[0]).value -1;

        var iLast = this.getField(ndays[ndays.length-1]).value -1;

        travelRate += iFirst * this.getField(nrates[0]).value;

        if (ndays.length > 2) travelRate += this.getField(ndays[1]).value * this.getField(nrates[1]).value;

        travelRate += iLast * this.getField(nrates[ndays.length-1]).value;

       

        event.value = travelRate;

    }

}

Thom Parker
Community Expert
Community Expert
May 23, 2019

Why don't you just put field calculations on FieldA and FieldG?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
QloudAuthor
Inspiring
May 28, 2019

it's a travel form so only A would be constant. But I think I'm going to use a different route. I'll just use a popup to ask if it's the first or last day of trip. Then calculate values based off select answer

Thom Parker
Community Expert
Community Expert
May 28, 2019

Ok, so all you need to do is have the calculation script test for a value.

var nValue = this.getField("Whatever").value;

if((nValue != "") && !isNaN(nValue))

{

    .. do Somthing...

}

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often