如何使用 ONLYOFFICE 宏对 PDF 表单中的特定字段执行计算

2025年03月13日作者:Selie

ONLYOFFICE 宏功能强大且用途广泛,不仅可用于自动执行文本文档、电子表格和演示文稿中的任务,还能应用于 PDF 表单。在本文中,我们将介绍如何创建并运行一个宏,用于对表单中的特定字段进行计算,以快速准确地处理税款计算。

How to perform calculations on specific fields in a PDF form using an ONLYOFFICE macro

构建宏

从文档中检索所有表单

首先,我们从文档中检索所有字段:

// Get all forms from the document
    const document = Api.GetDocument();
    let forms = document.GetAllForms();
  • Api.GetDocument() 获取当前活动文档。
  • GetAllForms() 检索文档内的所有表单字段。

定义表单键

然后,我们定义表单键:

  // Formkeys of input forms
    var formkey1 = "Form1";
    var formkey2 = "Form2";
    var taxFormkey = "TaxForm";

    // Formkeys of result forms
    var sumResultKey = "SumResult";
    var taxResultKey = "TaxResult";
  • formkey1formkey2 taxFormkey 用于存储输入字段的标识符。
  • sumResultKey 和 taxResultKey 用于存储输出结果的字段标识符。

检索表单值

getFormValue 函数会遍历表单,查找与 formKey 匹配的表单:

  // Function to get the value of a form by its key
    function getFormValue(formKey) {
        for (let form of forms) {
            if (form.GetFormKey() === formKey) {
                return parseFloat(form.GetText()) || 0;
            }
        }
    }
  • GetText() 检索表单的文本值。
  • parseFloat() 将文本值转换为数字。

若转换失败,则默认赋值为 0,以避免计算错误。

将值插入表单

setFormValue 函数搜索与 formKey 相匹配的表单字段,并使用 SetText() 方法将该字段更新为格式化后的数值(保留小数点后两位)。

  // Function to set the value of a result form
    function setFormValue(formKey, value) {
        for (let form of forms) {
            if (form.GetFormKey() === formKey) {
                form.SetText(value.toFixed(2));
            }
        }
    }

主要计算

在主要计算函数中,我们执行以下步骤:

  • 从表单字段中获取用户输入值。
  • 计算 input1 和 input2 的总和。
  • 计算总和的百分比以得出税额。
  • 调用 setFormValue() 方法,将计算得到的结果填入对应的表单字段中。
 // Main calculation function
    function calculateAndInsert() {
        let input1 = getFormValue(formkey1);
        let input2 = getFormValue(formkey2);
        let taxInput = getFormValue(taxFormkey);
        // Perform calculations
        var sum = parseFloat(input1) + parseFloat(input2);
        var tax = sum * taxInput / 100; // % tax

        // Insert results
        setFormValue(sumResultKey, sum);
        setFormValue(taxResultKey, tax);
    }

完整宏代码如下:

(function () {
    // Get all forms from the document
    const document = Api.GetDocument();
    let forms = document.GetAllForms();
    // Formkeys of input forms
    var formkey1 = "Form1";
    var formkey2 = "Form2";
    var taxFormkey = "TaxForm";

    // Formkeys of result forms
    var sumResultKey = "SumResult";
    var taxResultKey = "TaxResult";

    // Function to get the value of a form by its key
    function getFormValue(formKey) {
        for (let form of forms) {
            if (form.GetFormKey() === formKey) {
                return parseFloat(form.GetText()) || 0;
            }
        }
    }

    // Function to set the value of a result form
    function setFormValue(formKey, value) {
        for (let form of forms) {
            if (form.GetFormKey() === formKey) {
                form.SetText(value.toFixed(2));
            }
        }
    }

    // Main calculation function
    function calculateAndInsert() {
        let input1 = getFormValue(formkey1);
        let input2 = getFormValue(formkey2);
        let taxInput = getFormValue(taxFormkey);
        // Perform calculations
        var sum = parseFloat(input1) + parseFloat(input2);
        var tax = sum * taxInput / 100; // % tax

        // Insert results
        setFormValue(sumResultKey, sum);
        setFormValue(taxResultKey, tax);
    }

    // Call the calculation function
    calculateAndInsert();
})();

现在,让我们来执行这个宏,看看它的运行效果!

ONLYOFFICE 致力于为用户提供多功能工具,满足各种需求,其中宏功能尤为强大。

我们诚邀您探索我们的 API 方法库,发挥创意,开发独一无二的宏。如果您有任何宏作品或创新想法,欢迎随时与我们分享,您的建议和合作对我们非常重要。

创建免费的 ONLYOFFICE 账户

在线查看并协作编辑文本文档、电子表格、幻灯片、表单和 PDF 文件。