<body>
<div id="content-header">
<div class="padding">
<h1>Welcome</h1>
</div>
</div>
<div id="content-main">
<div class="padding">
<p>Choose the buttons below to add boilerplate text to the document by using the Word JavaScript API.</p>
<br />
<h3>Try it out</h3>
<button id="emerson">Add quote from Ralph Waldo Emerson</button>
<br /><br />
<button id="checkhov">Add quote from Anton Chekhov</button>
<br /><br />
<button id="proverb">Add Chinese proverb</button>
</div>
</div>
<br />
<div id="supportedVersion"/>
</body>
打开 Web 应用项目根目录中的文件“Home.js”。 此文件指定加载项脚本。 将整个内容替换为以下代码,并保存文件。
'use strict';
(function () {
Office.onReady(function() {
// Office is ready.
$(document).ready(function () {
// The document is ready.
// Use this to check whether the API is supported in the Word client.
if (Office.context.requirements.isSetSupported('WordApi', '1.1')) {
// Do something that is only available via the new APIs.
$('#emerson').click(insertEmersonQuoteAtSelection);
$('#checkhov').click(insertChekhovQuoteAtTheBeginning);
$('#proverb').click(insertChineseProverbAtTheEnd);
$('#supportedVersion').html('This code is using Word 2016 or later.');
} else {
// Lets you know that this code will not work with your version of Word.
$('#supportedVersion').html('This code requires Word 2016 or later.');
}
});
});
async function insertEmersonQuoteAtSelection() {
await Word.run(async (context) => {
// Create a proxy object for the document.
const thisDocument = context.document;
// Queue a command to get the current selection.
// Create a proxy range object for the selection.
const range = thisDocument.getSelection();
// Queue a command to replace the selected text.
range.insertText('"Hitch your wagon to a star." - Ralph Waldo Emerson\n', Word.InsertLocation.replace);
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('Added a quote from Ralph Waldo Emerson.');
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
}
async function insertChekhovQuoteAtTheBeginning() {
await Word.run(async (context) => {
// Create a proxy object for the document body.
const body = context.document.body;
// Queue a command to insert text at the start of the document body.
body.insertText('"Knowledge is of no value unless you put it into practice." - Anton Chekhov\n', Word.InsertLocation.start);
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('Added a quote from Anton Chekhov.');
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
}
async function insertChineseProverbAtTheEnd() {
await Word.run(async (context) => {
// Create a proxy object for the document body.
const body = context.document.body;
// Queue a command to insert text at the end of the document body.
body.insertText('"To know the road ahead, ask those coming back." - Chinese proverb\n', Word.InsertLocation.end);
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('Added a quote from a Chinese proverb.');
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
}
})();
打开 Web 应用项目根目录中的文件“Home.css”。 此文件指定加载项自定义样式。 将整个内容替换为以下代码,并保存文件。
Description 元素的 DefaultValue 属性有占位符。 将它替换为“A task pane add-in for Word”。
保存文件。
...
<ProviderName>John Doe</ProviderName>
<DefaultLocale>en-US</DefaultLocale>
<!-- The display name of your add-in. Used on the Store and various places of the Office UI such as the add-ins dialog. -->
<DisplayName DefaultValue="My Office Add-in" />
<Description DefaultValue="A task pane add-in for Word"/>
...