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

Explorer ,
May 23, 2019

Copy link to clipboard

Copied

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]

     }

}

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;

    }

}

TOPICS
Acrobat SDK and JavaScript

Views

340

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

Explorer ,
May 23, 2019

Copy link to clipboard

Copied

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]

     }

}

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;

    }

}

TOPICS
Acrobat SDK and JavaScript

Views

341

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 23, 2019 0
Adobe Community Professional ,
May 23, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 23, 2019 0
Explorer ,
May 28, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2019 0
Adobe Community Professional ,
May 28, 2019

Copy link to clipboard

Copied

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...

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2019 0
Explorer ,
May 31, 2019

Copy link to clipboard

Copied

No, not test for value. Test for position in range of fields. If it's the first or last field with value in range of fields is what I am trying to do.

function something(check, fields) {

     If (Check = "yes") do something to first and last fields;

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Most Valuable Participant ,
May 31, 2019

Copy link to clipboard

Copied

Something like this?

function something(check, fields) {

     If (check == "yes") {

        this.getField(fields[0]).value = "First field";

        this.getField(fields[fields.length-1]).value = "Last field";

     }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Explorer ,
May 31, 2019

Copy link to clipboard

Copied

Umm, close I think it would be more like this... cause I only want to acknowledge fields with values

function something(check, fields) {

   var n = 0;

   If (check == "yes") ;{

   for (i in fields[i])

   if (i.value > 0)

   fields[i] = fields[n];

   n++;


   this.getField(fields[0]).value = "First field";


   this.getField(fields[fields.length-1]).value = "Last field";


  }


}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Most Valuable Participant ,
May 31, 2019

Copy link to clipboard

Copied

No, that loop doesn't make sense. If you only want to have fields with values then only add those fields to the array in the first place.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Explorer ,
May 31, 2019

Copy link to clipboard

Copied

I have to add all fields and take out the ones without values because the person filling the form out may not need all fields. So basically I was trying to make a new array of fields containing only fields with values

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Explorer ,
May 31, 2019

Copy link to clipboard

Copied

i was trying to have the if inside the for

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Most Valuable Participant ,
May 31, 2019

Copy link to clipboard

Copied

Does the array contain field names, or the actual Field objects?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Explorer ,
May 31, 2019

Copy link to clipboard

Copied

yes! lol... they are field names of field objects. which I'll have on the field when functions is called. something like

something(checkfield, [field1, field2, field3, field4, field5, field6])

But if the user only uses fields1-3, i need to recalculate values for 1 and 3. If that makes sense..

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Most Valuable Participant ,
May 31, 2019

Copy link to clipboard

Copied

OK, then you can filter the array like this so that it only contains fields that have non-empty values:

for (var i=fields.length-1; i>=0; i--) {

     var f = this.getField(fields);

     if (f.valueAsString=="") fields.splice(i, 1);

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Explorer ,
May 31, 2019

Copy link to clipboard

Copied

so the final would be similar to this?

function something(Check, Fields) {

    var A = this.getField(Check);

    If ((A).currentValueIndices=10) ;{

        for (var i=fields.length-1; i>=0; i--) {

            var f = this.getField(fields);

   

            if (f.valueAsString=="") fields.splice(i, 1);

           

        }

        this.getField(fields[0]).value = "First field";

        this.getField(fields[fields.length-1]).value = "Last field";

    }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Most Valuable Participant ,
May 31, 2019

Copy link to clipboard

Copied

No, this line has multiple errors in it:

If ((A).currentValueIndices=10) ;{

It should be:

if (A.currentValueIndices==10) {

You have to remember that JS is case-sensitive and that the comparison and value-assignment operators are not the same.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Explorer ,
May 31, 2019

Copy link to clipboard

Copied

i guess i could've said

(f[0]).value = "First Field"

(f[fields.lenth-1]).value= "Last Field"

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Most Valuable Participant ,
May 31, 2019

Copy link to clipboard

Copied

No, that wouldn't have worked if the array only contains field names.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Explorer ,
May 31, 2019

Copy link to clipboard

Copied

ok thanks, I have one more issue though. The values keep recalculating...

This is what I have

function something(Check, fields) {

    var A = this.getField(Check);

    if (A.currentValueIndices==10) {

        for (var i=fields.length-1; i>=0; i--) {

            var f = this.getField(fields);

            if (f.valueAsString=="") fields.splice(i, 1);

        }

        this.getField(fields[0]).value = this.getField(fields[0]).value*0.75;

        this.getField(fields[fields.length-1]).value = this.getField(fields[fields.length-1]).value*0.75;

    }

}

so if "first field" start with 100, i hit enter it goes to 75. Great so far. But when I do the next field "first field" goes to 56.25. How can I stop it from doing that?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Most Valuable Participant ,
May 31, 2019

Copy link to clipboard

Copied

You can't, really. You need to do this some other way. I would recommend keeping two sets of fields, the original values and the adjusted ones.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2019 0
Explorer ,
Jun 05, 2019

Copy link to clipboard

Copied

ok, I restructured the document a little bit. So now I have 3 "number of days" fields and 3 "rates" fields and a "total" field. "fields" is actually the array for number of days fields. This is the code I came up with. But I'm missing something cause it's not working. If you see anything wrong please let me know

function TripRate(fields, rates) {

    var travelDay = 0.75;

    for (var i=fields.length-1; i!==""; i--) {

        var f = this.getField(fields);

        if (f.valueAsString === "") fields.splice(i, 1);

        var r = this.getField(rates);

        if (r.valueAsString === "") fields.splice(i, 1);

    }

    var n = 0;

    if (fields.length < 1 || rates.length < 1)event.value = "";

    if (fields.length == 1 && rates.length == 1){

        var tDays = 0;

        tDays = fields[0].value;

        if (tDays==1){

            tDays = tDays * travelDay;

            event.value = tDays;

        }

        else if (tDays == 2){

            tDays = (tDays * travelDay) * 2;

            event.value = tDays;

        }

        else if (tDays > 2){

            var oDays = fields[0].value-2;

            var calcRate = oDays * r.value;

            var calcFLDay = (2 * r.value) * travelDay;

            var newRate = calcRate+calcFLDay;

            event.value = newRate;

        }

    }

    if (fields.length >= 2 && rates.length >= 2){

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

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

        var travelRate = (FirstDay+LastDay)*travelDay;

        fields[0].value--;

        fields[fields.length-1].value--;

        for ( i in fields){

            travelRate += fields.value * rates.value;

        event.value = travelRate;

        }

    }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 05, 2019 0
Adobe Community Professional ,
Jun 05, 2019

Copy link to clipboard

Copied

You use splice only on array fields. Is this correct?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 05, 2019 0
Explorer ,
Jun 05, 2019

Copy link to clipboard

Copied

yes

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 05, 2019 0
Explorer ,
Jun 05, 2019

Copy link to clipboard

Copied

looking back at it. I don't think I need the splice anymore. When I was using it the fields were set up differently and I believe I covered it on the code that follows it.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 05, 2019 0
Most Valuable Participant ,
Jun 05, 2019

Copy link to clipboard

Copied

This part of your code is problematic:

    for (var i=fields.length-1; i!==""; i--) {

        var f = this.getField(fields);

        if (f.valueAsString === "") fields.splice(i, 1);

        var r = this.getField(rates);

        if (r.valueAsString === "") fields.splice(i, 1);

    }

First of all, why are you comparing i to "", instead of zero?

Also, in the case that both the fields and rates values are empty in the same iteration you'll be removing two items, instead of one...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 05, 2019 0
Explorer ,
Jun 06, 2019

Copy link to clipboard

Copied

I believe I got the first half from you originally. And I added the second part.. but that was before I restructured the document. The intent was to strip blank fields from each array. However, the way I coded it below that, blank fields should not be an issue.

So I just removed that portion and I'm still getting NaN..

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 06, 2019 0
Most Valuable Participant ,
Jun 06, 2019

Copy link to clipboard

Copied

Without seeing the file and understanding what the values are it's very difficult to help you further with this.

If you're getting NaN it means you're trying to perform a mathematical operation on something that isn't a number, though.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 06, 2019 0
Explorer ,
Jun 06, 2019

Copy link to clipboard

Copied

Understandable, but there's only numeric values in it. And I'm only calling .value, i also tried valueasstring. I just noticed that even when I have no values in fields I get NaN...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 06, 2019 0
Most Valuable Participant ,
Jun 06, 2019

Copy link to clipboard

Copied

Sorry, I can't help further without seeing the file.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 06, 2019 0
Explorer ,
Jun 20, 2019

Copy link to clipboard

Copied

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;

    }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 20, 2019 0