Skip to content

Instantly share code, notes, and snippets.

@bferronato
Forked from danperrout/SELIC.gs
Created September 21, 2022 17:59
Show Gist options
  • Save bferronato/24a72e304fea2185e52c2ee0202f28f8 to your computer and use it in GitHub Desktop.
Save bferronato/24a72e304fea2185e52c2ee0202f28f8 to your computer and use it in GitHub Desktop.
API Função SELIC Google Planilhas (Sheets)
/*
* @return Retorna a taxa anual da SELIC de acordo com a data passada (se não for passada nenhuma data retorna a taxa anual do dia de hoje).
* Fonte: https://www.bcb.gov.br/estabilidadefinanceira/selicdadosdiarios
**/
function SELIC(dataConsulta = new Date()) {
var today = Utilities.formatDate(new Date(), "GMT+3", "dd/MM/yyyy");
if (dataConsulta.length == 0)
dataConsulta = new Date()
dataConsulta = new Date(String(dataConsulta))
dataConsulta = Utilities.formatDate(dataConsulta, "GMT+3", "dd/MM/yyyy")
if (dataConsulta == today)
(dataConsulta) = Utilities.formatDate(new Date((new Date()).getTime() - 1 * (24 * 3600 * 1000)), "GMT+3", "dd/MM/yyyy")
var data = {
"dataInicial": dataConsulta,
"dataFinal": dataConsulta
};
var payload = JSON.stringify(data);
var options = {
"method": "POST",
headers: {
Accept: " application/json, text/plain, */*",
"Content-Type": "application/json",
},
payload: payload,
};
var url = "https://www3.bcb.gov.br/novoselic/rest/taxaSelicApurada/pub/search?page=1&pageSize=20";
try {
let jsondata = UrlFetchApp.fetch(url, options);
let parsedData = JSON.parse(jsondata.getContentText());
console.log(parsedData.registros[0].taxaAnual.toFixed(2));
return parseFloat(parsedData.registros[0].taxaAnual.toFixed(4)) / 100
} catch (e) {
// Logs an ERROR message.
var erro = 'Erro ao obter taxa SELIC: ' + e
console.error(erro);
return erro
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment