Skip to content

Instantly share code, notes, and snippets.

@hobru
Created November 23, 2019 13:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hobru/e25ce0f58403c1b8d31a443b2b2adfdb to your computer and use it in GitHub Desktop.
Save hobru/e25ce0f58403c1b8d31a443b2b2adfdb to your computer and use it in GitHub Desktop.
Calling a sample Service from OData.org
name: Simple OData Connect
description: Calling a sample Service from OData.org
host: EXCEL
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(getData));
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
async function getData() {
const url = "https://services.odata.org/V4/(S(vlyeplehfrwjmggz5bkdsawe))/TripPinServiceRW/Airports";
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
const myInit = {
method: "GET",
headers: myHeaders
};
const myRequest = new Request(url, myInit);
const response = await fetch(myRequest);
//Expect that status code is in 200-299 range
if (!response.ok) {
throw new Error(response.statusText);
}
const jsonResponse = await response.json();
console.log("REST call successful!");
console.log("Response: " + jsonResponse.value[0].Name);
await Excel.run(async (context) => {
var Name = context.workbook.worksheets.getActiveWorksheet().getRange("D7");
var IataCode = context.workbook.worksheets.getActiveWorksheet().getRange("D8");
var LocationAdr = context.workbook.worksheets.getActiveWorksheet().getRange("D9");
Name.values = jsonResponse.value[0].Name;
IataCode.values = jsonResponse.value[0].IataCode;
LocationAdr.values = jsonResponse.value[0].Location.Address;
});
return jsonResponse;
}
language: typescript
template:
content: |
<button id="run" class="ms-Button">
<span class="ms-Button-label">Run</span>
</button>
language: html
style:
content: |-
section.samples {
margin-top: 20px;
}
section.samples .ms-Button, section.setup .ms-Button {
display: block;
margin-bottom: 5px;
margin-left: 20px;
min-width: 80px;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
core-js@2.4.1/client/core.min.js
@types/core-js
jquery@3.1.1
@types/jquery@3.3.1
@ShellyShulei
Copy link

Hi, when I run this code shows the error below:

ReferenceError {description: "'Headers' is undefined", number: -2146823279, stack: "ReferenceError: 'Headers' is undefinedat Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:112:21)at step (https://script-lab-runner.azureedge.net/?backButton=true:76:13)at Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:57:46)at Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:51:9)at Promise (https://unpkg.com/core-js@2.4.1/client/core.min.js:8:11235)at Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:47:5)at getData (https://script-lab-runner.azureedge.net/?backButton=true:105:5)at Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:90:21)at step (https://script-lab-runner.azureedge.net/?backButton=true:76:13)at Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:57:46)"}

description: "'Headers' is undefined"

message: "'Headers' is undefined"

number: -2146823279

stack: "ReferenceError: 'Headers' is undefinedat Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:112:21)at step (https://script-lab-runner.azureedge.net/?backButton=true:76:13)at Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:57:46)at Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:51:9)at Promise (https://unpkg.com/core-js@2.4.1/client/core.min.js:8:11235)at Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:47:5)at getData (https://script-lab-runner.azureedge.net/?backButton=true:105:5)at Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:90:21)at step (https://script-lab-runner.azureedge.net/?backButton=true:76:13)at Anonymous function (https://script-lab-runner.azureedge.net/?backButton=true:57:46)"

▶__proto__: ReferenceError

@hobru
Copy link
Author

hobru commented Dec 7, 2019

Let me check. I have to admit that I never tried loading it into a fresh Excel-sheet

@hobru
Copy link
Author

hobru commented Dec 8, 2019

Did you change anything in the code? I just tried this from a different Excel and it worked just fine. After opening Script Lab go to Import -> Enter the GistId and click on Import -> Run.
Can you check under the Libraries tab? Do you see the jquery reference there?

@ShellyShulei
Copy link

ShellyShulei commented Dec 8, 2019 via email

@hobru
Copy link
Author

hobru commented Aug 30, 2020

There is a parallel discussion on https://stackoverflow.com/questions/61499584/how-can-i-use-fetch-to-call-an-api-in-script-lab. For now, can you check in Excel Online? Apparently there is an issue with some Excel Windows versions.

@kk221
Copy link

kk221 commented Nov 9, 2020

Hey Hobru
This is a very useful tutorial to fetch the data from a website, with your original code, it works well for me.
I'm wondering as I need to call the API by an API key, I tried as followed but showed error.

// Get Data from EF by API key async function getData() { const url = "https://api.eflow.team/v1"; const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/json"); myHeaders.append("X-Eflow-API-Key","GwkvD80gSi6nTqOyPA6NnQ"); const myInit = { method: "POST", headers: myHeaders}; const myRequest = new Request(url, myInit); const response = await fetch(myRequest); const jsonResponse = await response.json(); //Expect that status code is in 200-299 range if (!response.ok) { throw new Error(response.statusText); }

Error:

Error {} message: "" stack: "Error at <anonymous>:84:31 at step (<anonymous>:32:23) at Object.next (<anonymous>:13:53) at fulfilled (<anonymous>:4:58)" __proto__: Error

Can you please help me out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment