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

Can a custom calculation script be overriden?

Community Beginner ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

I have the following script in a custom calculation property:

var a = this.getField("5a.0.4");
var b = this.getField("5a.1.4");
var c = this.getField("5a.2.4");
var d = this.getField("5b1");
var y = this.getField("2ea3").value;

var x = Number(a.value)+Number(b.value)+Number(c.value)+Number(d.value);

if(x != 0 && y>=x){
event.value = 0;
a.display = display.hidden;
b.display = display.hidden;
c.display = display.hidden;
d.display=display.hidden}
else{
event.value = x;
a.display = display.visible;
b.display = display.visible;
c.display = display.visible;
d.display=display.visible}

I wish to allow the user to also just type in a manual value, thus overriding the script if desired.  Unfortunately, the script overrides the manual entry.  Is there a way to switch off the script and allow a manual entry that stays visible?

TOPICS
JavaScript , PDF forms

Views

744

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

Community Expert , Aug 19, 2021 Aug 19, 2021

OK, then you can do it like this:

 

if (event.source!=null && (event.source.name=="5a.0.4" || event.source.name=="5a.1.4" || event.source.name=="5a.2.4" || event.source.name=="5b1" || event.source.name=="2ea3")) {
	// put the rest of the calculation code here
}

Votes

Translate

Translate
Community Expert ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

Yes, but you need to carefully think how it should work.

What should happen if they change the value of, for example, the "5a.0.4" field after they entered a custom value into the calculated field? Should it overwrite their custom value and re-apply the automatic calculation?

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 Beginner ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

I would say yes, it would overright the custome value.

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 ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

OK, next question: Are all the five fields used for the automatic calculation all have values inputted by the user, or do some of them have automatic calculations of their own?

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 Beginner ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

they are all inputted by user.

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 ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

OK, then you can do it like this:

 

if (event.source!=null && (event.source.name=="5a.0.4" || event.source.name=="5a.1.4" || event.source.name=="5a.2.4" || event.source.name=="5b1" || event.source.name=="2ea3")) {
	// put the rest of the calculation code here
}

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 Beginner ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

Would this work if someone had put an input into "2ea3" which is the sum ofthe other fields that was a different number than the sum ofthe other fields?  Like if all the other fields added up to 10, could someone override that and put in, for instance, 56, 100 or 10000000, or any other number?

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 ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

Sure, they can just enter the custom value into one of the source fields, and leave the others empty to get the same result. That might be easier...

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 Beginner ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

Well, I tried this code:

if (event.source!=null && (event.source.name=="5a.0.4" || event.source.name=="5a.1.4" || event.source.name=="5a.2.4" || event.source.name=="5b1" || event.source.name=="2ea3")) {
	var a = this.getField("5a.0.4");
var b = this.getField("5a.1.4");
var c = this.getField("5a.2.4");
var d = this.getField("5b1");
var y = this.getField("2ea3").value;

var x = Number(a.value)+Number(b.value)+Number(c.value)+Number(d.value);

if(x != 0 && y>=x){
event.value = 0;
a.display = display.hidden;
b.display = display.hidden;
c.display = display.hidden;
d.display=display.hidden}
else{
event.value = x;
a.display = display.visible;
b.display = display.visible;
c.display = display.visible;
d.display=display.visible}

I get Sytax Error Missing } in compound statement 21:at line 22

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 ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

Put one more } at the end.

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 Beginner ,
Aug 20, 2021 Aug 20, 2021

Copy link to clipboard

Copied

It seems I am back to where I started.  The point of this code was to make event.value = 0; if


var y = this.getField("2ea3").value
was greater than var x.  I need a way to manually override that.  However, now, if it is overrriden, it will not go back to that comparative function if the user goes back to any of the contributory fields

 

[var a = this.getField("5a.0.4");
var b = this.getField("5a.1.4");
var c = this.getField("5a.2.4");
var d = this.getField("5b1");]  (square brackets are not in the JS)

and changes them.  There is no way to revert so that the comparison is completed again.  It terminates the comparison functionality.  Is there a way to rever the who thing so that it looks at var y?  Or even if the user does not go back to change var a, b, c, or d, is there a way to keep the functionality of the script so that is continues to check to see if var y is greater than  the var x?

Again, I really really appreciate all the help here

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 ,
Aug 20, 2021 Aug 20, 2021

Copy link to clipboard

Copied

LATEST

Your desired behavior relies on some complex conditions. As try67 mentioned, you have to think about all the conditions involved and how they will affect the behavior. In fact, these should all be written down in a table. Then you design the code to fit the conditions listed in the table. 

 

 According to the code you posted above.

  1. When data is entered into any of the input fields the calculation takes place. So all you have to do to break the override is to enter data into any of these fields.

  2.  When the "2ea3" field value is greater than the sum of the other fields, the other fields are hidden. Therefore, the only field left to change for the override is the "2ea3" field. Enter data into this field and the override will be broken. 

 

If this isn't what you wanted to happen, then again, you need to write down exactly the the conditions that lead to each change in the field value, and the override. 

 

If necesary, you could have a button that resets the whole process. 

 

 

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