简体   繁体   English

Office-JS忽略Word for Mac中的尾随换行符

[英]Office-JS ignores trailing new-lines in Word for Mac

In my Office-JS Add-In I'm inserting a text with a trailing newline and set the cursor to the end of this text like this: 在我的Office-JS加载项中,我要插入带有尾随换行符的文本,并将光标设置为该文本的结尾,如下所示:

function myInsertTest() {
  Word.run(function(context) {
    var selectedRange = context.document.getSelection();
    selectedRange.insertText("myText", "End");
    selectedRange.insertText("\r\n", "End");
    selectedRange.select("End");
    return context.sync();
  });
}

In Word 2016 on Windows the cursor is correctly set to the second line, but in Word for Mac (Version 16.11) the cursor is set to the end of the first line. 在Windows上的Word 2016中,光标已正确设置为第二行,但在Mac版Word(版本16.11)中,光标已设置为第一行的末尾。

So if I run this function for example 3 times in a row this results in the following texts: 因此,如果我连续运行此函数例如3次,将导致以下文本:

On Windows: 在Windows上:

myText
myText
myText
<-- Cursor is here

On Mac: 在Mac上:

myTextmyTextmyText<-- Cursor is here
// 3 new lines following

Did I miss something? 我错过了什么?

You can avoid dealing with new line oddities by allowing Word to handle this for you. 您可以通过允许Word为您处理新的奇怪行而避免。 The trick is to provide insert an HTML <br> tag using insertHtml() instead: 技巧是使用insertHtml()代替插入HTML <br>标签:

function myInsertTest() {
  Word.run(function(context) {
    let selectedRange = context.document.getSelection();
    selectedRange.insertText("myText", "End");
    selectedRange.insertHtml("<br />&nbsp;", "End");
    selectedRange.select("End");
  });
}

Note that the &nbsp; 请注意, &nbsp; is required to get around Word being a little too clever and ignoring a break that doesn't have any content after it. 需要绕过Word一点点的聪明,并忽略之后没有任何内容的中断。

This actually a bug and in fact its now failing in the latest builds for Windows. 这实际上是一个错误,事实上,它现在在Windows的最新版本中失败了。 Thanks for reporting this issue we will look into it and fix soon. 感谢您报告此问题,我们将对其进行调查并尽快修复。

You can use the JavaScript 您可以使用JavaScript

String.fromCharCode(13)

to insert ANSI 13 (the character Word uses). 插入ANSI 13(Word使用的字符)。 So: 所以:

function myInsertTest() {
  Word.run(function(context) {
    var selectedRange = context.document.getSelection();
    selectedRange.insertText("myText", "End");
    selectedRange.insertText(String.fromCharCode(13), "End");
    selectedRange.select("End");
    return context.sync();
  });
}

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

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