Hi,
SUMMARY
I wrote an Office Script and it is running fine when you manually run it on Excel Online.
However, when I create a flow and run it, it gives me a fetch undefined error.
Do I need to require a fetch? Do I need to add another line of code?
Or is this a bug in Power Automate and Office Script connection?
DETAILS
The script I coded is to retrieve data from an external API. I got no problem accessing and retrieving the data when running the script manually on Excel Online.
Response from the API GET request is successful...

Script completes...

I also made sure to catch the error when requesting from the API source. This is my Office Script code which causes the error "fetch is undefined."
async function listItems(_url: string, _page? : number): Promise<Response> {
const page = (_page) ? '&page=' + _page.toString() : '';
try {
//Note: This works when running it manually on Excel Online
const response = await fetch(encodeURI(_url + page), {
method: 'GET',
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${theCog}`
}
}
);
//Power Automate flow doesnt even reach here, which means the error occurs on fetch
console.log(`Request URL: ${response.url}`);
console.log(`Response Status: ${response.status} ${response.statusText}`);
return response;
} catch (e) {
console.log('Fetch Error: ', e);
throw new Error('HTTP Bad Request | Reason: ' + e);
}
}
This is the output log from my flow.
{
"message": "We were unable to run the script. Please try again.\nRuntime error: HTTP Bad Request | Reason: ReferenceError: fetch is not defined\r\nclientRequestId: <redacted>",
"logs": [
"[2021-06-16T14:42:43.1160Z] Script has started...",
"[2021-06-16T14:42:43.5380Z] Folder ID: 19<redacted>68(BICOL)",
"[2021-06-16T14:42:43.5530Z] Retrieving data from API source...",
"[2021-06-16T14:42:43.5840Z] Fetch Error: "
]
}
My flow is pretty simple and my script DO NOT require additional parameters.
What is missing here? I welcome any suggestion or solutions. 😀