Copy link to clipboard
Copied
Hello,
I am new at Adobe Acrobate Pro DC and would need some help.
I have created one text field, where I would like to use basic calculation (plus and minus), just like in excel when you write in a cell for example "=2+5" and you press enter and you get a result 7.
Is it possible to have that in Adobe?
I don´t need to calculate between two fields, I would just like to use a basic mathematical operations inside one text field, so when I click enter I get a result.
And also if it is possible to calculate without "=" sign.
I supose I would need some custom made Script, but I haven´t figured it out yet.
Thank you.
Copy link to clipboard
Copied
If you will just use + and - you can use this as validation script of that field:
if(event.value){
var p = /\+|\-/;
var str = event.value;
var m = str.match(p);
var s = str.split(p);
if(m == "+")
event.value = Number(s[0])+Number(s[1]);
else
event.value = Number(s[0])-Number(s[1]);}
You don't need = sign, just use like this: 2+2 and press enter.
This script is only to be used with positive numbers.
Copy link to clipboard
Copied
*Nesa Nurani...thank you very much...it works great 🙂 I do work only with positive numbers so it´s just fine.
But I have a couple of things that still bother me and would really appreciate it if you could help me.
1. Since the format category is set to "None" I have no thousand seperator. I know it is possible when you set your format category to a "Number", but if I set it to "Number" I can´t input "+" or "-".
2. I would really like the user to only be able to put "+" and "-" as a calculation operations in a text field and nothing else (accept the numbers of course). Since the format category is set to "None" the user can bassicly put in all the letters, signs...
Is there any way to work arround this problem?
Thank you very much for your time and help.
Copy link to clipboard
Copied
I have actually one more issue...
if I don´t calculate in a field and I press enter, the result comes "NaN".
I would need also a possibility that a user writes only a number or calculates and by clicking Enter returns a result.
In this situation a need for example entering "2+0" so that I receive a result "2" otherwise it writes "NaN"
thank you
Copy link to clipboard
Copied
To remove NaN and if calculation is not correct for example (2 or +2 or 2+) is entered it will set field to blank, otherwise it will calculate, use this instead:
if(event.value){
var p = /\+|\-/;
var str = event.value;
var m = str.match(p);
var s = str.split(p);
if(!s[0] || !s[1])
event.value = "";
else if(m == "+")
event.value = Number(s[0])+Number(s[1]);
else if(m == "-")
event.value = Number(s[0])-Number(s[1]);}
To input only numbers and +- go to field properties, under format tab select 'Custom' and under 'Custom keystroke script' use this:
event.rc = /^[\d+-]*$/.test(event.change);
Copy link to clipboard
Copied
With "Custom keystroke script" works great. It prevented everything other except +,- and the numbers.
But in a script above I have an issue.
If I enter just a number (with no calculation) and press enter, than a field goes blank and I don´t want it to do that.
What I need is when:
1. I enter just a number (no calculations) and press enter, nothing happens (number stays)
2. When I calculate inside a text field (for example 2+2) and press enter, it shows up 4 (that already works)
3. If I calculate inside a text field bigger numbers (for example 20000+20000) and press enter, it should show me 40.000 (with a "dot" as a thousand seperator)
4. If I enter just a bigger number with no calculation (for example 40000) and press enter, it should show me 40.000 (with a "dot" as a thousand seperator)
thank you for everything so far 🙂
Copy link to clipboard
Copied
In that case use this:
if(event.value){
var p = /\+|\-/;
var str = event.value;
var m = str.match(p);
var s = str.split(p);
if(m == "+")
event.value = Number(s[0])+Number(s[1]);
else if(m == "-")
event.value = Number(s[0])-Number(s[1]);}
For "dot" as thousand separator under format tab , select 'Custom format script' and use this:
if(event.value)
event.value = util.printf("%,2.0f",event.value);
Copy link to clipboard
Copied
Thank you so much.
It works perfectly 🙂
Best regards
Nermin
Copy link to clipboard
Copied
Sorry, but I have on more question.
How could I change in a text field so that I can perform infinitive numbers of calculation (adding and substracting).
With this script I can only calculate two numbers (for example 2+2=4).
When I wan´t it for more numbers (for example 2+2+2) I get the result 4.
I tryed to change the script:
from
"event.value = Number(s[0])+Number(s[1]);"
to
"event.value = Number(s[0])+Number(s[1])+Number(s[2]);"
In this case I can calculate for example 2+2+2 and get 6, but when I calculate 2+2 it ends up NaN.
I would need to be possible to add or substract 2,3.....12...infinitive number of values or just to enter a number with no calculation and just press enter and get a number (this last function already works and would like to stay it that way).
Thank you.
Thank you.
Copy link to clipboard
Copied
As validation script use:
event.value = eval(event.value);
Copy link to clipboard
Copied
eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine.I wouldn't recommend this.
Copy link to clipboard
Copied
What party does you mean?
Copy link to clipboard
Copied
JS is so sand-boxed in Acrobat that it's really no danger at all. The worse that can be done is to make changes to the current document (which the user can do manually anyway).
Copy link to clipboard
Copied
If you don't include the "=" symbol it's actually very easy, using a neat hack. Try this as the field's custom Validation script:
event.value = eval(event.value);
This will allow you to use any mathematical operator, and even longer formulas. For example, if you enter "4*5+20/2" it will show "30" when you exit the field.
Copy link to clipboard
Copied
Just noticed this was already suggested above...
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more