Created
October 11, 2019 19:26
Star
You must be signed in to star a gist
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() != ""; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Un script Google Apps Script pour importer les tickets de caisse Leclerc reçu par email dans un tableau Google Spreadsheet.
Il est ensuite possible d'analyser ces dépenses avec des tableaux croisés dynamiques ou des graphiques.