OfficeJS - 删除和重新排列 Word 文档中的部分
OfficeJS -Delete and rearrange sections in a Word document
我想使用 office.js
删除和重新排列 Word 文档的部分。例如,一个 Word 文档有 3 个部分。这是部分列表 headers:
- 摇摆一节
- 摇摆部分二
- 摇摆节三
用户将其更改为:
- 摇摆节三
- 摇摆一节
最后,我想在我的 Word 文档中查看效果。我可以轻松地向用户显示 header 部分的列表。并且用户可以更改(重新排列或删除)header 的列表。我已经尝试了一种方法来做到这一点。我更改了 sections
的 items
并尝试加载 sections
。这是代码:
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
是一个 objects 的列表,其中包含已更新(用户更改后)的 header 列表。在这里,我只是尝试重新排列。
此代码不执行任何操作。我是否以正确的方式尝试?
感谢阅读。欢迎任何类型的tip/help!
我解决了这个问题。我选择了节体的 Ooxml
然后清除文档 body 并将节体作为我的序列。我使用 body 部分的第一行来匹配这些部分。您可以使用 header 部分。那也行。这是示例代码:
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);
}
我想使用 office.js
删除和重新排列 Word 文档的部分。例如,一个 Word 文档有 3 个部分。这是部分列表 headers:
- 摇摆一节
- 摇摆部分二
- 摇摆节三
用户将其更改为:
- 摇摆节三
- 摇摆一节
最后,我想在我的 Word 文档中查看效果。我可以轻松地向用户显示 header 部分的列表。并且用户可以更改(重新排列或删除)header 的列表。我已经尝试了一种方法来做到这一点。我更改了 sections
的 items
并尝试加载 sections
。这是代码:
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
是一个 objects 的列表,其中包含已更新(用户更改后)的 header 列表。在这里,我只是尝试重新排列。
此代码不执行任何操作。我是否以正确的方式尝试?
感谢阅读。欢迎任何类型的tip/help!
我解决了这个问题。我选择了节体的 Ooxml
然后清除文档 body 并将节体作为我的序列。我使用 body 部分的第一行来匹配这些部分。您可以使用 header 部分。那也行。这是示例代码:
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);
}