[英]Create table in word with data which is in html form using office.js
我正在創建一個插件,該插件使用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生成整個表,然后將其作為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);
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.