-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
office-js-preview-tests.ts
320 lines (261 loc) · 8.85 KB
/
office-js-preview-tests.ts
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
office-js-preview
Copyright (c) Microsoft Corporation
*/
function test_excel() {
// Range
Excel.run(function (ctx) {
var range = ctx.workbook.getSelectedRange().load("values");
return ctx.sync()
.then(function () {
var vals = range.values;
for (var i = 0; i < vals.length; i += 1) {
for (var j = 0; j < vals[i].length; j += 1) {
vals[i][j] = vals[i][j].toUpperCase();
}
}
range.values = vals;
})
.then(ctx.sync);
}).catch(function (error) {
console.log(error);
});
// Chart
Excel.run(function (ctx) {
var sheet = ctx.workbook.worksheets.getItem("Sheet1");
var range = sheet.getRange("A1:B3");
range.values = [
["", "Gender"],
["Male", 12],
["Female", 14]
];
var chart = sheet.charts.add(Excel.ChartType._3DColumn, range, "Auto");
chart.format.fill.setSolidColor("F8F8FF");
chart.title.text = "Class Demographics";
chart.title.format.font.bold = true;
chart.title.format.font.size = 18;
chart.title.format.font.color = "568568";
chart.legend.position = "Right";
chart.legend.format.font.name = "Algerian";
chart.legend.format.font.size = 13;
chart.dataLabels.showPercentage = true;
chart.dataLabels.format.font.size = 15;
chart.dataLabels.format.font.color = "444444";
var points = chart.series.getItemAt(0).points;
points.getItemAt(0).format.fill.setSolidColor("8FBC8F");
points.getItemAt(1).format.fill.setSolidColor("D87093");
return ctx.sync();
}).catch(function (error) {
console.log(error);
});
// Table
Excel.run(function (ctx) {
var rows = ctx.workbook.tables.getItem("Table1").rows.load("values");
return ctx.sync()
.then(function () {
var largestRow = 0;
var largestValue = 0;
for (var i = 0; i < rows.items.length; i += 1) {
if (rows.items[i].values[0][1] > largestValue) {
largestRow = i;
largestValue = rows.items[i].values[0][1];
}
}
var largestRowRng = rows.getItemAt(largestRow).getRange();
largestRowRng.format.fill.color = "#ff0000";
})
.then(ctx.sync);
}).catch(function (error) {
console.log(error);
});
// Object.set
Excel.run(ctx => {
const range = ctx.workbook.getSelectedRange();
range.set({
values: [[1]],
format: {
font: {
bold: true
},
fill: {
color: "red"
}
}
});
return ctx.sync();
}).catch(console.log);
}
function test_word() {
// Search
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;
// Setup the search options.
var options = Word.SearchOptions.newObject(context);
options.matchCase = false
// Queue a commmand to search the document.
var searchResults = context.document.body.search('video', options);
// Queue a commmand to load the results.
context.load(searchResults, 'text, font');
// Synchronize the document state by executing the queued-up commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
var results = 'Found count: ' + searchResults.items.length +
'; we highlighted the results.';
// Queue a command to change the font for each found item.
for (var i = 0; i < searchResults.items.length; i += 1) {
searchResults.items[i].font.color = '#FF0000' // Change color to Red
searchResults.items[i].font.highlightColor = '#FFFF00';
searchResults.items[i].font.bold = true;
}
// Synchronize the document state by executing the queued-up commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
console.log(results);
});
});
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
// Content control
Word.run(function (context) {
// Create a proxy range object for the current selection.
var range = context.document.getSelection();
// Queue a commmand to create the content control.
var myContentControl = range.insertContentControl();
myContentControl.tag = 'Customer-Address';
myContentControl.title = 'Enter Customer Address Here:';
myContentControl.style = 'Heading 2';
myContentControl.insertText('One Microsoft Way, Redmond, WA 98052', 'Replace');
myContentControl.cannotEdit = true;
myContentControl.appearance = 'Tags';
// Queue a command to load the id property for the content control you created.
context.load(myContentControl, 'id');
// Synchronize the document state by executing the queued-up commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('Created content control with id: ' + myContentControl.id);
});
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
// Body.insertInlinePictureFromBase64 Word 1.1
Word.run(function (context) {
// Create a proxy body object.
var body = context.document.body;
// Queue a command to insert the image into the document.
var image = body.insertInlinePictureFromBase64('', Word.InsertLocation.start);
// Queue a command to select the image.
image.select();
// Synchronize the document state by executing the queued commands,
// and returning a promise to indicate task completion.
return context.sync()
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
// Body.insertInlinePictureFromBase64 Word 1.2
Word.run((context) => {
// Create a proxy object for the range at the current selection.
var imageRange = context.document.getSelection();
// Load the selected range.
context.load(imageRange, 'text');
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
return context.sync()
.then(() => {
// Queue a command to insert the image into the document.
var insertedImage = imageRange.insertInlinePictureFromBase64('', Word.InsertLocation.replace);
// Queue a command to navigate the UI to the insert picture.
insertedImage.select();
// Queue an indefinite number of commands to insert paragraphs
// based on the number of callouts added to the image.
if (this._calloutNumber > 0) {
var lastParagraph = insertedImage.insertParagraph('Here are your callout descriptions:', Word.InsertLocation.after) as Word.Paragraph;
for (var i = 0; i < this._calloutNumber; i += 1) {
lastParagraph = lastParagraph.insertParagraph((i + 1) + ') [enter callout description].', Word.InsertLocation.after);
}
}
})
// Synchronize the document state by executing the queued commands.
.then(context.sync);
})
.catch((error) => {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
}
async function test_visio() {
const url = "someurl";
try {
const session = new OfficeExtension.EmbeddedSession(url, { id: "embed-iframe", container: document.getElementById("iframeHost") });
await session.init();
await Visio.run(session, async context => {
const eventResult = context.document.onPageLoadComplete.add(async args => {
console.log(Date.now() + ": Page Load Complete Event: " + JSON.stringify(args));
});
await context.sync();
console.log("Success");
});
} catch (error) {
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
}
}
function test_OfficePromise() {
let p1: Promise<any> = Excel.run(async () => { return 10 });
let p2: Promise<any> = new OfficeExtension.Promise(resolve => setTimeout(resolve, 1000));
let p3: Promise<any> = new Office.Promise(resolve => setTimeout(resolve, 1000));
let p4: OfficeExtension.IPromise<any> = new OfficeExtension.Promise(resolve => setTimeout(resolve, 1000));
}
async function test_interfaces() {
await Excel.run(async context => {
let range = context.workbook.getSelectedRange();
range.set({
values: [["Hi"]],
format: {
fill: {
color: "red"
}
}
});
let rangeSettables: Excel.Interfaces.RangeUpdateData = {
values: [["Hi"]],
format: {
fill: {
color: "red"
}
}
};
range.set(rangeSettables);
});
}
async function testResumeExistingObject () {
let range: Excel.Range;
await Excel.run(async context => {
range = context.workbook.getSelectedRange();
await context.sync();
});
await Excel.run(range, async context => {
range.clear();
await context.sync();
});
await Excel.run({delayForCellEdit: true, previousObjects: range}, async context => {
range.clear();
await context.sync();
});
}