const sheetId = "1nJqJeDylacvTVs1GWZ3xjpvoj7cy8QfjHPx_PXqNxKs";
const base = `https://docs.google.com/a/google.com/spreadsheets/d/${sheetId}/gviz/tq?`;
const language = "Portuguese";
//const Key = `AIzaSyBSjSKGn5mmiEIRvn9yOwyRgX3qgsaQzqk`;
//const section = "Bebidas";
let qu = "SELECT *";
const query = encodeURIComponent(qu);
//const url = `${base}&tq=${query}&sheet=${section}`;
document.addEventListener('DOMContentLoaded', init);
const container = document.querySelector(".container");
function init() {
populate(".container", "bebidas");
}
function populate(container, section) {
let _container = document.querySelector(container);
let _url = `${base}&tq=${query}&sheet=${section}`;
fetch(_url)
.then(res => res.text())
.then(rep => {
let jsonString = JSON.parse(rep.match(/(?<="table":).*(?=}\);)/g)[0]);
var id = "";
var priceId = "";
jsonString.cols.forEach((x, i) => {
if (x.label.includes(language)) { id = i; }
if (x.label.includes("Preço")) { priceId = i; }
});
console.log(`${id} - ${id + 1} - ${priceId}`);
var _content = "";
jsonString.rows.forEach((x, y) => {
let _val = x['c'];
let productName = _val[id]['v'];
let productDescription = _val[id + 1] != null ? _val[id + 1]['v'] : "";
let productPrice = _val[priceId]['v'];
if (productName != "") {
var _price = new Intl.NumberFormat("pt-BR", { style: 'currency', currency: 'BRL' }).format(parseFloat(productPrice));
_content += `<div class="menu-item">
<div class="item">
<span class="name">${productName}</span>
${productDescription != "" ? `<span class="description">${productDescription}</span>` : ""}
</div>
<span class="price">${_price}</span>
</div>`;
}
});
_content.trim();
_contentFinal = new DOMParser().parseFromString(_content, 'text/html');
_contentFinal.querySelectorAll("body > *").forEach(x => _container.append(x) );
});
}