Created
January 30, 2018 22:22
-
-
Save bruno2ms/77a8e2bbbb170e47ab6fa9a3c2e66351 to your computer and use it in GitHub Desktop.
Parse Google Spreadsheet Vendas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' ', '_'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!