我正在创建一个插件,该插件使用 office.js 以表格形式显示数据库中的数据,并且在该表格列中可以包含 HTML 形式的数据。所以我的要求是当我创建表格时,如果有任何列具有应显示为带有格式的普通文本的 html 内容,则在该表中。
我找到了一些创建表的代码
function writeTable() {
// Build table.
var myTable = new Office.TableData();
myTable.headers = [["Cities"]];
myTable.rows = [['<b>Hello there</b>'], ['Roma'], ['Tokyo'], ['Seattle']];
// Write table.
Office.context.document.setSelectedDataAsync(myTable, { coercionType: Office.CoercionType.Table },
function (result) {
var error = result.error
if (result.status === Office.AsyncResultStatus.Failed) {
write(error.name + ": " + error.message);
}
});
}
在上面的代码中
myTable.rows = [['<b>Hello there</b>'], ['Roma'], ['Tokyo'], ['Seattle']];
在上面的代码中,第一个值是 html 内容,因此在创建表时不应显示 html,输出应像 Hello 那里以粗体显示。
我还找到了根据需要实际以正常形式显示 html 的代码,但我无法将其与上述代码一起使用。我找到的用于 html 渲染的代码是下面。
function writeHtmlData() {
Office.context.document.setSelectedDataAsync("<b>Hello</b> World!", { coercionType: Office.CoercionType.Html }, function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
// write('Error: ' + asyncResult.error.message);
}
});
}
谢谢!
Pradeep:我强烈建议您尝试在Word中为表格使用新的API。 这将帮助您满足所有格式需求。
API 目前为预览版,可在此处查看如何使用预览版。https://github.com/OfficeDev/office-js-docs/tree/WordJs_1.3_Openspec
然后查看主表操作对象的所有文档!
桌子:https://github.com/OfficeDev/office-js-docs/blob/WordJs_1.3_Openspec/word/resources/table.md
表格单元格https://github.com/OfficeDev/office-js-docs/blob/WordJs_1.3_Openspec/word/resources/tablecell.md
表行https://github.com/OfficeDev/office-js-docs/blob/WordJs_1.3_Openspec/word/resources/tablerow.md
最后,这里有一个示例,以便您了解如何使用 API :):
Word.run(function (ctx) {
var fruits = [["Apple", "red", "round", "crunchy"], ["Banana", "yellow", "long", "mushy"], ["Pear", "green", "oblong", "variable"]];
var fruitsNonuniform = [["Apple", "red"], ["Banana", "yellow", "long", "mushy"], ["Pear", "green", "oblong"]];
var fruitsUnderfilled = [["Apple", "red", "", ""], ["Banana", "yellow", "long", "mushy"], ["Pear", "green", "oblong", ""]];
var table = ctx.document.body.insertTable(fruits.length, fruits[0].length, "start", fruits);
ctx.load(table);
return ctx.sync().then(function () {
table.style = "Grid Table 6 Colorful - Accent 2";
return ctx.sync().then(function () {
showNotification("Success")
});
}).catch(function (e) {
showNotification(e.message);
});
});
希望这有帮助,编码愉快!!-胡安
中生成整个表,然后将其作为 HTML 插入。
function writeHtmlData() {
console.log('writeHtmlData');
var headers = [["Cities"]];
var rows = [['<b>Hello there</b>'], ['Roma'], ['Tokyo'], ['Seattle']];
var html = '<table>';
html += '<thead>';
for (var i = 0; i < headers.length; i++) {
html += '<tr>';
var cells = headers[i];
for (var j = 0; j < cells.length; j++) {
html += '<th>' + cells[j] + '</th>';
}
html += '</tr>';
}
html += '</tr>';
html += '</thead>';
html += '<tbody>';
for (var i = 0; i < rows.length; i++) {
html += '<tr>';
var cells = rows[i];
for (var j = 0; j < cells.length; j++) {
html += '<td>' + cells[j] + '</td>';
}
html += '</tr>';
}
html += '</tbody>';
html += '</table>';
Office.context.document.setSelectedDataAsync(html, { coercionType: Office.CoercionType.Html }, function (asyncResult) {
if (asyncResult.status == "failed") {
console.debug("Action failed with error: " + asyncResult.error.message);
}
});
}