我创建了一个 Word 加载项,并使用 Word API 1.3 在 Word 2016(版本 16.0.7341.2029)中插入一个表格,如下所示:
var value = [[3,4],[5,6]];
Word.run(function (ctx) {
var table = ctx.document.body.insertTable(value.length, value[0].length, Word.InsertLocation.end, value);
var myContentControl = table.insertContentControl();
myContentControl.tag = 'MyTable1';
myContentControl.title = 'This is a table';
return ctx.sync()
.then(function () {
console.log('Table created');
}).catch(function (err) {
console.log(err);
});
});
我在内容控件中看到具有正确值的表格。
当我检查text
控件的属性,我看到一个字符串4\t5\r\n6\t7
.
我想更改整个表的值,提供一个新数组(无需再次删除和添加整个表)。我想保留用户所做的格式。我正在尝试这样做:
Word.run(function (ctx) {
var controls = ctx.document.contentControls;
controls.load('id, tag, title, text');
// Get all content control, ...
return ctx.sync()
.then(function () {
// ... find the one using lodash, ...
var ctrl = _.find(controls.items, { 'tag': 'MyTable1' });
if (ctrl) { // found
// ... and update the value.
ctrl.text = newValue; // <== this line does not change the text
ctx.sync()
.then(function () {
console.log('Table should be updated');
}).catch(function (err) {
console.log(err);
});
} else {
Console.log('Unable to find table.');
}
}).catch(function (err) {
console.log(err);
});
});
我设置的行text
属性再次不会改变任何东西,我正在寻找一个函数,可以在不删除表格或逐个单元格的情况下完成此操作。有任何想法吗?
您可以按照与创建表值类似的方式在 Word 中设置表值。
table.values = [["a", "b"], ["c", "d"]];
下面是代码中的示例:
Word.run(function (ctx) {
var ctrl = ctx.document.contentControls.getByTag("MyTable1").getFirst();
return ctx.sync().then(function () {
if (!ctrl.isNull) { // found
var newValue = [['a', 'b'], ['c', 'd']];
ctrl.tables.getFirst().values = newValue;
ctx.sync().then(function () {
console.log('Table should be updated');
}).catch(function (err) {
console.log(err);
});
} else {
console.log('Unable to find table.');
}
}).catch(function (err) {
console.log(err);
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)