在 Word 文档中插入带有插入文本的换行符
Insert a line break with inserted text in a Word document
使用 javascript (office.js) 构建文字加载项以插入文本。到目前为止,带有 .insertText 的未格式化文本。如果我想插入下面的内容,应该使用哪个函数?
- 格式化文本(例如大小、字体、样式)
- 换行符
- 要点
代码:
results.items[i].insertText("Any text going here.", "replace");
例如,我如何在 "Any text going here" 中插入换行符?
使用 JavaScript,添加 "line-break"(我假设您的意思与在 UI 中按 ENTER 相同 - 这在技术上是一个 新段落) 使用字符串 "\n"
。所以,例如:
results.items[i].insertText("Any text going here.\n", "replace");
- 使用
insertBreak
插入不同类型的中断。可以是换行符、段落符、分节符等
insertBreak(breakType: Word.BreakType, insertLocation: Word.InsertLocation): void;
- 用于添加项目符号等列表。使用
startNewList
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 有很多用于常见任务的绝妙技巧和代码示例。
使用 javascript (office.js) 构建文字加载项以插入文本。到目前为止,带有 .insertText 的未格式化文本。如果我想插入下面的内容,应该使用哪个函数?
- 格式化文本(例如大小、字体、样式)
- 换行符
- 要点
代码:
results.items[i].insertText("Any text going here.", "replace");
例如,我如何在 "Any text going here" 中插入换行符?
使用 JavaScript,添加 "line-break"(我假设您的意思与在 UI 中按 ENTER 相同 - 这在技术上是一个 新段落) 使用字符串 "\n"
。所以,例如:
results.items[i].insertText("Any text going here.\n", "replace");
- 使用
insertBreak
插入不同类型的中断。可以是换行符、段落符、分节符等
insertBreak(breakType: Word.BreakType, insertLocation: Word.InsertLocation): void;
- 用于添加项目符号等列表。使用
startNewList
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 有很多用于常见任务的绝妙技巧和代码示例。