This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
word.document.service.js
100 lines (100 loc) · 5.35 KB
/
word.document.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"use strict";
// Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See full license in root of repo.
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WordDocumentService = void 0;
/*
This file defines a service for manipulating the Word document.
*/
var core_1 = require("@angular/core");
var WordDocumentService = /** @class */ (function () {
function WordDocumentService() {
}
/// <summary>
/// Performs a search and replace, but makes no changes to text in the excluded paragraphs.
/// </summary>
WordDocumentService.prototype.replaceFoundStringsWithExceptions = function (searchString, replaceString, excludedParagraph) {
// Run a batch operation against the Word object model.
Word.run(function (context) {
// Find and load all ranges that match the search string, and then all paragraphs in the document.
// Only the 'items' property of each is needed, no properties on the items are needed, so add any string
// after the 'items/' part of the load parameter.
var foundItems = context.document.body.search(searchString, { matchCase: false, matchWholeWord: true }).load('items/NoPropertiesNeeded');
var paras = context.document.body.paragraphs.load('items/NoPropertiesNeeded');
// Synchronize the document state by executing the queued commands, and return a promise to indicate task completion.
return context.sync()
.then(function () {
// Create an array of paragraphs that have been excluded.
var excludedRanges = [];
excludedRanges.push(paras.items[excludedParagraph].getRange('Whole'));
var replacementCandidates = [];
// For each instance of the search string, record whether or not it is in an
// excluded paragraph.
for (var i = 0; i < foundItems.items.length; i++) {
for (var j = 0; j < excludedRanges.length; j++) {
replacementCandidates.push({
range: foundItems.items[i],
locationRelation: foundItems.items[i].compareLocationWith(excludedRanges[j])
});
}
}
// Synchronize the document state by executing the queued commands, and return a promise to indicate task completion.
return context.sync()
.then(function () {
// Replace instances of the search string with the replace string only if they are
// not inside of (or identical to) an excluded range.
replacementCandidates.forEach(function (item) {
switch (item.locationRelation.value) {
case "Inside":
case "Equal":
break;
default:
item.range.insertText(replaceString, 'Replace');
}
});
});
});
})
.catch(this.errorHandler);
};
/// <summary>
/// Inserts sample content for testing the find-and-replace functionality..
/// </summary>
WordDocumentService.prototype.replaceDocumentContent = function (paragraphs) {
// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;
// Queue a commmand to clear the contents of the body.
body.clear();
// Queue commands to insert text into the end of the Word document body.
// Use insertText for the first to prevent a line break from being inserted
// at the top of the document.
body.insertText(paragraphs[0], "End");
// Use insertParagraph for all the others.
for (var i = 1; i < paragraphs.length; i++) {
body.insertParagraph(paragraphs[i], 'End');
}
// Synchronize the document state by executing the queued commands, and return a promise to indicate task completion.
return context.sync();
})
.catch(this.errorHandler);
};
WordDocumentService.prototype.errorHandler = function (error) {
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
};
WordDocumentService = __decorate([
core_1.Injectable()
], WordDocumentService);
return WordDocumentService;
}());
exports.WordDocumentService = WordDocumentService;
//# sourceMappingURL=word.document.service.js.map