更改内容控件中表的值 [英] Change value of a table in content control

查看:96
本文介绍了更改内容控件中表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Word加载项,我使用Word API 1.3在Word 2016(版本16.0.7341.2029)中插入一个表格,如下所示:

I created a Word add-in and I insert a table in Word 2016 (version 16.0.7341.2029) using the Word API 1.3 like this:

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 \\\\\\\

I see the table in a content control with the proper values. When I check the text property of the control, I see a string 4\t5\r\n6\t7.

我想更改提供新数组的整个表的值(不再删除和添加整个表)。我想保留用户的格式。我试图这样做:

I want to change the values of the whole table providing a new array (without removing and adding the whole table again). I want to keep the formatting the user made. I am trying to do it like this:

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 属性的行不会改变任何内容我正在寻找一个能够在不删除任何内容的情况下执行此操作的函数桌子或逐个细胞。有什么想法吗?

The line where I set the text property again does not change anything and I was looking for a function that does it without removing the table or going cell by cell. Any ideas?

推荐答案

您可以在Word中设置表格值,就像创建它们一样。

You can set table values in Word a similar way as you created them.

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);
    });
});

这篇关于更改内容控件中表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆