【问题标题】:Insert a line break with inserted text in a Word document在 Word 文档中插入带有插入文本的换行符
【发布时间】:2019-05-14 07:52:55
【问题描述】:

使用 javascript (office.js) 构建一个 word 插件来插入文本。到目前为止,带有 .insertText 的未格式化文本。如果我想插入下面​​的内容,应该使用哪个函数?

  • 格式化文本(例如大小、字体、样式)
  • 换行
  • 要点

代码:

results.items[i].insertText("Any text going here.", "replace");

例如,我如何在“Any text going here”中插入换行符?

【问题讨论】:

    标签: ms-word office-js word-addins


    【解决方案1】:

    使用 JavaScript,使用字符串 "\n" 添加“换行符”(我假设您的意思与在 UI 中按 ENTER 相同 - 从技术上讲,这是一个新段落)。所以,例如:

    results.items[i].insertText("Any text going here.\n", "replace");
    

    【讨论】:

    • Word 知道硬回报和软回报。 \n 是硬返回,会导致某些 Word 样式中出现额外的空白(就像您按两次 Enter 键一样)。您还可以在字符串中添加软返回,这与 shift + enter 相同,并且总是会导致下一行返回没有空格。你可以像String.fromCharCode(11)这样添加一个软返回。
    • @Fluous 你所说的“硬返回”是一个段落中断。可能添加或不添加的“空白”量是在与段落关联的段落样式中定义的段落格式的结果:Space Before 和/或 Space After。您所说的“软返回”只是段落中的换行符。区分这些总是很重要的,通常换行不是正确的 - 它的使用应该受到严格限制。
    • 感谢您的澄清!不久前,我正在搜索这些信息(即如何进行换行而不是换行)。通过发布此评论,我希望将来可以帮助某人。
    【解决方案2】:
    • 使用insertBreak 插入不同类型的中断。可以是换行符、分段符、分节符等。
    insertBreak(breakType: Word.BreakType, insertLocation: Word.InsertLocation): void;
    
    startNewList(): Word.List;
    

    列表示例

    //This example starts a new list stating with the second paragraph.
    await Word.run(async (context) => {
        let paragraphs = context.document.body.paragraphs;
        paragraphs.load("$none"); //We need no properties.
    
        await context.sync();
    
        var list = paragraphs.items[1].startNewList(); //Indicates new list to be started in the second paragraph.
        list.load("$none"); //We need no properties.
    
        await context.sync();
    
        //To add new items to the list use start/end on the insert location parameter.
        list.insertParagraph('New list item on top of the list', 'Start');
        let paragraph = list.insertParagraph('New list item at the end of the list (4th level)', 'End');
        paragraph.listItem.level = 4; //Sets up list level for the lsit item.
        //To add paragraphs outside the list use before/after:
        list.insertParagraph('New paragraph goes after (not part of the list)', 'After');
    
        await context.sync();
    });
    
    • 对于文本格式设置,您可以通过查看此处的examples 获得提示,其中设置了字体系列和文本颜色。
    //adding formatting like html style
    var blankParagraph = context.document.body.paragraphs.getLast().insertParagraph("", "After");
    blankParagraph.insertHtml('<p style="font-family: verdana;">Inserted HTML.</p><p>Another paragraph</p>', "End");
    
    // another example using modern Change the font color
    // Run a batch operation against the Word object model.
    Word.run(function (context) {
    
        // Create a range proxy object for the current selection.
        var selection = context.document.getSelection();
    
        // Queue a commmand to change the font color of the current selection.
        selection.font.color = 'blue';
    
        // Synchronize the document state by executing the queued commands,
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            console.log('The font color of the selection has been changed.');
        });
    })
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });
    

    Word 插件tutorial 有很多关于代码示例的常见任务的漂亮技巧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-30
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多