Created
May 23, 2017 04:17
-
-
Save alexluecke/a52dedd6c13ab05f4beafee40ce29a64 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 fixCategories() { | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var data = sheet.getDataRange().getValues(); | |
var newData = new Array(); | |
var map = { | |
'Subscriptions': ['netflix', 'google music'], | |
'Gas/Automotive': ['dmv'], | |
'Groceries': ['safeway', 'trader joe', 'new seasons'], | |
'Gym': ['fitness'], | |
}; | |
data.forEach(function(row, i) { | |
var isPayment = new RegExp('payment', 'gi'); | |
if (i === 0) { | |
newData.push(row); | |
return; | |
} | |
if (row[7] && !isPayment.test(row[5])) { | |
row[6] = -row[7]; | |
} else { | |
Object.keys(map).forEach(function(key) { | |
var found = map[key].filter(function(item) { | |
var regex = new RegExp(item, 'gi'); | |
return regex.test(row[4]); | |
}).length > 0; | |
if (found) { | |
row[5] = key; | |
} | |
}); | |
} | |
newData.push(row); | |
}); | |
sheet.clearContents(); | |
sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment