Jan 23, 2023 02:29 AM
I want to make an automation that runs a script, and in that script I'd like to call an ajax function (either with jQuery somehow or with XMLHttpRequest() etc.) to grab some external data from a Google calendar. Is this possible? I've searched around and can't seem to find any information on it and the script editor says the ajax functions aren't defined, so I'm not sure if/how I can include jQuery if that can help.
Thanks
Solved! Go to Solution.
Jan 23, 2023 07:14 PM
Sorry, Luke, it was my mistake; the Airtable script editor does not support the require() function, so you can't use it to include the jQuery library in your script.
Use the fetch() or XMLHttpRequest() JavaScript built-in functions to make your requests.
Here's an example of how you can use the fetch() function to make a GET request to the Google Calendar API to retrieve events for a specific calendar:
var calendarId = 'primary';
var apiKey = 'YOUR_API_KEY';
fetch(`https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events?key=${apiKey}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => {
// Do something with the data
console.log(data);
})
.catch(error => {
console.error(error);
});
Thank you.
Jan 23, 2023 04:00 AM - edited Jan 23, 2023 04:00 AM
Yes, it is possible. However, there are a few things to consider:
Airtable's script editor does not support jQuery by default, so you would need to include the jQuery library in your script to use its AJAX functions. You can include jQuery by adding the following line at the top of your script:
var $ = require('https://code.jquery.com/jquery-3.6.0.min.js');
I hope it helps. Thank you
Jan 23, 2023 07:53 AM
I tried this, but all I get when I test the script is:
at main on line 5
Jan 23, 2023 09:13 AM
Hey @MrLuke - you can use the fetch method in Automation Run Script Actions to call external APIs.
You cannot require third-party dependencies using `require` in Scripting Extensions or Automation Run Script Actions. You can include npm packages and client-side Javascript with Custom Extensions, though they cannot be triggered by an Automation that you mentioned in your original question.
For your use case, I anticipate auth into Google's APIs may be difficult but probably possible.
And just since you mentioned Google Calendar specifically, are you aware of the native Google Calendar Sync feature? (no code required)
Jan 23, 2023 07:14 PM
Sorry, Luke, it was my mistake; the Airtable script editor does not support the require() function, so you can't use it to include the jQuery library in your script.
Use the fetch() or XMLHttpRequest() JavaScript built-in functions to make your requests.
Here's an example of how you can use the fetch() function to make a GET request to the Google Calendar API to retrieve events for a specific calendar:
var calendarId = 'primary';
var apiKey = 'YOUR_API_KEY';
fetch(`https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events?key=${apiKey}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => {
// Do something with the data
console.log(data);
})
.catch(error => {
console.error(error);
});
Thank you.
Jan 24, 2023 01:53 AM
Thanks for this! The `fetch` method worked perfectly. I had already tried `XMLHttpRequest()` but that didn't work and was treated like it wasn't supported.
Jan 24, 2023 01:56 AM
Thanks, I've got it working with `fetch` now. 🙂
I have tried to the Google Calendar sync, which is great, but we need some additional flexibility/editing which synced tables don't appear to allow or had some limitations on which a custom script should hopefully avoid.