Created
October 11, 2019 19:26
-
-
Save youribonnaffe/a1c5b6b677412d96b3ee449dc02a17b2 to your computer and use it in GitHub Desktop.
Import de tickets de caisse Leclerc
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 import() { | |
var threads = GmailApp.search('from:@ticketcaisse.e-leclerc.com'); | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
for (t = 0; t < threads.length; t++) { | |
var message = threads[t].getMessages()[0]; | |
var shoppingList = parseEmail(message); | |
for (i = 0; i < shoppingList.length; i++) { | |
sheet.appendRow(shoppingList[i]); | |
} | |
} | |
} | |
function parseEmail(message) { | |
const CATEGORY_PREFIX = '>>'; | |
const ITEM_LINE_SEPARATOR = " "; | |
var shoppingList = []; | |
var lines = message.getPlainBody().split('\n'); | |
var readingCategory = false; | |
for (i = 0; i < lines.length; i++) { | |
var line = lines[i]; | |
if (line.indexOf('----') == 0) { | |
// end of list of bought items | |
break; | |
} | |
if (line.indexOf(CATEGORY_PREFIX) == 0) { | |
var category = line.replace(CATEGORY_PREFIX, '').trim(); | |
readingCategory = true; | |
} else if (readingCategory) { | |
var items = line.split(ITEM_LINE_SEPARATOR).filter(nonEmptyItem()); | |
var item = items[0].trim(); | |
var price; | |
if (items.length == 1) { | |
// multiple items, read next line | |
i++; | |
line = lines[i]; | |
var items = line.split(ITEM_LINE_SEPARATOR).filter(nonEmptyItem()); | |
price = items[1].trim(); | |
} else { | |
price = items[1].trim(); | |
} | |
price = price.replace('.', ',').trim(); | |
shoppingList.push([message.getDate(), category, item, price]); | |
} | |
} | |
return shoppingList; | |
} | |
function nonEmptyItem() { | |
return function (el) { | |
return el.trim() != ""; | |
}; | |
} | |
Author
youribonnaffe
commented
Oct 11, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment