Skip to content

Instantly share code, notes, and snippets.

@bruno2ms
Created January 30, 2018 22:22
Show Gist options
  • Save bruno2ms/77a8e2bbbb170e47ab6fa9a3c2e66351 to your computer and use it in GitHub Desktop.
Save bruno2ms/77a8e2bbbb170e47ab6fa9a3c2e66351 to your computer and use it in GitHub Desktop.
Parse Google Spreadsheet Vendas
var ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
var entries = [{
name : "Export JSON",
functionName : "exportJSON"
}];
ss.addMenu("Export JSON", entries);
};
function exportJSON() {
var app = UiApp.createApplication().setTitle('Resultado do parse de JSON!');
var textArea = app.createTextArea();
textArea.setValue(makeJson());
app.add(textArea);
textArea.setSize("100%", "100%");
SpreadsheetApp.getActiveSpreadsheet().show(app);
};
function makeJson() {
var vendas = [];
var sheet = ss.getActiveSheet();
// var sheets = ss.getSheets();
// for (var i = 0; i < sheets.length; i++) {
// vendas = vendas.concat(getSheetData(sheets[i]));
// }
vendas = vendas.concat(getSheetData(sheet));
return JSON.stringify(vendas);
}
function getSheetData(sheet) {
var vendas = [];
var range = sheet.getRange('A2:N33');
var values = range.getValues();
var colunas = values[0].length,
linhas = values.length;
for (var i = 2; i < values.length; i++) {
var text = "";
var row = values[i];
var data = row[0];
for (var j = 1; j < row.length; j++) {
if (row[j]) {
vendas.push({
data: data,
vendedor: getVendedor(values, j),
tipoVenda: getTipoVenda(values, j),
value: row[j]
});
}
}
}
return vendas;
}
function getTipoVenda(values, coluna) {
return values[1][coluna].toLowerCase().replace(' ', '_');;
}
function getVendedor(values, coluna) {
var header = "";
if (coluna === 0)
header = "DATA";
else if (values[0][coluna] != "")
header = values[0][coluna];
else if (values[0][coluna + 1] != "")
header = values[0][coluna + 1];
else if (values[0][coluna - 1] != "")
header = values[0][coluna - 1];
return header.toLowerCase().replace(' ', '_');
}
@gagustavo
Copy link

Nice!

@herberthamaral
Copy link

:D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment