Excel function in javascript
Copy link to clipboard
Copied
Guys, I need some help.
How to write this function =IFERROR(IF(C6>B6;1;SUM(C6/B6));0) in javascript in acrobat reader dc?
can you help me?
Grateful
Berg
Copy link to clipboard
Copied
Looks like you need a calculation script.
These articles will cover everything you need:
https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm
https://www.pdfscripting.com/public/Entering-Calculation-Scripts.cfm
https://www.pdfscripting.com/public/How-to-Write-a-Basic-PDF-Calculation-Script.cfm
https://acrobatusers.com/tutorials/conditional-execution/
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Dear Thom Parker,
Thank you for helping me by showing the path of the stones.
However, unfortunately I don't have that much knowledge in JavaScript for PDF, I definitely intend to learn and be able to contribute to the community.
I needed at this point to be able to write this expression in JavaScript to PDF, so that I have a north of how to write other functions that I have.
Would it be possible for this help in writing this expression for mi, at this time?
Grateful
Berg
Copy link to clipboard
Copied
Hi @BergLino ,
I am not sure if you have tested your IFERROR formula in Excel but the syntax is wrong and it is throwing errors.
As with every programming language, you must watch for the proper syntax. So, when you're expressing logical comparisons in Micrososft Excel you must separate the conditional evaluations with a comma ( , ) instead of a semicolon ( ; ).
Change it like this:
=IFERROR(IF(C6>B6,1,SUM(C6/B6)),0)
In addition, you're not going to be able to edit a PDF using Adobe Acrobat Reader DC (free PDF viewer); you must have Adobe Acrobat Pro DC (full desktop version, PDF editor; requires paid subscription).
But anyway, if I understood correctly, the IFERROR formula that you're employing contains a nested function that evaluates a few conditions.
The first condition establishes that the value that is referenced in cell C6 should be greater than the value in cell B6. But, if that condition is NOT TRUE the formula will throw the infamous #VALUE! error.
When that condition is not met, you want to apply the value of the 1 instead of the #VALUE! erroralert in the eevent target cell, correct?
The second condition runs the first condition once more, and if C6 is greater than B6 then that condition is TRUE. If this condition is met, then divide the value of C6 by B6 and display that total in the event value cell.
And the third and last condition evaluates if the value in cell C6 is blank. If TURE, the result in the event value cell is zero (0).
Using the suggested guides of @Thom Parker , you may achive the equivalent in Acrobat JavaScript using a custom calculation script like shown in the example below:
/*
FROM MICROSOFT EXCEL =IFERROR(IF(C6>B6,1,SUM(C6/B6)),0)
declare your variables
*/
var B6 = Number(this.getField("B6").value);
var C6 = Number(this.getField("C6").value);
event.value = "0"; //set a default value
//establish a condition
if (C6 > B6) {
event.value = 1;
} else {
// reevaluate conditions
(C6 == 0) ? event.value = util.printf("0", event.target.vale) : event.value = util.printf("%.9f", C6/B6)
}
Here's a screenshot comparing both of Acrobat JavaScript result with Microsoft Excel IFERROR function:
Copy link to clipboard
Copied
I think the IFERROR function is used here to catch division by zero. In a script you can try to mimic that (although division by zero in JS does not cause an error), but a better solution is to avoid this situation altogether by checking the value of the denominator first.

