You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: Excel-custom-functions/AzureFunction/README.md
+30-30
Original file line number
Diff line number
Diff line change
@@ -11,16 +11,16 @@ extensions:
11
11
technologies:
12
12
- Add-ins
13
13
createdDate: 10/2/2019 1:25:00 PM
14
-
description: "You can expand the capabilities of Excel custom functions by integrating with Azure functions."
14
+
description: "You can expand the capabilities of Excel custom functions by integrating with Azure Functions."
15
15
---
16
16
17
-
# Integrate an Azure function with your Excel custom function
17
+
# Integrate an Azure Function with your Excel custom function
18
18
19
-
You can expand the capabilities of Excel custom functions by integrating with Azure functions. An Azure function allows you to move your code to the cloud so it is not visible from the browser, and you can choose additional languages to run in besides JavaScript. Also an Azure function can integrate with other Azure services such as message queues and storage. And you can share the function with other clients.
19
+
You can expand the capabilities of Excel custom functions by integrating with Azure Functions. An Azure Function allows you to move your code to the cloud so it is not visible from the browser, and you can choose additional languages to run in besides JavaScript. Also an Azure Function can integrate with other Azure services such as message queues and storage. And you can share the function with other clients.
20
20
21
-
This sample shows how to move the code from the `Add` custom function into an Azure function.
21
+
This sample shows how to move the code from the `Add` custom function into an Azure Function.
22
22
23
-

23
+

24
24
25
25
## Applies to
26
26
@@ -34,7 +34,7 @@ To set up your development environment:
34
34
2. Install version 2.x of the [Azure Functions Core Tools](https://docs.microsoft.com/azure/azure-functions/functions-run-local#v2). Note: The sample does not use extension bundles so you will install the .NET Core 2.x SDK in a later step.
35
35
3. Install the [C# extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp). You can also search for the C# extension in the VS Code Extensions view (Ctrl + Shift + X) and typing 'C# for Visual Studio Code'. Note: You will be notified to install the .NET Core 3.0 SDK, but this is not needed for this sample.
36
36
4. Install the [Azure Functions extension](https://docs.microsoft.com/azure/azure-functions/functions-create-first-function-vs-code#install-the-azure-functions-extension). You can also search for the Azure Functions extension in the VS Code Extensions view (Ctrl + Shift + X) and typing 'Azure Functions'.
37
-
5. Download and install [.NET Core 2.2](https://dotnet.microsoft.com/download/dotnet-core) which is the required version for Azure functions to build projects correctly.
37
+
5. Download and install [.NET Core 2.2](https://dotnet.microsoft.com/download/dotnet-core) which is the required version for Azure Functions to build projects correctly.
38
38
39
39
You will also need the following:
40
40
@@ -45,7 +45,7 @@ You will also need the following:
45
45
46
46
Solution | Author(s)
47
47
---------|----------
48
-
Azure function integration | David Chesnut (**Microsoft**)
48
+
Azure Function integration | David Chesnut (**Microsoft**)
49
49
50
50
## Version history
51
51
@@ -58,11 +58,11 @@ Version | Date | Comments
58
58
**THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.**
59
59
60
60
----------
61
-
## Try out the Azure function from localhost
61
+
## Try out the Azure Function from localhost
62
62
63
-
This sample moves the `Add` custom function to an Azure function named `AddTwo()`. The Azure function adds the two numbers and returns the result. The next steps will run the sample. If you want to know how it works, jump down to the explanation section.
63
+
This sample moves the `Add` custom function to an Azure Function named `AddTwo()`. The Azure Function adds the two numbers and returns the result. The next steps will run the sample. If you want to know how it works, jump down to the explanation section.
64
64
65
-
### Build and run the Azure function
65
+
### Build and run the Azure Function
66
66
67
67
1. Clone or download this repository to a local project folder.
68
68
2. Start Visual Studio Code and open the **AzureFunction/AzureFunctionProject** folder in the project.
@@ -72,11 +72,11 @@ This sample moves the `Add` custom function to an Azure function named `AddTwo()
This will modify the tasks so that when you run the project it opens port 3000 for CORS. This allows the Office Add-in to call the function in later steps since it will run on port 3000.
75
-
5. From the **Debug** menu, choose **Start Debugging** (F5). The Azure function project will compile and start running.
75
+
5. From the **Debug** menu, choose **Start Debugging** (F5). The Azure Function project will compile and start running.
76
76
>Note: You may see an alert to allow func access through the firewall.
77
-
After running you should see output similar to the following image. It will list the URL you can use to call the Azure function.
78
-

79
-
6. You can test that the Azure function is working by calling it from a browser. Open a browser window and enter the following URL to add two numbers.
77
+
After running you should see output similar to the following image. It will list the URL you can use to call the Azure Function.
78
+

79
+
6. You can test that the Azure Function is working by calling it from a browser. Open a browser window and enter the following URL to add two numbers.
80
80
`http://localhost:7071/api/AddTwo?first=1&second=2`. You should see the result `{ "answer": 3}` returned to the browser.
81
81
82
82
### Build and run the custom function
@@ -101,12 +101,12 @@ This sample moves the `Add` custom function to an Azure function named `AddTwo()
101
101
```command line
102
102
$ npm run start:web
103
103
```
104
-
3. In Excel with the add-in loaded, try out the custom function by entering `=CONTOSO.ADD(1,2)` into a cell. The cell should call the custom function, which then calls the Azure function, and returns the result of 3.
104
+
3. In Excel with the add-in loaded, try out the custom function by entering `=CONTOSO.ADD(1,2)` into a cell. The cell should call the custom function, which then calls the Azure Function, and returns the result of 3.
105
105

106
106
107
107
## Deploy to an Azure Function App
108
108
109
-
You can also deploy and run the Azure function from your own Azure account.
109
+
You can also deploy and run the Azure Function from your own Azure account.
110
110
111
111
1. Start Visual Studio Code and open the **AzureFunction/AzureFunctionProject** folder in the project.
112
112
2. In Visual Studio Code, press F1 to open the command palette. In the command palette, search for and select **Azure Functions: Deploy to function app....** and then choose your subscription.
@@ -127,34 +127,34 @@ You can also deploy and run the Azure function from your own Azure account.
127
127
7. Select the Azure icon on the Activity bar. You should see an Azure Functions area in the Side Bar.
128
128

129
129
8. In the **Azure: Functions** area, expand the new function app under your subscription. Expand **Functions**, right-click **AddTwo**, and then choose **Copy function URL**.
130
-
9. You can test that the Azure function is working by calling it from a browser. Open a browser window and enter the URL you just copied. If you want to add numbers, append the following query string to the URL `?first=1&second=2`. You should see the result `{ "answer": 3}` returned to the browser.
130
+
9. You can test that the Azure Function is working by calling it from a browser. Open a browser window and enter the URL you just copied. If you want to add numbers, append the following query string to the URL `?first=1&second=2`. You should see the result `{ "answer": 3}` returned to the browser.
131
131
132
132
### Add CORS support for the custom function
133
133
134
-
You'll need to enable CORS support for the Office Add-in before it can call the Azure function online.
134
+
You'll need to enable CORS support for the Office Add-in before it can call the Azure Function online.
135
135
136
-
1. Open the Azure portal and sign in. You can do this by selecting the Azure icon on the Activity bar in Visual Studio Code. Then right-click your Azure subscription for the Azure function and choose **Open in Portal**.
136
+
1. Open the Azure portal and sign in. You can do this by selecting the Azure icon on the Activity bar in Visual Studio Code. Then right-click your Azure subscription for the Azure Function and choose **Open in Portal**.
137
137
2. In the Azure portal, select the function app.
138
138
3. Choose **Platform features**.
139
139
4. Choose **CORS** in the **API** section.
140
-
5. Add a new entry in the **Allowed Origins** list for `https://localhost:3000`. This will enable the custom function to call into the Azure function app.
140
+
5. Add a new entry in the **Allowed Origins** list for `https://localhost:3000`. This will enable the custom function to call into the Azure Function app.
141
141
6. Choose **Save**.
142
142
143
143
>**Note:** If you deploy the Excel custom function add-in, be sure to update the Allowed Origins list to support the new domain location.
144
144
145
-
## Update the Excel custom function to call into the Azure function app
145
+
## Update the Excel custom function to call into the Azure Function app
146
146
147
-
Once the Azure function is deployed you can update the custom function to call the cloud version of the Azure function instead of the localhost version.
147
+
Once the Azure Function is deployed you can update the custom function to call the cloud version of the Azure Function instead of the localhost version.
148
148
149
149
1. Start Visual Studio Code and open the **AzureFunction/CustomFunctionProject/src/functions/functions.js** file in the project.
150
150
2. Go to the add function which sets the url string. The code appears as the following:
151
151
```js
152
152
function add(first, second) {
153
-
//If you publish the Azure function online, update the following URL to use the correct URL location.
153
+
//If you publish the Azure Function online, update the following URL to use the correct URL location.
154
154
let url = "http://localhost:7071/api/AddTwo";
155
155
...
156
156
```
157
-
3. In the `let url` assignment replace the localhost URL with the Azure function URL you copied from the previous exercise.
157
+
3. In the `let url` assignment replace the localhost URL with the Azure Function URL you copied from the previous exercise.
158
158
4. Run the following commands to build and start the custom function add-in on Excel on Windows. If the project was still running from a previous step, stop it and close Excel before restarting it.
159
159
```command line
160
160
$ npm run build
@@ -168,13 +168,13 @@ Once the Azure function is deployed you can update the custom function to call t
168
168
$ npm run build
169
169
$ npm run start:web
170
170
```
171
-
2. Try out the custom function by entering `=CONTOSO.ADD(1,2)` into a cell. The cell should call the custom function, which then calls the Azure function, and returns the result of 3.
171
+
2. Try out the custom function by entering `=CONTOSO.ADD(1,2)` into a cell. The cell should call the custom function, which then calls the Azure Function, and returns the result of 3.
172
172
173
173
## Key parts of this sample
174
174
175
-
### The Azure function
175
+
### The Azure Function
176
176
177
-
The Azure function is named `AddTwo()` and is in the **AzureFunctionProject/AddTow.cs** file. First the function needs to get the two parameters `first` and `second`. They might be passed as URL query parameters, or in a JSON body. So the code handles both scenarios. Notice the use of `??` and `?.` operators when retrieving values from the JSON body. It's possible a parameter was not passed and we don't want the code to fail because of a null. So these operators help work around any null parameters. For more information see [?? operator (C# reference)](https://docs.microsoft.com/dotnet/csharp/language-reference/operators/null-coalescing-operator) and [Null-conditional operators ?. and ?[]](https://docs.microsoft.com/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-)
177
+
The Azure Function is named `AddTwo()` and is in the **AzureFunctionProject/AddTow.cs** file. First the function needs to get the two parameters `first` and `second`. They might be passed as URL query parameters, or in a JSON body. So the code handles both scenarios. Notice the use of `??` and `?.` operators when retrieving values from the JSON body. It's possible a parameter was not passed and we don't want the code to fail because of a null. So these operators help work around any null parameters. For more information see [?? operator (C# reference)](https://docs.microsoft.com/dotnet/csharp/language-reference/operators/null-coalescing-operator) and [Null-conditional operators ?. and ?[]](https://docs.microsoft.com/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-)
178
178
179
179
```csharp
180
180
//retrieve parameters if passed on URL. They are passed in string format (convert them later)
@@ -207,11 +207,11 @@ return new OkObjectResult((n1+n2).ToString());
207
207
208
208
### The custom function
209
209
210
-
The Excel custom function is named `Add` and is in the **CustomFunctionProject/src/functions/functions.js** file. The `Add` function calls the `AddTwo` Azure function and just passes along the `first` and `second` parameters. The fetch function is used to call the Azure function, and the returned answer string is returned to Excel.
210
+
The Excel custom function is named `Add` and is in the **CustomFunctionProject/src/functions/functions.js** file. The `Add` function calls the `AddTwo` Azure Function and just passes along the `first` and `second` parameters. The fetch function is used to call the Azure Function, and the returned answer string is returned to Excel.
211
211
212
212
```js
213
213
functionadd(first, second) {
214
-
//If you publish the Azure function online, update the following URL to use the correct URL location.
214
+
//If you publish the Azure Function online, update the following URL to use the correct URL location.
215
215
let url ="http://localhost:7071/api/AddTwo";
216
216
217
217
returnnewPromise(function(resolve,reject){
@@ -242,7 +242,7 @@ function add(first, second) {
242
242
243
243
### AzureFunctionProject notes
244
244
245
-
The Azure function is configured to allow anonymous access which means anyone can call the function if they have the URL. This is only for development purposes. In production code, you should configure [authentication and authorization in Azure App Service](https://docs.microsoft.com/azure/app-service/overview-authentication-authorization), to ensure only authorized users and code can call your Azure functions.
245
+
The Azure Function is configured to allow anonymous access which means anyone can call the function if they have the URL. This is only for development purposes. In production code, you should configure [authentication and authorization in Azure App Service](https://docs.microsoft.com/azure/app-service/overview-authentication-authorization), to ensure only authorized users and code can call your Azure Functions.
0 commit comments