Skip to content

bkper/exchange-app

Repository files navigation

ExchangeApp

A Google Apps Script library to exchange currencies based on updated rates.

clasp npm (scoped)

var value = ExchangeApp.convert(20, 'USD', 'BRL');

The fluent way

var value = ExchangeApp.exchange(20).from('USD').to('BRL').convert();

Default rates endpoint

The default rates endpoint is set latest rates from openexchangerate.org and supports around 200 global currencies, including unofficial, black market and alternative digital currencies, such as Bitcoin, Etherum and so on.

Here is a full list of supported codes.

Rates are updated once per hour. Read the FAQ for more details.

Changing rates endpoint

You can change the currency endpoint if you need to implement your own endpoint to query historical rates, or different precision. The endpoint response JSON format should be:

{
  base: string;
  date: string;
  rates: {
    [key: string]: number;
  }
}

Example:

{
  "base": "EUR",
  "rates": {
    "CAD": 1.565,
    "CHF": 1.1798,
    "GBP": 0.87295,
    "SEK": 10.2983,
    "EUR": 1.092,
    "USD": 1.2234,
  }
}

Any base can be used.

Example

A free and realiable rates endpoint you can use is exchangeratesapi.io, which provides current and historical foreign exchange rates published by the European Central Bank.

To convert 20 USD to BRL based on rates of day 2018-04-08:

ExchangeApp.setRatesEndpoint('https://api.exchangeratesapi.io/2018-04-08', 3600//cache)

var value = ExchangeApp.exchange(20).from('USD').to('BRL').convert();

Setup

This library is already published as an Apps Script, making it easy to include in your project.

To add it to your script, do the following in the Apps Script code editor:

  1. Click on the menu item "Resources > Libraries..."
  2. In the "Add a Library" text box, enter the Script ID "12pPyeoZrmRDHjGnm4brpl-uIr424_bjAtFMjedtr5aJc_Pt7vKg3IGwy" and click the "Select" button.
  3. Choose a version in the dropdown box (usually best to pick the latest version).
  4. Click the "Save" button.

Typescript Definitions for autocomplete:

1) Add the package:
npm i -S @bkper/exchange-app-types

or

yarn add --dev @bkper/exchange-app-types
2) Configure tsconfig.json:
{
    "compilerOptions": {
        "typeRoots" : ["node_modules/@bkper", "node_modules/@types" ]
    }
}

Learn more about @types, typeRoots and types