gpt4 book ai didi

add-in - 使用 office.js 从 Word 中的 html 表单中的表中的 "BindingSelectionChanged"事件中逐个获取当前行单元格值

转载 作者:行者123 更新时间:2023-12-04 01:14:31 25 4
gpt4 key购买 nike

你好,我正在使用 office.js 创建一个应用程序,它将在 excel 和 word 应用程序插件中使用,我编写了一些代码,实际上逐个单元格地给出整行文本。但由于我的要求是维护样式和每个单元格并将它们存储在数据库中,这样当插件再次运行时,它应该以与存储相同的格式加载数据。目前它只是我收到的回复文本。我问过一个类似的问题,就是从当前单元格中获取带有样式的文本,这确实很好用。 How do I get the formatting the Current cell of the table in Word using office.js

如果可以按行和列位置获取单元格 html,还有另一件事也可以解决问题。谢谢!

最佳答案

您好,我找到了我的问题的解决方案,但这只是 word 的解决方案,这在 excel 中不起作用,但这对我有用,所以我写在这里:-

   function Addtable() {
var document = Office.context.document;
var headers = [["Cities"]];
var rows = [['<b>Hello there</b> <ul><li>One</li><li>Two</li></ul>'], ['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) {
document.bindings.addFromSelectionAsync(Office.BindingType.Table, function (result) {
console.log(result);

var binding = result.value;
binding.addHandlerAsync(Office.EventType.BindingSelectionChanged, onBindingSelectionChanged);
});
});
}

上面是我想生成一个包含 html 值的表格时调用的函数。

下面是我用来获取当前单元格的值并替换为一些虚拟值的代码。

  var onBindingSelectionChanged = function (results) {
if (!isExecuted) {
Word.run(function (context) {
var tableCell = context.document.getSelection().parentTableCell;
context.load(tableCell);

return context.sync()
.then(function () {
if (tableCell.isNull == true) {
//selection is not within a cell.....
console.log("selection not in a header");
}
else {
// the selection is inside a cell! lets get the content....
var body = tableCell.body;
var html = tableCell.body.getHtml();

var tableHtml = tableCell.body.getHtml();

context.sync()
.then(function () {
var cellHtml = html.value;
var newHtml = "<table><tr><td><ul><li>yellow</li></ul></td></tr></table";

// Option 1
body.insertHtml(newHtml, Word.InsertLocation.replace);

// Option 2
//body.clear();
//body.insertHtml(newHtml, Word.InsertLocation.end);

return context.sync().then(function () {
console.log('HTML was successfully replaced.');
}).catch(function (e) {
console.log(e.message);
});
}).catch(function (e) {
console.log(e.message);
});

}
}).catch(function (e) {
console.log(e.message);
});
});
isExecuted = true;
}
else {
isExecuted=false;
}

};

谢谢!

关于add-in - 使用 office.js 从 Word 中的 html 表单中的表中的 "BindingSelectionChanged"事件中逐个获取当前行单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38306010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com