简体   繁体   English

如何在不弄乱MS Word 2013中的空白段落和对象标识符的情况下使用String.replace()方法?

[英]How to use String.replace() method without messing up blank paragraphs and object identifiers in MS Word 2013?

I am writing an app for MS Word 2013, which changes formatting of all dates in the selected text to a certain format. 我正在编写适用于MS Word 2013的应用程序,该应用程序会将所选文本中所有日期的格式更改为某种格式。 Here's a stripped-down version of the code, which looks for mm-dd-yyyy dates and changes them to dd.mm.yyyy dates once the text is selected from the document and button is clicked in the app. 这是该代码的精简版本,用于查找mm-dd-yyyy日期,并在从文档中选择文本并在应用程序中单击按钮后将其更改为dd.mm.yyyy日期。

(function () {
    "use strict";

    Office.initialize = function (reason) {
        $(document).ready(function () {
            app.initialize();
            $('#get-data-from-selection').click(getDataFromSelection);
        });
    };

    function getDataFromSelection() {
        Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
            function (result) {
                var output = 
                    result.value.replace(/(\d\d)-(\d\d)-(\d\d\d\d)/g, '$2.$1.$3');
                Office.context.document.setSelectedDataAsync(output);                
            }
         );
    }
})();

The problem I run into is that when table or text box is included into selection it is getting converted to regular text. 我遇到的问题是,当表格或文本框包含在选择中时,它会转换为常规文本。 For example: screenshot 1 例如: 屏幕截图1

When I select text before the table and include carriage return of the last line of text before the table into selection, all the selected text jumps into the top left cell of the table. 当我选择表格之前的文本并包括将表格之前的最后一行文本的回车符包括在内时,所有选定的文本都会跳到表格的左上角单元格中。 screenshot 2 屏幕截图2

At the same time if I only select cells of the table, nothing happens when I call the app. 同时,如果我仅选择表的单元格,则在调用应用程序时不会发生任何事情。

As for text inside text box, it disappears when text box is included into larger selection, but if only text inside text box is selected, everything works fine. 至于文本框内的文本,当将文本框包含在较大的选择中时,它会消失,但是如果仅选择文本框内的文本,则一切正常。

Briefly, an input to my code is a selection in the document, containing 3 regular paragraphs, a table, and a text box. 简而言之,我的代码的输入是文档中的选择,包含3个常规段落,一个表和一个文本框。 The output should be same as input with dates format changed from mm-dd-yyyy to dd.mm.yyyy. 输出应与日期格式从mm-dd-yyyy更改为dd.mm.yyyy的输入相同。 The structure of the document should remain the same. 文档的结构应保持不变。

Please hint me on what I am doing wrong. 请提示我我做错了什么。 I have been looking for answer for a while, but could not find the solution. 我一直在寻找答案,但找不到解决方案。 Thank you! 谢谢!

The problem is that you're using the CoercionType "plain text", so Word is doing its best to give you that. 问题是您使用的是CoercionType“纯文本”,因此Word正在尽力做到这一点。 This means the underlying structures of the Word document are being removed before being handed over to your code for processing. 这意味着Word文档的基础结构在移交给您的代码进行处理之前已被删除。

And then you're writing this plain text back into the document, replacing the selection. 然后,您将此纯文本写回到文档中,替换了所选内容。 In some instances, that will result in replacing the document structures. 在某些情况下,这将导致替换文档结构。 In other instances Word is deciding it won't do that, so nothing happens. 在其他情况下,Word决定不会这样做,因此什么也没发生。

If you want to do something like this "wholesale" you need to work with a CoercionType that retains Word's underlying structures as well as any formatting that might be present. 如果您想做类似“批发”的事情,则需要使用CoercionType,它保留了Word的基础结构以及可能存在的任何格式。 The HTML CoercionType might help, but best would certainly be OOXML (the Office Open XML file format). HTML CoercionType可能会有所帮助,但最好的确是OOXML(Office Open XML文件格式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM