Last active
July 14, 2016 09:00
-
-
Save modalsoul/a542983c7b4f43acad261912f45d7491 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
var refuelingDateCol = 2; | |
var distanceCol = 3; | |
var oilingQuantityCol = 4; | |
var feeCol = 5; | |
var groupingMonthCol = 5; | |
var groupingDistanceCol = 6; | |
var groupingFuelConsumptionCol = 7; | |
var mySheetURL = 'http://your/form/url' | |
function myFunction() { | |
var spreadSheet = SpreadsheetApp.openByUrl(mySheetURL); | |
var sheet = spreadSheet.getSheets()[0]; | |
var lastRow = sheet.getLastRow(); | |
var dataCount = lastRow-1; | |
var list = new Array(); | |
var consumptionList = new Array(); | |
var targetSheet = spreadSheet.getSheets()[1]; | |
for(i=2;i<lastRow+1;i++) { | |
list.push({ | |
"date":sheet.getRange(i, refuelingDateCol).getValue(), | |
"distance":sheet.getRange(i, distanceCol).getValue(), | |
"quantity":sheet.getRange(i, oilingQuantityCol).getValue()}); | |
} | |
var i = 1; | |
// Record fuel consumption per distance. | |
UnderscoreGS._each(UnderscoreGS._tail(list), function(d) { | |
var date = d.date; | |
var fuel = d.quantity; | |
var distance = d.distance - list[i-1].distance; | |
var fuelConsumption = distance/fuel; | |
targetSheet.getRange(i, 1).setValue(date); | |
targetSheet.getRange(i, 2).setValue(distance); | |
targetSheet.getRange(i, 3).setValue(fuelConsumption); | |
consumptionList.push({ | |
"date":date, | |
"distance":distance, | |
"fuel":fuel | |
}); | |
i++; | |
}, this); | |
var group = new Object(); | |
UnderscoreGS._each(consumptionList, function(d){ | |
var yyyyMM = Utilities.formatDate( d.date, 'JST', 'yyyyMM'); | |
if(UnderscoreGS._isEmpty(group[yyyyMM])) { | |
group[yyyyMM] = new Array(); | |
} | |
group[yyyyMM].push(d); | |
}, this); | |
var gi = 1; | |
// Record fuel consumption per month. | |
UnderscoreGS._each(group, function(a){ | |
var date = Utilities.formatDate( a[0].date, 'JST', 'yyyy年M月'); | |
var distance = 0; | |
var fuel = 0; | |
UnderscoreGS._each(a, function(b){ | |
distance += b.distance; | |
fuel += b.fuel; | |
}, this); | |
targetSheet.getRange(gi, groupingMonthCol).setValue(date); | |
targetSheet.getRange(gi, groupingDistanceCol).setValue(distance); | |
targetSheet.getRange(gi, groupingFuelConsumptionCol).setValue(distance/fuel); | |
gi++; | |
}, this); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment