Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Import de tickets de caisse Leclerc
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() != "";
};
}
@youribonnaffe

This comment has been minimized.

Copy link
Owner Author

youribonnaffe commented Oct 11, 2019

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.

@youribonnaffe

This comment has been minimized.

Copy link
Owner Author

youribonnaffe commented Oct 11, 2019

Capture d’écran 2019-10-11 à 21 28 36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.