Skip to content

Instantly share code, notes, and snippets.

@youribonnaffe
Created October 11, 2019 19:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save youribonnaffe/a1c5b6b677412d96b3ee449dc02a17b2 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link
Author

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