Skip to content

Commit feb5325

Browse files
[All hosts] Add code samples (#2207)
1 parent 9ecd4d7 commit feb5325

File tree

9 files changed

+256
-4
lines changed

9 files changed

+256
-4
lines changed

docs/code-snippets/office-snippets.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,26 @@ Office.CoercionType:enum:
979979
}
980980
});
981981
}
982+
Office.Context:interface:
983+
- |-
984+
// Get the Office host, version, and platform in which the add-in is running.
985+
const contextInfo = Office.context.diagnostics;
986+
console.log("Office application: " + contextInfo.host);
987+
console.log("Office version: " + contextInfo.version);
988+
console.log("Platform: " + contextInfo.platform);
989+
Office.Context#auth:member:
990+
- |-
991+
// Get an access token.
992+
const authContext = Office.context.auth;
993+
authContext.getAccessTokenAsync((result) => {
994+
if (result.status === Office.AsyncResultStatus.Failed) {
995+
console.log("Error obtaining token", result.error);
996+
return;
997+
}
998+
999+
const token = result.value;
1000+
console.log(token);
1001+
});
9821002
Office.Context#contentLanguage:member:
9831003
- |-
9841004
function sayHelloWithContentLanguage() {
@@ -1763,6 +1783,33 @@ Office.DevicePermissionType:enum:
17631783
} else {
17641784
console.log("The add-in isn't running in Excel, Outlook, PowerPoint, or Word.");
17651785
}
1786+
Office.Dialog:interface:
1787+
- |-
1788+
// The following example shows how to open a dialog with a specified size. It also shows
1789+
// how to register a function to handle the message when Office.UI.messageParent() is called
1790+
// in the dialog and how to use that handler to close the dialog.
1791+
1792+
Office.context.ui.displayDialogAsync("https://www.contoso.com/myDialog.html", { height: 30, width: 20 },
1793+
(asyncResult) => {
1794+
const dialog = asyncResult.value;
1795+
dialog.addEventHandler(Office.EventType.DialogMessageReceived, (arg) => {
1796+
dialog.close();
1797+
// Do something to process the message.
1798+
});
1799+
}
1800+
);
1801+
1802+
// The following example does the same thing in TypeScript.
1803+
1804+
Office.context.ui.displayDialogAsync("https://www.contoso.com/myDialog.html", { height: 30, width: 20 },
1805+
(asyncResult: Office.AsyncResult) => {
1806+
const dialog: Office.Dialog = asyncResult.value;
1807+
dialog.addEventHandler(Office.EventType.DialogMessageReceived, (arg: string) => {
1808+
dialog.close();
1809+
// Do something to process the message.
1810+
});
1811+
}
1812+
);
17661813
Office.Dialog#addEventHandler:member(1):
17671814
- |-
17681815
// The following example shows how to open a dialog with a specified size. It also shows
@@ -1833,6 +1880,10 @@ Office.Dialog#messageChild:member(1):
18331880
const messageToDialog = JSON.stringify(currentWorksheet);
18341881
dialog.messageChild(messageToDialog);
18351882
}
1883+
Office.DialogMessageOptions#targetOrigin:member:
1884+
- |-
1885+
// The following example shows how to send a message to the domain of the parent runtime.
1886+
Office.context.ui.messageParent("Some message", { targetOrigin: "https://resource.contoso.com" });
18361887
Office.DialogOptions#height:member:
18371888
- |-
18381889
// The following example shows how to open a dialog with a specified size. It also shows

docs/docs-ref-autogen/office/office/office.context.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ remarks: >-
2323
</strong></td><td> Not supported </td><td> Supported </td><td> Supported </td><td> Not supported </td><td> Not
2424
applicable </td></tr> <tr><td><strong> Word </strong></td><td> Supported </td><td> Supported </td><td> Supported
2525
</td><td> Supported </td><td> Not applicable </td></tr> </table>
26+
27+
28+
#### Examples
29+
30+
31+
```TypeScript
32+
33+
// Get the Office host, version, and platform in which the add-in is running.
34+
35+
const contextInfo = Office.context.diagnostics;
36+
37+
console.log("Office application: " + contextInfo.host);
38+
39+
console.log("Office version: " + contextInfo.version);
40+
41+
console.log("Platform: " + contextInfo.platform);
42+
43+
```
2644
isPreview: false
2745
isDeprecated: false
2846
type: interface
@@ -39,6 +57,24 @@ properties:
3957
content: 'auth: Auth;'
4058
return:
4159
type: '<xref uid="office!Office.Auth:interface" />'
60+
description: |-
61+
62+
63+
#### Examples
64+
65+
```TypeScript
66+
// Get an access token.
67+
const authContext = Office.context.auth;
68+
authContext.getAccessTokenAsync((result) => {
69+
if (result.status === Office.AsyncResultStatus.Failed) {
70+
console.log("Error obtaining token", result.error);
71+
return;
72+
}
73+
74+
const token = result.value;
75+
console.log(token);
76+
});
77+
```
4278
- name: commerceAllowed
4379
uid: 'office!Office.Context#commerceAllowed:member'
4480
package: office!

docs/docs-ref-autogen/office/office/office.dialog.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,45 @@ summary: >-
99
remarks: >-
1010
**Requirement set**:
1111
[DialogAPI](https://learn.microsoft.com/javascript/api/requirement-sets/common/dialog-api-requirement-sets)
12+
13+
14+
#### Examples
15+
16+
17+
```TypeScript
18+
19+
// The following example shows how to open a dialog with a specified size. It also shows
20+
21+
// how to register a function to handle the message when Office.UI.messageParent() is called
22+
23+
// in the dialog and how to use that handler to close the dialog.
24+
25+
26+
Office.context.ui.displayDialogAsync("https://www.contoso.com/myDialog.html", { height: 30, width: 20 },
27+
(asyncResult) => {
28+
const dialog = asyncResult.value;
29+
dialog.addEventHandler(Office.EventType.DialogMessageReceived, (arg) => {
30+
dialog.close();
31+
// Do something to process the message.
32+
});
33+
}
34+
);
35+
36+
37+
// The following example does the same thing in TypeScript.
38+
39+
40+
Office.context.ui.displayDialogAsync("https://www.contoso.com/myDialog.html", { height: 30, width: 20 },
41+
(asyncResult: Office.AsyncResult) => {
42+
const dialog: Office.Dialog = asyncResult.value;
43+
dialog.addEventHandler(Office.EventType.DialogMessageReceived, (arg: string) => {
44+
dialog.close();
45+
// Do something to process the message.
46+
});
47+
}
48+
);
49+
50+
```
1251
isPreview: false
1352
isDeprecated: false
1453
type: interface

docs/docs-ref-autogen/office/office/office.dialogmessageoptions.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@ properties:
2525
content: 'targetOrigin: string;'
2626
return:
2727
type: string
28+
description: |-
29+
30+
31+
#### Examples
32+
33+
```TypeScript
34+
// The following example shows how to send a message to the domain of the parent runtime.
35+
Office.context.ui.messageParent("Some message", { targetOrigin: "https://resource.contoso.com" });
36+
```

docs/docs-ref-autogen/office_release/office/office.context.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ remarks: >-
2323
</strong></td><td> Not supported </td><td> Supported </td><td> Supported </td><td> Not supported </td><td> Not
2424
applicable </td></tr> <tr><td><strong> Word </strong></td><td> Supported </td><td> Supported </td><td> Supported
2525
</td><td> Supported </td><td> Not applicable </td></tr> </table>
26+
27+
28+
#### Examples
29+
30+
31+
```TypeScript
32+
33+
// Get the Office host, version, and platform in which the add-in is running.
34+
35+
const contextInfo = Office.context.diagnostics;
36+
37+
console.log("Office application: " + contextInfo.host);
38+
39+
console.log("Office version: " + contextInfo.version);
40+
41+
console.log("Platform: " + contextInfo.platform);
42+
43+
```
2644
isPreview: false
2745
isDeprecated: false
2846
type: interface

docs/docs-ref-autogen/office_release/office/office.dialog.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,45 @@ summary: >-
99
remarks: >-
1010
**Requirement set**:
1111
[DialogAPI](https://learn.microsoft.com/javascript/api/requirement-sets/common/dialog-api-requirement-sets)
12+
13+
14+
#### Examples
15+
16+
17+
```TypeScript
18+
19+
// The following example shows how to open a dialog with a specified size. It also shows
20+
21+
// how to register a function to handle the message when Office.UI.messageParent() is called
22+
23+
// in the dialog and how to use that handler to close the dialog.
24+
25+
26+
Office.context.ui.displayDialogAsync("https://www.contoso.com/myDialog.html", { height: 30, width: 20 },
27+
(asyncResult) => {
28+
const dialog = asyncResult.value;
29+
dialog.addEventHandler(Office.EventType.DialogMessageReceived, (arg) => {
30+
dialog.close();
31+
// Do something to process the message.
32+
});
33+
}
34+
);
35+
36+
37+
// The following example does the same thing in TypeScript.
38+
39+
40+
Office.context.ui.displayDialogAsync("https://www.contoso.com/myDialog.html", { height: 30, width: 20 },
41+
(asyncResult: Office.AsyncResult) => {
42+
const dialog: Office.Dialog = asyncResult.value;
43+
dialog.addEventHandler(Office.EventType.DialogMessageReceived, (arg: string) => {
44+
dialog.close();
45+
// Do something to process the message.
46+
});
47+
}
48+
);
49+
50+
```
1251
isPreview: false
1352
isDeprecated: false
1453
type: interface

docs/docs-ref-autogen/office_release/office/office.dialogmessageoptions.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@ properties:
2525
content: 'targetOrigin: string;'
2626
return:
2727
type: string
28+
description: |-
29+
30+
31+
#### Examples
32+
33+
```TypeScript
34+
// The following example shows how to send a message to the domain of the parent runtime.
35+
Office.context.ui.messageParent("Some message", { targetOrigin: "https://resource.contoso.com" });
36+
```

generate-docs/API Coverage Report.csv

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6907,8 +6907,8 @@ Office.CoercionType,"SlideRange",EnumField,Good,false
69076907
Office.CoercionType,"Table",EnumField,Good,false
69086908
Office.CoercionType,"Text",EnumField,Good,false
69096909
Office.CoercionType,"XmlSvg",EnumField,Good,false
6910-
Office.Context,N/A,Class,Good,false
6911-
Office.Context,"auth",Property,Fine,false
6910+
Office.Context,N/A,Class,Good,true
6911+
Office.Context,"auth",Property,Fine,true
69126912
Office.Context,"commerceAllowed",Property,Good,true
69136913
Office.Context,"contentLanguage",Property,Good,true
69146914
Office.Context,"diagnostics",Property,Good,true
@@ -6995,13 +6995,13 @@ Office.DevicePermissionType,N/A,Enum,Good,true
69956995
Office.DevicePermissionType,"camera",EnumField,Fine,false
69966996
Office.DevicePermissionType,"geolocation",EnumField,Fine,false
69976997
Office.DevicePermissionType,"microphone",EnumField,Fine,false
6998-
Office.Dialog,N/A,Class,Good,false
6998+
Office.Dialog,N/A,Class,Good,true
69996999
Office.Dialog,"addEventHandler(eventType, handler)",Method,Fine,true
70007000
Office.Dialog,"close()",Method,Good,true
70017001
Office.Dialog,"messageChild(message, messageOptions)",Method,Fine,true
70027002
Office.Dialog,"sendMessage(name)",Method,Poor,false
70037003
Office.DialogMessageOptions,N/A,Class,Good,false
7004-
Office.DialogMessageOptions,"targetOrigin",Property,Good,false
7004+
Office.DialogMessageOptions,"targetOrigin",Property,Good,true
70057005
Office.DialogOptions,N/A,Class,Fine,false
70067006
Office.DialogOptions,"asyncContext",Property,Fine,false
70077007
Office.DialogOptions,"displayInIframe",Property,Good,false

generate-docs/script-inputs/local-repo-snippets.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,26 @@ Office.CoercionType:enum:
25202520
}
25212521
});
25222522
}
2523+
Office.Context:interface:
2524+
- |-
2525+
// Get the Office host, version, and platform in which the add-in is running.
2526+
const contextInfo = Office.context.diagnostics;
2527+
console.log("Office application: " + contextInfo.host);
2528+
console.log("Office version: " + contextInfo.version);
2529+
console.log("Platform: " + contextInfo.platform);
2530+
Office.Context#auth:member:
2531+
- |-
2532+
// Get an access token.
2533+
const authContext = Office.context.auth;
2534+
authContext.getAccessTokenAsync((result) => {
2535+
if (result.status === Office.AsyncResultStatus.Failed) {
2536+
console.log("Error obtaining token", result.error);
2537+
return;
2538+
}
2539+
2540+
const token = result.value;
2541+
console.log(token);
2542+
});
25232543
Office.Context#contentLanguage:member:
25242544
- |-
25252545
function sayHelloWithContentLanguage() {
@@ -3304,6 +3324,33 @@ Office.DevicePermissionType:enum:
33043324
} else {
33053325
console.log("The add-in isn't running in Excel, Outlook, PowerPoint, or Word.");
33063326
}
3327+
Office.Dialog:interface:
3328+
- |-
3329+
// The following example shows how to open a dialog with a specified size. It also shows
3330+
// how to register a function to handle the message when Office.UI.messageParent() is called
3331+
// in the dialog and how to use that handler to close the dialog.
3332+
3333+
Office.context.ui.displayDialogAsync("https://www.contoso.com/myDialog.html", { height: 30, width: 20 },
3334+
(asyncResult) => {
3335+
const dialog = asyncResult.value;
3336+
dialog.addEventHandler(Office.EventType.DialogMessageReceived, (arg) => {
3337+
dialog.close();
3338+
// Do something to process the message.
3339+
});
3340+
}
3341+
);
3342+
3343+
// The following example does the same thing in TypeScript.
3344+
3345+
Office.context.ui.displayDialogAsync("https://www.contoso.com/myDialog.html", { height: 30, width: 20 },
3346+
(asyncResult: Office.AsyncResult) => {
3347+
const dialog: Office.Dialog = asyncResult.value;
3348+
dialog.addEventHandler(Office.EventType.DialogMessageReceived, (arg: string) => {
3349+
dialog.close();
3350+
// Do something to process the message.
3351+
});
3352+
}
3353+
);
33073354
Office.Dialog#addEventHandler:member(1):
33083355
- |-
33093356
// The following example shows how to open a dialog with a specified size. It also shows
@@ -3374,6 +3421,10 @@ Office.Dialog#messageChild:member(1):
33743421
const messageToDialog = JSON.stringify(currentWorksheet);
33753422
dialog.messageChild(messageToDialog);
33763423
}
3424+
Office.DialogMessageOptions#targetOrigin:member:
3425+
- |-
3426+
// The following example shows how to send a message to the domain of the parent runtime.
3427+
Office.context.ui.messageParent("Some message", { targetOrigin: "https://resource.contoso.com" });
33773428
Office.DialogOptions#height:member:
33783429
- |-
33793430
// The following example shows how to open a dialog with a specified size. It also shows

0 commit comments

Comments
 (0)