• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
May 23, 2019 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]

     }

}

TOPICS
Acrobat SDK and JavaScript

Views

1.9K

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
community guidelines

correct answers 1 Correct answer

Explorer , Jun 20, 2019 Jun 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

...

Votes

Translate

Translate
Community Expert ,
May 23, 2019 May 23, 2019

Copy link to clipboard

Copied

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

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

Votes

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
community guidelines
Explorer ,
May 28, 2019 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

Votes

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
community guidelines
Community Expert ,
May 28, 2019 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...

}

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

Votes

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
community guidelines
Explorer ,
May 31, 2019 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;

}

Votes

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
community guidelines
Community Expert ,
May 31, 2019 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";

     }

}

Votes

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
community guidelines
Explorer ,
May 31, 2019 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";


  }


}

Votes

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
community guidelines
Community Expert ,
May 31, 2019 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.

Votes

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
community guidelines
Explorer ,
May 31, 2019 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

Votes

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
community guidelines
Explorer ,
May 31, 2019 May 31, 2019

Copy link to clipboard

Copied

i was trying to have the if inside the for

Votes

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
community guidelines
Community Expert ,
May 31, 2019 May 31, 2019

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Explorer ,
May 31, 2019 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..

Votes

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
community guidelines
Community Expert ,
May 31, 2019 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);

}

Votes

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
community guidelines
Explorer ,
May 31, 2019 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";

    }

}

Votes

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
community guidelines
Community Expert ,
May 31, 2019 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.

Votes

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
community guidelines
Explorer ,
May 31, 2019 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"

Votes

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
community guidelines
Community Expert ,
May 31, 2019 May 31, 2019

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Explorer ,
May 31, 2019 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?

Votes

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
community guidelines
Community Expert ,
May 31, 2019 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.

Votes

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
community guidelines
Explorer ,
Jun 05, 2019 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;

        }

    }

}

Votes

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
community guidelines
Community Expert ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Explorer ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

yes

Votes

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
community guidelines
Explorer ,
Jun 05, 2019 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.

Votes

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
community guidelines
Community Expert ,
Jun 05, 2019 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...

Votes

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
community guidelines
Explorer ,
Jun 06, 2019 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..

Votes

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
community guidelines