OfficeJS - 删除和重新排列 Word 文档中的部分 [英] OfficeJS -Delete and rearrange sections in a Word document
问题描述
我想使用 office.js
删除和重新排列 Word 文档的各个部分.例如,一个 Word 文档有 3 个部分.这是部分标题的列表:
I want to delete and rearrange the sections of a Word document using office.js
. For example, a Word document have 3 sections. And here is the list of sections headers:
- 摇滚部分一
- 摇摆部分二
- 摇滚乐第三部分
用户将其更改为:
- 摇滚乐第三部分
- 摇滚部分一
最后,我想在我的 Word 文档中看到影响.我可以轻松地向用户显示部分标题列表.用户可以更改(重新排列或删除)标题列表.我已经尝试了一种方法来做到这一点.我更改了 sections
的 items
并尝试加载 sections
.代码如下:
And finally I want to see the impact in my Word document. I can easily show user the list of headers of sections. And user can change(rearrange or delete) the list of headers. I have tried a way to do this. I changed the items
of sections
and tried to load the sections
. Here is the code:
function RearrangeSections(sectionHeaderList) {
Word.run(function (context) {
var sections = context.document.sections;
context.load(sections);
return context.sync()
.then(function () {
if (sections != null) {
var headers = [];
for (var i = 0; i < sections.items.length; i++) {
// Grab the header from the current section
var header = sections.items[i].getHeader('primary');
// Add loading this header to the queue
context.load(header);
// Push this header into the headers collection
headers.push(header);
}
var sectionItems = sections.items; //Get section items into a new list
context.sync().then(function () {
for (var i = 0; i < sectionHeaderList.length; i++) {
for (var j = 0; j < headers.length; j++) {
var targetHeader = sectionHeaderList[i].name; //
if (headerText == targetHeader) {
context.document.sections.items[i] = sectionItems[j]; // Change the section items
}
}
}
context.load(context.document.sections); // Finally load the sections
return context.sync().then(function () {
//
});
});
}
}).catch(function (myError) {
//otherwise we handle the exception here!
});
}).catch(errorHandler);
}
在方法参数中,sectionHeaderList
是一个对象列表,其中包含更新的(用户更改后)标题列表.在这里,我只是尝试重新排列.
In the method parameter, sectionHeaderList
is a list of objects which has the updated(after user change) header list. Here I just tried to rearrange.
这段代码不做任何事情.我是否以正确的方式尝试?
This code do not do any thing. Am I trying in a proper way?
感谢阅读.欢迎任何类型的提示/帮助!
Thanks for reading. Any types of tip/help is welcome !
推荐答案
我解决了这个问题.我选择了部分正文的 Ooxml
,然后清除文档正文并将部分正文作为我的序列.我使用部分正文的第一行来匹配部分.您可以使用节标题.这也会起作用.示例代码如下:
I solved the problem. I picked the Ooxml
of the section bodies and then clear the document body and put the section bodies as my sequence. I used first line of the section body for matching the sections. You may use section header. That will also work. Here is the sample code:
function RearrangeSections(sectionHeaderList) {
Word.run(function (context) {
var body = context.document.body;
var sections = context.document.sections;
context.load(body);
context.load(sections);
return context.sync()
.then(function () {
if (sections != null) {
var itemsCount = sections.items.length;
var bodies = [];
var bodiesOoxml = [];
for (var i = 0; i < sections.items.length; i++) {
var body = sections.items[i].body;
context.load(body);
bodies.push(body);
}
// Sync/Exectute the queued actions
var sectionItems = [];
sectionItems = sections.items;
context.sync().then(function () {
for (var i = 0; i < bodies.length; i++) {
var ooxml = bodies[i].getOoxml();
bodiesOoxml.push(ooxml);
}
context.sync().then(function () {
context.document.body.clear();
for (var i = 0; i < sectionHeaderList.length; i++) {
for (var j = 0; j < bodies.length; j++) {
var bodyText = bodies[j].text;
var headerText = bodyText.split("\r")[0];
var targetHeader = sectionHeaderList[i].name;
if (headerText == targetHeader) {
var val = bodiesOoxml[j].value;
body.insertOoxml(val, Word.InsertLocation.end);
}
}
}
context.load(context.document.sections);
return context.sync().then(function () {
console.log("Done");
})
});
}).catch(function (e) {
//Show error
});
}
}).catch(function (myError) {
//Show error
});
}).catch(errorHandler);
}
这篇关于OfficeJS - 删除和重新排列 Word 文档中的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!