Office js 使用Word JS Api从文档中删除文本框

Office js 使用Word JS Api从文档中删除文本框,office-js,office-addins,word,Office Js,Office Addins,Word,我正在尝试从文档中删除文本框,并用它们的文本替换它们。 我知道Word JS API并没有提供一个简单的方法来处理文本框。 因此,我试图通过更新文档的OOXML或删除所选范围并插入文本片段来实现这一点 虽然我能够选择形状、图片、表格并执行此操作,但一旦出现文本框或所选对象包含一段文本,我就会失败。e、 我们在其中添加了一些文字的形状或文字艺术。这是我删除范围后使用的代码 Word.run(function (context) { let rng = context.docume

我正在尝试从文档中删除文本框,并用它们的文本替换它们。 我知道Word JS API并没有提供一个简单的方法来处理文本框。 因此,我试图通过更新文档的OOXML或删除所选范围并插入文本片段来实现这一点

虽然我能够选择形状、图片、表格并执行此操作,但一旦出现文本框或所选对象包含一段文本,我就会失败。e、 我们在其中添加了一些文字的形状或文字艺术。这是我删除范围后使用的代码

Word.run(function (context) {

        let rng = context.document.getSelection();

        return context.sync().then(function () {
            rng.delete();

            return context.sync().then(function () {

                rng.insertText("Foo - Bar", Word.InsertLocation.replace);

                return context.sync().then(function () {
                    console.log('done');
                });

            });
        });
    })
我得到的结果是,只有文本被替换,但它的容器(文本框)仍然保留。
有什么想法吗?

我最终采取了以下解决方法。它在Windows和macOs中运行良好且快速

  • 获取文档正文的OOXML

  • 解析OOXL.value并生成一个xmlDocument(xmlDoc)

  • 检测包含以下文本的现有文本框和形状:getElementsByTagName(“wps:wsp”)

  • 从(3)中提取文本

  • 生成一个简单的xml TextElement并提取文本

  • 将(3)替换为(5)

  • 将更新的xmlDoc序列化为xmlString并获取更新的OOXML.value

  • 将更新的OOXML.value插入到文档中,替换现有的OOXML.value

    运行(函数(上下文){

    //选择文档正文并提取OOXML
    const body=context.document.body;
    const ooxml=body.getOoxml();
    返回context.sync().then(函数(){
    //初始化DOM解析器
    const parser=new DOMParser();
    const xmlDoc=parser.parseFromString(ooxml.value,“text/xml”);
    //跑完全程
    const rows=xmlDoc.getElementsByTagName(“w:r”);
    for(设j=0;j0;
    //如果没有文本框、形状、艺术字,跳过当前运行
    如果(!rowHasTextBox)继续;
    //选择文本框、形状、艺术字和获取段落
    const textboxContainer=row.getElementsByTagName(“wps:txbx”)[0];
    常量段落=textboxContainer.getElementsByTagName(“w:p”);
    //创建将替换现有管路的新管路
    const newRow=xmlDoc.createElement(“w:r”);
    常量特征线=xmlDoc.createElement(“w:br”);
    //追加特征线和“{{”
    newRow.appendChild(特征线);
    newRow.appendChild(startRow);
    for(设p=0;p0;
    如果(!段落)继续;
    //提取文本
    让文本提取=”;
    const textboxtext=段落[p].getElementsByTagName(“w:t”);
    for(设k=0;k{
    console.log('Error:',Error);
    决议(假);
    });
    
  •      //Select document body and extract OOXML 
         const body = context.document.body;
         const ooxml = body.getOoxml();
    
         return context.sync().then(function () {
    
             //Initialize DOM Parser
             const parser = new DOMParser();
             const xmlDoc = parser.parseFromString(ooxml.value, "text/xml");
    
             //Get all runs
             const rows = xmlDoc.getElementsByTagName("w:r");
             for (let j = 0; j < rows.length; j++) {
                 const row = rows[j];
                 const rowHasTextBox = row.getElementsByTagName("wps:txbx").length > 0;
                 //If no textbox, shape, wordart exists skip current run
                 if (!rowHasTextBox) continue;
    
                 //Select textbox, shape, wordart and get paragraphs
                 const textboxContainer = row.getElementsByTagName("wps:txbx")[0];
                 const paragraphs = textboxContainer.getElementsByTagName("w:p");
    
                 // Create a new run which will replace the existing run
                 const newRow = xmlDoc.createElement("w:r");
                 const breakLine = xmlDoc.createElement("w:br");
                 //Append breakline and "{{"
                 newRow.appendChild(breakLine);
                 newRow.appendChild(startRow);
    
                 for (let p = 0; p < paragraphs.length; p++) {
                     //Check whether paragrapj has text
                     const paragraphHasText = paragraphs[p].getElementsByTagName("w:t").length > 0;
                     if (!paragraphHasText) continue;
                     //Extract text
                     let textExtracted = "";
                     const textBoxTexts = paragraphs[p].getElementsByTagName("w:t");
                     for (let k = 0; k < textBoxTexts.length; k++) {
                         const textBoxText = textBoxTexts[k].innerHTML;
                         textExtracted = textExtracted + textBoxText;
                         textExtracted = textExtracted + " ";
                     }
                      // Create a temp run which will hold the etxtracted text
                     const tempRow = xmlDoc.createElement("w:r");
                     const newText = xmlDoc.createElement('w:t');
                     newText.setAttribute("xml:space", "preserve");
                     newText.innerHTML = textExtracted;
                     textExtracted = "";
                     tempRow.appendChild(newText);
                     newRow.appendChild(tempRow);
                     const breakLine = xmlDoc.createElement("w:br");
                     newRow.appendChild(breakLine);
                 }
    
    
                 //Replace existing run with the new one
                 row.replaceWith(newRow);
             }
             //Serialize dom , clear body and replace OOXML
             const serializedXML = new XMLSerializer().serializeToString(xmlDoc.documentElement);
             body.clear();
             return context.sync().then(function () {
                 body.insertOoxml(serializedXML, Word.InsertLocation.replace);
                 console.log('done');
             });
         });
     })
     .catch(error => {
         console.log('Error: ', error);
         resolve(false);
     });