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

Script to find the minimum of 4 fields EXCLUDING a field if it has zeros in it

Community Beginner ,
Jan 14, 2019 Jan 14, 2019

var aNumFields = ["FieldA", "FieldB", "FieldC", "FieldD"];

function Min("aNumFields")
{
    // n = number of fields that have a numerical value > 0
    var i = 0;
    for ( var i = 0; i<aNumFields.length; i++) {
       var v = this.getField(aNumFields).value;
           if ( v !=  ) {
             n++;
             min = v;
           }
    }
}
event.value = minValue;

I have changed this script around so many times I have probably made it impossible to correct, any assistance you can give would be greatly appreciated.

TOPICS
Acrobat SDK and JavaScript , Windows
667
Translate
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

Community Expert , Jan 14, 2019 Jan 14, 2019

Use this code as the custom calculation script of the field:

function Min(aFields) {

    var min = Infinity;

    for (var i = 0; i<aFields.length; i++) {

        var v = Number(this.getField(aFields).value);

        if (v!=0 && v<min) min = v;

    }

    return (min==Infinity) ? "" : min;

}

var aFields = ["FieldA", "FieldB", "FieldC", "FieldD"];

event.value = Min(aFields);

Translate
Community Expert ,
Jan 14, 2019 Jan 14, 2019

Yeah, that code is a mess... I can fix it for you but I need to know what should it return if all the fields are empty (or zero)?

Translate
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 Beginner ,
Jan 14, 2019 Jan 14, 2019

The Text box should stay blank if all the fields are zeros.

Translate
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 ,
Jan 14, 2019 Jan 14, 2019

Use this code as the custom calculation script of the field:

function Min(aFields) {

    var min = Infinity;

    for (var i = 0; i<aFields.length; i++) {

        var v = Number(this.getField(aFields).value);

        if (v!=0 && v<min) min = v;

    }

    return (min==Infinity) ? "" : min;

}

var aFields = ["FieldA", "FieldB", "FieldC", "FieldD"];

event.value = Min(aFields);

Translate
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 Beginner ,
Jan 14, 2019 Jan 14, 2019

Thank you I saved it and it worked like a charm.

Translate
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 Beginner ,
Jan 14, 2019 Jan 14, 2019

Thank you so very much.

I would have never thought to put the var fields and value at
the end.

I must have changed it 100 times, I think it is back to school
for me.  Using Java once in a blue moon I sure did forget a lot.

Thank you,

Translate
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 ,
Jan 14, 2019 Jan 14, 2019
LATEST

The location of it doesn't matter, actually. Could be before or after the function definition. The main issue was the code inside the function.

And if you want to learn it the first thing to realize is it's JavaScript, not Java. The names are similar, but the languages are quite different.

Translate
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