Help with javascript to calculate many fields

Community Beginner ,
Dec 05, 2017

Copy link to clipboard

Copied

Hello,

I need help with my java code. I got a form with 3 columns with 160 text fields in each column. They are named A1.0.0-A1.15.9, B1.0.0-B1.15.9, C1.0.0-C1.15.9.

What i want to do is take (value of) B1.x.x - C1.x.x and if the difference is more then 1.0 and less then 1.9 i want to give a message to controll that number.

So far i got this code:

(function calculate() { 

    var fieldNumber = event.target.name.replace("A", ""); 

    var v1 = +this.getField("B"+fieldNumber).value; 

    var v2 = +this.getField("C"+fieldNumber).value;

    var v3 = v1 - v2;

event.value = v3;     

})()

else if ((v3 >= 1.0) && (v3 <= 1.9)) {

app.alert = ("controll this");

}

else {

event.value = "";

})();

I get a syntex error on row 9, what did i miss? My idea is to call on this function in the calculate tab in the A.x.x column. Please help a novice thats trying to learn java

TOPICS
Acrobat SDK and JavaScript, Windows

Views

116

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

Help with javascript to calculate many fields

Community Beginner ,
Dec 05, 2017

Copy link to clipboard

Copied

Hello,

I need help with my java code. I got a form with 3 columns with 160 text fields in each column. They are named A1.0.0-A1.15.9, B1.0.0-B1.15.9, C1.0.0-C1.15.9.

What i want to do is take (value of) B1.x.x - C1.x.x and if the difference is more then 1.0 and less then 1.9 i want to give a message to controll that number.

So far i got this code:

(function calculate() { 

    var fieldNumber = event.target.name.replace("A", ""); 

    var v1 = +this.getField("B"+fieldNumber).value; 

    var v2 = +this.getField("C"+fieldNumber).value;

    var v3 = v1 - v2;

event.value = v3;     

})()

else if ((v3 >= 1.0) && (v3 <= 1.9)) {

app.alert = ("controll this");

}

else {

event.value = "";

})();

I get a syntex error on row 9, what did i miss? My idea is to call on this function in the calculate tab in the A.x.x column. Please help a novice thats trying to learn java

TOPICS
Acrobat SDK and JavaScript, Windows

Views

117

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
Dec 05, 2017 0
Most Valuable Participant ,
Dec 05, 2017

Copy link to clipboard

Copied

The error is on the app.alert line. The = sign does not belong. = is used only to set one value to another, but you aren't changing app.alert.

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...
Dec 05, 2017 1
Most Valuable Participant ,
Dec 05, 2017

Copy link to clipboard

Copied

Oh, and you have an "else" without an "if". When you've fixed this I think you will find the messages very annoying as there will be far too many of them.

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...
Dec 05, 2017 1
Community Beginner ,
Dec 05, 2017

Copy link to clipboard

Copied

Thank you. Yes i guess it can be annoying with alot of messages. I was thinking about highlighting the fields instead of message. Something like this:

(function calculate() {

    var fieldNumber = event.target.name.replace("A", "");

    var v1 = +this.getField("B"+fieldNumber).value;

    var v2 = +this.getField("C"+fieldNumber).value;

    var v3 = v1 - v2;

event.value = v3;    

}()

else if ((v3 >= 1.0) && (v3 <= 1.9)) {

    fillColor = color.red; 

    fillColor = color.red;

}

else if {

event.value = "";

})();

@try67 if you could please point out the errors you mentioned, that would help alot. Im just starting to learn java. i dont think i can use above code to fill/highlight cells, any ideas how to improve the code?

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...
Dec 05, 2017 0
Most Valuable Participant ,
Dec 05, 2017

Copy link to clipboard

Copied

First thing to learn is that's it's called JavaScript, not Java.

I suggest you look up some tutorials about core JavaScript and learn the basics of the language, such as correct syntax, function declaration, etc.

Once you've done that go over your code and you should find some mistakes yourself.

If you've done all of that and are still having issues, post here again.

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...
Dec 05, 2017 0
Adobe Community Professional ,
Dec 05, 2017

Copy link to clipboard

Copied

Get rid of the function. It's completely unnecessary. And in fact, the value v3 that is declared in the function is not accessible outside of it. So the if statement will never work.

Learn more about JavaScript and doing calculations here:

Free Video Content & Full Listing of Available Videos

Calculating field values and more

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...
Dec 05, 2017 0
Most Valuable Participant ,
Dec 05, 2017

Copy link to clipboard

Copied

There are a lot of errors in your code. You have lines outside the function. You have an else if without a preceding if. You have a mismatched number of parentheses, etc.

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...
Dec 05, 2017 1
Most Valuable Participant ,
Dec 05, 2017

Copy link to clipboard

Copied

Look at line 8. It starts with "else". Why?

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...
Dec 05, 2017 0