当前位置: 首页> 财经> 访谈 > 建设网站公司网站_网络小程序开发公司_优化步骤_域名注册免费

建设网站公司网站_网络小程序开发公司_优化步骤_域名注册免费

时间:2025/9/8 20:17:20来源:https://blog.csdn.net/qq_36129733/article/details/147580942 浏览次数:0次
建设网站公司网站_网络小程序开发公司_优化步骤_域名注册免费

ONLYOFFICE 表单可以轻松收集结构化输入,但空的、非必填字段经常会被忽略。在本篇博文中,我们将演示如何构建一个宏,用于突出显示表单中所有空的、非必填字段,或者仅突出显示包含特定提示文本的字段。

ONLYOFFICE Docs 最新下载

如何使用 ONLYOFFICE 宏突出显示具有特定提示文本的空文本字段

构建宏

1. 检索表单数据和实时表单对象

该宏首先使用 Api.GetDocument() 访问活动文档。然后,我们检索两组信息:

  • 表单数据:使用 doc.GetFormsData(),我们获取所有表单字段的 JSON 表示。此 JSON 包含诸如键、类型、值、提示以及字段是否必填等属性。
  • 活动表单对象:我们使用 doc.GetAllForms() 来检索与这些表单字段对应的活动对象。
    var doc = Api.GetDocument();var forms = doc.GetAllForms();var formsData = JSON.parse(JSON.stringify(doc.GetFormsData()));

2. 可选的基于提示的验证

有时您可能只想检查那些带有特定提示文本的空字段。在我们的宏中,您可以通过切换 checkSpecificTip 变量来启用或禁用此功能。例如,您可能需要只验证提示为“请输入您的第二个地址”的字段。

   // Optional: set to true to check only fields with a specific tip text.var checkSpecificTip = false;var requiredTipText = "Please enter your second address"; // Change this as needed.

3. 循环遍历每个表单字段

然后,该宏会遍历 JSON 数据中的每个表单字段。我们只关注文本表单字段(类型为“text”)。对于每个未标记为必填的文本字段,我们会检查其值是否为空。

forms.forEach(function(form) {if (form.GetFormType() === "textForm") {var key = form.GetFormKey();var required = form.IsRequired()// Find corresponding form data by key.var formData = formsData.find(function(fd) {return fd.key === key;});if (formData) {// A field is considered empty if its value is missing or only whitespace.var isEmpty = !formData.value || formData.value.trim() === "";// Use the live form's GetTip() to get the tip (if available).var tip = form.GetTipText() ? form.GetTipText() : "";var shouldCheck = !checkSpecificTip || (checkSpecificTip && tip.trim() === requiredTipText.trim());if (shouldCheck && isEmpty && !required) {form.SetBorderColor(0,255,127); // spring green border.form.SetBackgroundColor(171, 242, 255); // Light blue background.invalidCount++;}}}});
  • 检查字段值:
    如果字段的 JSON“值”属性缺失或仅存在空格,则该字段被视为空。
  • 提示过滤:
    如果启用了 checkSpecificTip,我们只检查提示与 requiredTipText 匹配的字段。
  • 高亮:
    对于符合条件的字段,我们找到对应的实时表单对象(通过匹配键),并设置春绿色边框和浅蓝色背景。

4.记录结果

最后,宏记录突出显示的空的不需要的文本字段的总数。

console.log("Form Field Validator complete. " + invalidCount + " empty unrequired text field(s) highlighted.");

完整的宏代码

(function () {// Optional: set to true to check only fields with a specific tip text.var checkSpecificTip = false;var requiredTipText = "Please enter your second address"; // Change this as needed.var doc = Api.GetDocument();var forms = doc.GetAllForms();var formsData = JSON.parse(JSON.stringify(doc.GetFormsData()));var invalidCount = 0;forms.forEach(function(form) {if (form.GetFormType() === "textForm") {var key = form.GetFormKey();var required = form.IsRequired()// Find corresponding form data by key.var formData = formsData.find(function(fd) {return fd.key === key;});if (formData) {// A field is considered empty if its value is missing or only whitespace.var isEmpty = !formData.value || formData.value.trim() === "";// Use the live form's GetTip() to get the tip (if available).var tip = form.GetTipText() ? form.GetTipText() : "";var shouldCheck = !checkSpecificTip || (checkSpecificTip && tip.trim() === requiredTipText.trim());if (shouldCheck && isEmpty && !required) {form.SetBorderColor(0,255,127); // spring green border.form.SetBackgroundColor(171, 242, 255); // Light blue background.invalidCount++;}}}});console.log("Form Field Validator complete. " + invalidCount + " empty unrequired text field(s) highlighted.");
})();

此宏会自动检测并高亮显示包含预定义占位符文本的空文本字段,从而节省时间并减少重复工作。我们希望它能够帮助您优化 ONLYOFFICE 表单并提升整体工作流程效率。

关键字:建设网站公司网站_网络小程序开发公司_优化步骤_域名注册免费

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: