Created
July 7, 2019 19:58
-
-
Save cubedtear/640a2e2615ba304088513eee8418caf4 to your computer and use it in GitHub Desktop.
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
function contieneUnMes_(str) { | |
const meses = ["enero", "febrero", "marzo", "abril", | |
"mayo", "junio", "julio", "agosto", | |
"septiembre", "octubre", "noviembre", "diciembre"]; | |
return meses.indexOf(str.split(" ")[0]) > -1; | |
} | |
function createMonthArray_() { | |
var result = []; | |
for (var a = 0; a<31; a++) result.push(0); | |
return result; | |
} | |
function actualizarInfoDiaria_(duraciones, calorias, diasEjecritados, nuevosDatos) { | |
for (var i = 0; i<duraciones.length; i++) { | |
if (nuevosDatos[i][0].toString().length > 0) { | |
duraciones[i] += parseInt(nuevosDatos[i][0].toString()); | |
calorias[i] += parseInt(nuevosDatos[i][1].toString()); | |
diasEjecritados[i]++; | |
} | |
} | |
} | |
function dividirMedias_(duraciones, calorias, diasEjercitados) { | |
for (var i = 0; i<duraciones.length; i++) { | |
duraciones[i] = duraciones[i] + "/" + diasEjercitados[i]; | |
calorias[i] = "=" + calorias[i] + "/" + diasEjercitados[i]; | |
} | |
} | |
function arrayToColumn_(arr) { | |
var column = []; | |
for (var i = 0; i<arr.length; i++) { | |
column.push([arr[i]]); | |
} | |
return column; | |
} | |
function formularDuracion_(duraciones) { | |
var result = []; | |
for each (var d in duraciones) { | |
result.push("=TIME(0;" + d.toString() + ";0)"); | |
} | |
return result; | |
} | |
function onEdit(e) { | |
const dataRange = "F2:F6"; | |
const dailyRange = "B2:C32"; | |
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
const sheets = spreadsheet.getSheets(); | |
const resultSheet = spreadsheet.getSheetByName("All the datos"); | |
var totalDuracionPorDia = createMonthArray_(); | |
var totalCaloriasPorDia = createMonthArray_(); | |
var diasEjercitadosPorDia = createMonthArray_(); | |
var index = 2; | |
for each (var sheet in sheets) | |
{ | |
if (contieneUnMes_(sheet.getName().toLowerCase())) { | |
var data = sheet.getRange(dataRange).getValues(); | |
data.unshift(sheet.getName()); | |
//console.log(data); | |
resultSheet.getRange(index, 1, 1, 6).setValues([data]); | |
index += 1; | |
actualizarInfoDiaria_(totalDuracionPorDia, totalCaloriasPorDia, diasEjercitadosPorDia, sheet.getRange(dailyRange).getValues()); | |
} | |
} | |
resultSheet.getRange("K2:K32").setNumberFormat("hh:mm:ss").setFormulas(arrayToColumn_(formularDuracion_(totalDuracionPorDia))); | |
resultSheet.getRange("L2:L32").setNumberFormat("0.00").setValues(arrayToColumn_(totalCaloriasPorDia)); | |
dividirMedias_(totalDuracionPorDia, totalCaloriasPorDia, diasEjercitadosPorDia); | |
resultSheet.getRange("I2:I32").setNumberFormat("hh:mm:ss").setFormulas(arrayToColumn_(formularDuracion_(totalDuracionPorDia))); | |
resultSheet.getRange("J2:J32").setNumberFormat("0.00").setValues(arrayToColumn_(totalCaloriasPorDia)); | |
resultSheet.getRange(index, 1, 200, 6).clearContent(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment