There are several ways to create complex calculations in a clean elegant PDF file. Here’s how to integrate conditional formulas into your next document.
有多种方法可以在简洁优雅的PDF文件中创建复杂的计算。 这是将条件公式集成到下一个文档中的方法。
Acrobat中的条件公式 (Conditional Formulas in Acrobat)
We’ve previously covered the basics of automatic calculations in Adobe Acrobat. Conditional formulas let you go beyond the basics, creating more complex formulas in a PDF.
前面我们已经介绍了Adobe Acrobat中自动计算的基础知识。 条件公式使您可以超越基础知识,在PDF中创建更复杂的公式。
If you use formulas in Excel or Google Sheets, you likely already know what conditional formulas are. Also called logical formulas, they display a particular value or activate a calculation if a certain condition is met, such as if a number is negative or if a score is above the passing grade. Some of the most common conditional formulas in excel include the standard “IF,” as well as “SUMIF,” and COUNTIF.” There is also conditional formatting, where changes to the format are made if a cell meets a certain condition.
如果您在Excel或Google表格中使用公式,则可能已经知道什么是条件公式。 它们也称为逻辑公式,如果满足特定条件(例如,数字为负或分数高于及格分数),则它们将显示特定值或激活计算。 excel中一些最常见的条件公式包括标准“ IF”,“ SUMIF”和COUNTIF。 还有条件格式化,如果单元格满足特定条件,则对格式进行更改。
Using calculated fields, these kinds of formulas can also be applied in Adobe PDFs. You can display a number, text, or run a calculation based on the results of another input box. You can also use them in conjunction with other calculated fields. Conditional formulas are useful for many types of PDF forms, such as:
使用计算的字段,这些类型的公式也可以在Adobe PDF中应用。 您可以显示数字,文本或基于另一个输入框的结果运行计算。 您也可以将它们与其他计算字段结合使用。 条件公式可用于许多类型的PDF表单,例如:
Financial Documents: For example, if the principal determines someone’s interest rate, a conditional formula can be used to display the corresponding price based on the amount they input.
财务文件:例如,如果委托人确定某人的利率,则可以使用条件公式根据输入的金额显示相应的价格。
Tests and Exams: You can make an assessment that automatically shows either “pass” or “fail” at the end based on the test taker’s total score.
考试和考试:您可以进行评估,根据考生的总成绩自动显示“及格”或“不及格”。
Sales: If you’re using this for transactions, you can make a box that recommends products depending on a prospective buyer’s answers to specific questions.
销售:如果将其用于交易,则可以在一个框内根据潜在买家对特定问题的回答来推荐产品。
Take note that while calculated fields can only be made in Adobe Acrobat, the actual calculations will display in any PDF reader.
请注意,虽然只能在Adobe Acrobat中进行计算字段,但实际的计算将显示在任何PDF阅读器中。
创建简单的条件语句 (Creating Simple Conditional Statements)
Adobe Acrobat’s custom calculated fields use javascript as a programming language. Fortunately, you don’t need to know how to program in javascript to create a simple conditional field; you just need a basic pattern to follow.
Adobe Acrobat的自定义计算字段使用javascript作为编程语言。 幸运的是,您不需要知道如何使用javascript编程来创建简单的条件字段。 您只需要遵循一个基本模式即可。
In the following example, we’ll be using this simple company order form. This currently has seven fields: one for quantities of each of the five products, the total quantity, and the total price.
在以下示例中,我们将使用此简单的公司订单表格。 当前有七个字段:一个用于五个产品的数量,总数量和总价格。
A conditional statement in Adobe javascript follows this basic syntax:
Adobe javascript中的条件语句遵循以下基本语法:
var variable name = this.getField("name of field").value;
if( variable name condition) event.value = true result
else event.value = false result
var variable name = this.getField(" name of field ").value;
if( variable name condition ) event.value = true result
else event.value = false result
The first line defines the value that will be used for your conditional formula. In this particular case, we used the “this.getField” to obtain the value of one of the other fields in the document. On the second line, we define the condition. Similar to excel, we specify the condition as being greater than, less than, or equal to a particular value.
第一行定义将用于条件公式的值。 在这种特殊情况下,我们使用“ this.getField”获得文档中其他字段之一的值。 在第二行,我们定义条件。 与excel类似,我们将条件指定为大于,小于或等于特定值。
Lastly, we define the results. The true result is the value that will be displayed if the condition is met. On the next line, we use “else” to generate the false result, which is the value that will be displayed if the condition is not met.
最后,我们定义结果。 真正的结果是如果满足条件将显示的值。 在下一行,我们使用“ else”来生成错误结果,该错误结果是在不满足条件时将显示的值。
To put this into practice, we’ve created a calculated field called “Bulk Order.” To enter a custom calculated script, right-click the chosen field in Acrobat, and select “Properties.” From here, go to “Calculate > Custom Calculation Script > Edit.”
为了付诸实践,我们创建了一个名为“大宗订单”的计算字段。 要输入自定义的计算脚本,请在Acrobat中右键单击所选字段,然后选择“属性”。 从此处转到“计算>自定义计算脚本>编辑”。
This field will determine whether or not an order counts as bulk. The box will display “Yes” if the total quantity is greater than 20 items, and “No” if it is less than 20. Considering these conditions, we have the formula:
此字段将确定订单是否算作批量。 如果总数量大于20个,该框将显示“是”,如果小于20个则显示“否”。考虑到这些条件,我们有以下公式:
var TQ = this.getField("Total Quantity").value;
if( TQ > 20) event.value = "Yes"
else event.value = "No"
var TQ = this.getField(" Total Quantity" ).value;
if( TQ > 20 ) event.value = "Yes"
else event.value = "No"
In the image above, you can see that we set the variable name to “TQ,” and pulled the value of TQ from the field “Total Quantity.” Take note that these fields are case-sensitive. Then, we set our condition, which is that TQ must be greater than 20. If it meets this condition, it will display “Yes.” Otherwise, the box will generate “No.”
在上图中,您可以看到我们将变量名称设置为“ TQ”,并从“总计”字段中提取了TQ的值。 请注意,这些字段区分大小写。 然后,我们设置条件,即TQ必须大于20。如果满足此条件,它将显示“是”。 否则,该框将生成“否”。
If we ordered a total of 11 Lightning Cables and 10 Battery Banks, for example, we’d have a total of 21 items. It would then be considered a bulk order, and generate the following result:
例如,如果我们总共订购了11条避雷线和10个电池组,则总共有21件物品。 然后将其视为批量订单,并生成以下结果:
多种条件 (Multiple Conditions)
There are cases where you may want to have multiple conditions met instead of just one. Fortunately, there is a way to create a conditional field that generates values based on multiple conditions.
在某些情况下,您可能希望满足多个条件,而不仅仅是一个。 幸运的是,有一种方法可以创建一个条件字段,该条件字段可基于多个条件生成值。
Let’s say that in your store, all orders that reach at least 20 products and have a total price of 150 are eligible for a discount of 10%. The final amount would appear in a field called “Total With Discount.” In that case, we’d have to specify two variables and two conditions. We would have the following field:
假设在您的商店中,所有至少达到20种产品且总价为150的订单都可以享受10%的折扣。 最终金额将显示在“折扣总额”字段中。 在这种情况下,我们必须指定两个变量和两个条件。 我们将具有以下字段:
var Price = this.getField("Initial Price").value;
var TQ = this.getField("Total Quantity").value;
if( Price > 150 && TQ > 20 ) event.value = Price*0.9;
else event.value = Price;
var Price = this.getField(" Initial Price ").value;
var TQ = this.getField(" Total Quantity ").value;
if( Price > 150 && TQ > 20 ) event.value = Price*0.9 ;
else event.value = Price ;
As you can see, we defined two variables on two separate lines. You will also have to use the “&&” notation to combine the two different conditions. Take note that the final value is also a calculation that takes into account the discount.
如您所见,我们在两条单独的线上定义了两个变量。 您还必须使用“ &&”表示法来组合两个不同的条件。 请注意,最终值也是一种考虑了折扣的计算。
Therefore, if we use the same total as the example above, we’d generate the following result:
因此,如果我们使用与上面的示例相同的总数,则会生成以下结果:
计算顺序 (Calculation Orders)
One important consideration you should make is the calculation order. Unlike Excel, which renders calculations simultaneously, Acrobat relies on the user to determine which formulas come first.
您应该考虑的一项重要考虑是计算顺序。 与Excel可以同时呈现计算的Excel不同,Acrobat依靠用户来确定哪个公式最先出现。
To set the calculation order, go to the “Edit Form” sidebar and navigate to More > Set Field Calculation Order. In the above example, because the formulas for Bulk Order and Total With Discount are both reliant on Total Quantity and Total Price, we want to make sure that TQ and TP are calculated first.
要设置计算顺序,请转到“编辑表单”侧栏,然后导航至“更多”>“设置字段计算顺序”。 在上面的示例中,由于大宗订单和带折扣的总额的公式均取决于总数量和总价格,因此我们要确保首先计算TQ和TP。
Make sure to review the calculation order before publishing your form. It’s a good idea to try a few sample inputs on your form to make sure everything is working correctly.
在发布表单之前,请确保检查计算顺序。 最好在表单上尝试一些示例输入,以确保一切正常。
翻译自: https://www.howtogeek.com/679999/how-to-create-conditional-formulas-in-adobe-acrobat/