简体   繁体   English

如何使用Office-js合并Word表的单元格

[英]How to merge cells of a Word table using Office-js

I'm creating an add-in for work in which I have to reformat text to perfectly fit our designer's format. 我正在为工作创建一个加载项,在该加载项中,我必须重新格式化文本以完全适合我们设计师的格式。

My problem is that they used tables with an irregular number of cells. 我的问题是他们使用的表具有不规则数量的单元格。 And it seems office-js isn't able to create them, even though a regular user in Word can. 即使Word中的普通用户也可以,office-js似乎无法创建它们。

怪异的表

As an example, this is actually just one table with 3 rows. 例如,实际上这只是一个包含3行的表。

The first row has 2 cells and the first is colored blue. 第一行有2个单元格,第一行是蓝色。 The second has all it's borders changed to white. 第二个将所有边框更改为白色。 The second row has just one cell. 第二行只有一个单元格。 The third also has just one cell with another table inside. 第三个也只有一个单元格,内部有另一个表。

I know how to do all the other stuff(Change border color, embed another table, etc). 我知道如何做所有其他事情(更改边框颜色,嵌入另一个表等)。 What I can't figure out is how to programatically merge cells. 我不知道的是如何以编程方式合并单元格。

Office-js' table documentation has no method for the purpose. Office-js的表文档没有为此目的的方法。 Digging around I found out Excel's part of Office-js can actually do this through their range object's merge method , but Word's obviously has no such method . 深入研究发现,Office js的Excel部分实际上可以通过其range对象的merge方法来做到这一点,但是Word显然没有这种方法

So, is there no way to merge table cells programatically with Office-js? 因此,没有办法以编程方式与Office-js合并表单元格吗?

It can be done when creating the table using the insertHtml or insertOoxml method. 使用insertHtmlinsertOoxml方法创建表时可以完成此insertHtml

Using HTML, for example: 使用HTML,例如:

let rng = context.document.body;
let tableHTML = "<table><tr><td/><td/></tr><tr><td rowspan='2' /><td/></tr><tr><td/></tr></table>";
rng.insertHtml(tableHTML, 'End');

Note, however, that this will not execute correctly in Word On-line, which does not (yet) support merging or splitting cells. 但是请注意,这将无法在Word On-line中正确执行,Word Online还不支持合并或拆分单元格。 It can display such tables correctly, but cannot create / modify them. 它可以正确显示此类表格,但不能创建/修改它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM