-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome.js
More file actions
236 lines (143 loc) · 8.54 KB
/
Copy pathhome.js
File metadata and controls
236 lines (143 loc) · 8.54 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
(function () {
"use strict";
// The initialize function is run each time the page is loaded.
Office.initialize = function (reason) {
$(document).ready(function () {
// Use this to check whether the API is supported in the Word client.
if (Office.context.requirements.isSetSupported('WordApi', 1.1)) {
// Do something that is only available via the new APIs
// $('#emerson').click(insertEmersonQuoteAtSelection);
// $('#checkhov').click(insertChekhovQuoteAtTheBeginning);
// $('#proverb').click(insertChineseProverbAtTheEnd);
// $('#fieldadd').click(insertfieldxml);
// $('#fieldaddname').click(insertfieldxmlname);
$('#props').click(showProps);
$('#addprop').click(addProp);
$('#addpropdirect').click(addPropInsert);
}
else {
// Just letting you know that this code will not work with your version of Word.
$('#supportedVersion').html('This code requires Word 2016 or greater.');
}
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if(msie===0) showProps(); //Don't know if it should go within document.ready
});
//showProps();
};
var gPropNames = [];
var gPropValues = [];
function showProps(){
Word.run(function(context){
var properties = context.document.properties;
$('#wordProps').html("Hello!");
context.load(properties);
var custom = properties.customProperties;
context.load(custom);
var longstring = "";
var myTable= "<table><tr><th>Property</th>";
myTable+= "<th>Value</th>";
myTable+= "<th>Insert</th></tr>";
// myTable+= "<td style='width: 100px; color: red; text-align: right;'>Button</td></tr>";
return context.sync().then(function(){
properties.title = properties.title + " Additional Title Text";
gPropValues.length = 0;
gPropNames.length = 0;
for(var i = 0; i < custom.items.length; i++){
longstring += custom.items[i].key + ": " + custom.items[i].value +"<br>";
myTable+="<tr class='ms-bgColor-themeLighterAlt--hover'><td>" + custom.items[i].key + "</td>";
myTable+="<td>" + custom.items[i].value + "</td>";
// myTable+="<td style='width: 100px; text-align: right;'><input type='radio' name='fieldNameSelection' value='" + custom.items[i].key + "'/></td></tr>";
myTable+="<td><button id='" + custom.items[i].key + "'>Insert</button></td></tr>";
gPropValues[i] = custom.items[i].value;
gPropNames[i] = custom.items[i].key;
}
//$('#wordProps').html(longstring);
myTable+="</table>";
document.getElementById('wordProps').innerHTML = myTable;
for(var j = 0; j < gPropNames.length; j++){
$('#'+custom.items[j].key).on("click",
{name: custom.items[j].key, value: custom.items[j].value},
insertfieldxmlpars);
}
return context.sync();
});
});
}
function deleteProp(name){
Word.run(function(context){
var properties = context.document.properties;
context.load(properties);
var custom = properties.customProperties;
context.load(custom);
var item = custom.getItem(name);
item.delete();
return context.sync().then(showProps);
});
}
function addProp(){
Word.run(function(context){
var properties = context.document.properties;
context.load(properties);
var custom = properties.customProperties;
context.load(custom);
var name = $('input[name="newName"]').val();
var value = $('input[name="newValue"]').val();
custom.add(name, value);
$('input[name="newName"]').val("");
$('input[name="newValue"]').val("");
return context.sync().then(showProps);
});
}
function addPropInsert(){
console.log("Trying");
addProp();
console.log("addProp is finished");
insertfield($('input[name="newName"]').val(), $('input[name="newValue"]').val());
console.log("insert is finished");
}
function insertfieldxmlpars(event) {
var fieldname = event.data.name;
var fieldvalue = event.data.value;
insertfield(fieldname, fieldvalue);
}
function insertfield(fieldname, fieldvalue) {
var myXML;
myXML = "<pkg:package xmlns:pkg=\"http://schemas.microsoft.com/office/2006/xmlPackage\">\n <pkg:part pkg:name=\"/_rels/.rels\" pkg:contentType=\"application/vnd.openxmlformats-package.relationships+xml\" pkg:padding=\"512\">\n <pkg:xmlData>\n <Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\n <Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"word/document.xml\"/>\n </Relationships>\n </pkg:xmlData>\n </pkg:part>\n <pkg:part pkg:name=\"/word/document.xml\" pkg:contentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml\">\n <pkg:xmlData>\n <w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" >\n <w:body>\n <w:p>\n <w:fldSimple w:instr=\"DOCPROPERTY " + fieldname + " \\* MERGEFORMAT\">\n <w:r>\n <w:t>" + fieldvalue + "</w:t>\n </w:r>\n </w:fldSimple>\n </w:p>\n </w:body>\n </w:document>\n </pkg:xmlData>\n </pkg:part>\n</pkg:package>";
Office.context.document.setSelectedDataAsync(
myXML,
{ coercionType: 'ooxml' },
function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log("Action failed with error: " + asyncResult.error.message);
} else {
Word.run(function (context) {
var range = context.document.getSelection();
range.select('end');
return context.sync();
});
}
});
}
function insertfield2016(fieldname, fieldvalue) {
var myXML;
myXML = "<pkg:package xmlns:pkg=\"http://schemas.microsoft.com/office/2006/xmlPackage\">\n <pkg:part pkg:name=\"/_rels/.rels\" pkg:contentType=\"application/vnd.openxmlformats-package.relationships+xml\" pkg:padding=\"512\">\n <pkg:xmlData>\n <Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\n <Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"word/document.xml\"/>\n </Relationships>\n </pkg:xmlData>\n </pkg:part>\n <pkg:part pkg:name=\"/word/document.xml\" pkg:contentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml\">\n <pkg:xmlData>\n <w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" >\n <w:body>\n <w:p>\n <w:fldSimple w:instr=\"DOCPROPERTY " + fieldname + " \\* MERGEFORMAT\">\n <w:r>\n <w:t>" + fieldvalue + "</w:t>\n </w:r>\n </w:fldSimple>\n </w:p>\n </w:body>\n </w:document>\n </pkg:xmlData>\n </pkg:part>\n</pkg:package>";
Word.run(function (context) {
var thisDocument = context.document;
var range = thisDocument.getSelection();
// Queue a command to replace the selected text.
range.insertOoxml(myXML, Word.InsertLocation.before);
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('Added XML for DOCPROPERTY '+ fieldname);
});
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
}
})();