当前位置: 首页 > article >正文

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

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

ONLYOFFICE Docs 最新下载

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

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

构建宏

从文档中检索所有表格

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

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


http://www.kler.cn/a/584631.html

相关文章:

  • Haskell爬虫:为电商运营抓取京东优惠券的实战经验
  • LLM论文笔记 23: Meta Reasoning for Large Language Models
  • 【算法】数组、链表、栈、队列、树
  • 饮食调治痉挛性斜颈,开启健康生活
  • 《C#上位机开发从门外到门内》2-4:Modbus协议
  • WEB-CTFyj解题视频笔记(持续更新)
  • 机器学习之距离度量方法
  • 欧姆龙PLC学习的基本步骤
  • 高并发场景下如何实现消息精准一次消费?实战Java幂等性设计
  • PyTorch中前身传播forward方法调用逻辑
  • AI赋能铁道安全巡检探索智能巡检新时代,基于YOLOv7全系列【tiny/l/x】参数模型开发构建铁路轨道场景下轨道上人员行为异常检测预警系统
  • 使用 JavaScript 和 HTML5 实现强大的表单验证
  • ClickHouse剖析:架构、性能优化与实战案例
  • LeetCode 力扣热题100 最长递增子序列
  • Anaconda conda常用命令:从入门到精通
  • Linux 常用 20 条指令,解决大部分问题
  • 关于vue ui 命令无法无法打开vue项目管理器的记录
  • PHP与数据库连接常见问题及解决办法
  • 四、子串——10. 和为 K 的子数组
  • 动态IP/静态IP