如何使用ONLYOFFICE宏将TextForms添加到表单模板

2024年01月11日作者: Mona

在处理表单模板文件时,我们经常使用虚拟占位符,并最终将它们替换为表单元素。在这篇文章中,我们将使用文档编辑器宏自动执行此过程,在指定单词的每个实例上插入一个 TextForm。

构建宏

  const oDocument = Api.GetDocument();
  const numberOfElements = oDocument.GetElementsCount();

在这里,当前文档存储在 oDocument 变量中。然后,我们使用 GetElementsCount() 方法检索文档中的元素总数。

for (let elementIndex = 0; elementIndex < numberOfElements; elementIndex++)

本节使用 for 循环来循环迭代每个元素。这样我们能确保在每个元素(段落)中插入指定单词的 TextForm。

    const oElement = oDocument.GetElement(elementIndex);
    const rawText = oElement.GetText();

 for 循环中,我们使用带有 elementIndex 的 GetElement() 方法获取每个文档元素。然后,我们提取纯文本,将其存储在 rawText 变量中。

 const targetWord = "sample";
    let count = 0;
    for (let i = 0; i < targetWord.length; i++) {
      count++;
    }
    const wordLength = count - 1;

在这里,我们定义了评论的目标词。之后,我们使用 for 循环确定单词的长度并将其存储在变量 wordLength 中。

   const wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");

然后,使用目标单词创建正则表达式 (wordRegex) 以查找文档中的所有匹配项。

    let match;
    while ((match = wordRegex.exec(rawText)) !== null) {
      let matchPosition = match.index; 
      let nextMatchPosition = matchPosition + wordLength;

在这里,在 while 循环中,我们迭代在 wordRegex 中找到并存储的所有实例。

对于每个匹配项,将计算单词 (matchPosition) 的起始位置和单词 (nextMatchPosition) 之后下一个字符的位置。

const insertRange = oElement.GetRange(matchPosition, nextMatchPosition);
insertRange.Select();

之后,我们将要操作的范围存储在 insertRange 中。然后,我们使用 Select() 方法选择范围

oDocument.InsertTextForm({
        key: "Personal information",
        tip: "Enter your first name",
        required: true,
        placeholder: "Name",
        comb: true,
        cellWidth: 3,
        multiLine: false,
        autoFit: false,
        placeholderFromSelection: true,
        keepSelectedTextInForm: false,
      });

最后,我们在选定的范围内插入一个 TextForm。InsertTextForm() 方法具有各种参数。您可以根据需要修改它们。

完整的宏代码

下面是整个宏的代码。

(function () {
    function addTextForm() {
      const oDocument = Api.GetDocument();
      const numberOfElements = oDocument.GetElementsCount();
  
      //looping through all the elements in the document.
      for (
        let elementIndex = 0;
        elementIndex < numberOfElements;
        elementIndex++
      ) {
        const oElement = oDocument.GetElement(elementIndex);
        const rawText = oElement.GetText();
  
        // Specify the target word and the length of the word
        const targetWord = "sample";
        let count = 0;
        for (let i = 0; i < targetWord.length; i++) {
          count++;
        }
        const wordLength = count - 1;
  
        // Find all occurrences of the target word in the document
        const wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");
        let match;
        while ((match = wordRegex.exec(rawText)) !== null) {
          let matchPosition = match.index; // returns the index where the word starts.
          let nextMatchPosition = matchPosition + wordLength; //the index where the word ends.
          console.log(matchPosition);
          console.log(nextMatchPosition);
          const insertRange = oElement.GetRange(matchPosition, nextMatchPosition);
          insertRange.Select();
          //insert a text form at the selected range
          oDocument.InsertTextForm({
            key: "Personal information",
            tip: "Enter your first name",
            required: true,
            placeholder: "Name",
            comb: true,
            cellWidth: 3,
            multiLine: false,
            autoFit: false,
            placeholderFromSelection: true,
            keepSelectedTextInForm: false,
          });
        }
      }
    }
  
    addTextForm();
  })();

现在,让我们运行宏看看效果。

我们相信,将此宏集成到您的 oForm 工作流程中将增强您使用表单模板和 oForm 的体验。

不要错过利用 ONLYOFFICE API 强大功能的机会。我们广泛的 API 方法库是您将想法变为现实的关键。如果您有任何问题或创新概念,欢迎与我们分享。也很高兴能够与您合作。祝探索之路好运!