If you had the chance to follow all the previous lessons, then you have already learned all the important operations [CRUD] in Airtable scripting. And if you are enjoying the series, don’t forget to share it with your friends. In these following lessons, I will explain to you various other use cases for Airtable scripting. In this lesson, I will explain how to convert different currencies in an Airtable script.
First, let’s discuss the current base and table on which I am performing currency conversion operations. It’s important, because if you are following me and your table is not the same as mine, then the script won’t work.
Currently, I have a table called “Currencies” and in this table, there are three columns:
So, When I create an entry in this table, I will only add the value to the first column which is “AMOUNT IN USD”. The other remaining values will be filled out automatically by our script.
/* -------------- Selecting the table -------------- */let table = base.getTable('Currencies');
/* -------------- Getting data from API -------------- */let apiResponse = await fetch('https://api.exchangerate.host/latest?base=USD');
/* -------------- USING API DATA -------------- */let data = await apiResponse.json();let conversionRateINR = data.rates.INR;let conversionRateEURO = data.rates.EUR;
/* -------------- Getting current table data -------------- */let result = await table.selectRecordsAsync();
/* -------------- Updating data -------------- */for (let record of result.records) { await table.updateRecordAsync(record, { 'In INR': record.getCellValue('AMOUNT IN USD') * conversionRateINR, 'IN EURO': record.getCellValue('AMOUNT IN USD') * conversionRateEURO, });}
Let’s understand what exactly I am exactly doing in the code above:
Isn’t it quick and simple? Just get the currency data from the API, then use that data to convert and update records. Here we are selecting data based on USD from the API and converting it to INR & EUR.
I hope this was helpful for your Airtable scripting journey and stay connected for more useful learning lessons.
Feel free to contact me for Airtable consulting services by CLICKING HERE