目录

文档在线协同工具ONLYOFFICE教程如何使用宏对-PDF-表单中的特定字段执行计算

文档在线协同工具ONLYOFFICE教程:如何使用宏对 PDF 表单中的特定字段执行计算

,作为一款功能强大的在线编辑器,适用于您使用的平台的文本文档、电子表格、演示文稿、表单和 PDF 阅读器。此次 ONLYOFFICE发布全新版本8.3,整个套件具有多项增强功能↓↓↓

ONLYOFFICE 宏功能强大且用途广泛,不仅可用于自动执行文档、电子表格和演示文稿中的任务,还可用于 PDF 表单中的任务。在这篇博文中,我们将向您展示如何创建和运行一个独特的宏,该宏对表单中的特定字段执行计算。在此示例中,宏有助于计算税款,使流程更快、更准确。

https://i-blog.csdnimg.cn/img_convert/e31ef499f9f14937b4d1119e70eb71a8.png

构建宏

从文档中检索所有表格

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

// 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";
  • formkey1 formkey2taxFormkey 存储输入字段的标识符。
  • sumResultKeytaxResultKey 存储将插入结果的输出字段的标识符。

检索 表单

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() 方法使用格式化的数值(2 位小数)更新该字段。

  // 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));
            }
        }
    }

主要 计算

在主计算函数中我们:

  • 从表单字段检索用户输入的值。

  • 计算输入1

    输入2 的总和。

  • 按总额的百分比计算税额。

  • 调用 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();
})();