Skip to content

Instantly share code, notes, and snippets.

@redism
Created April 24, 2016 08:30
Show Gist options
  • Save redism/c1819482a022d1b75b9ac61c7ed80903 to your computer and use it in GitHub Desktop.
Save redism/c1819482a022d1b75b9ac61c7ed80903 to your computer and use it in GitHub Desktop.
Google script custom functions for KRW
function pad0(num) {
return num < 10 ? '0' + num.toString() : num.toString();
}
function getDateStringDaysAgo(days) {
const date = new Date(Date.now() - (days * 24 * 60 * 60 * 1000));
return date.getFullYear() + "-" + pad0(date.getMonth() + 1) + "-" + pad0(date.getDate());
}
/**
* Returns current value of given gold as Korean Won (KRW).
* @param [gram=1] {number} Gold in gram.
* @return {number} Current value in KRW.
* @customFunction
*/
function getCurrentGoldValueInKRW(gram) {
const GramPerOunce = 0.035274;
var response = UrlFetchApp.fetch("https://www.quandl.com/api/v3/datasets/WGC/GOLD_DAILY_KRW.json?start_date=" + getDateStringDaysAgo(20));
const body = JSON.parse(response.getContentText());
const krwPerOunce = body.dataset.data[0][1];
const ret = GramPerOunce * krwPerOunce * (gram || 1);
return ret;
}
/**
* Returns easy to read string of KRW. round off below 10,000 KRW.
* @param krw {number} Korean won to convert into string.
* @return {string}
* @customFunction
*/
function toEasyKRWCurrencyString(krw) {
const bil = Math.floor(krw / 100000000);
const man = Math.floor((krw % 100000000) / 10000);
const msg = [];
bil && msg.push(bil + '억');
man && msg.push(man + '만');
msg.length ? msg.push('원') : msg.push(krw + '원');
return msg.join(' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment