Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toward colo project accounting #46

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/colo17/UI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// cribbed from : https://developers.google.com/apps-script/guides/menus
// also: https://developers.google.com/apps-script/guides/sheets/functions

function onOpen() {
console.warn('AMBIENT: SpreadsheetApp');
const ui = SpreadsheetApp.getUi();

ui.createMenu('MadMode')
.addItem('Load Colo Invoices', 'LoadColoInvoices')
.addToUi();
// .addSubMenu(
// ui
// .createMenu('Lunch Money')
// .addItem('Load Txs', 'loadLunchMoneyTransactions')
// .addItem('Save Txs', 'saveLunchMoneyTransactions'),
// )
}

function LoadColoInvoices() {
console.warn('AMBIENT: SpreadsheetApp');
SpreadsheetApp.getUi()
.alert('TODO');
}
21 changes: 21 additions & 0 deletions packages/colo17/appsscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"timeZone": "America/Chicago",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Sheets",
"version": "v4",
"serviceId": "sheets"
}
],
"libraries": [
{
"userSymbol": "Cheerio",
"version": "14",
"libraryId": "1ReeQ6WO8kKNxoaA_O0XEQ589cIrRvEBA9qcWpNqdOP17i47u6N9M5Xh0"
}
]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
63 changes: 63 additions & 0 deletions packages/colo17/coloInvoices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const Colo = {
sheet: 'Expenses',
queryRange: 'coloInvoiceQuery',
hd: [
'Date',
'Message Id',
'From',
'Subject',
'Item #',
'Description',
'Amount',
],
};

function tableRecord_($) {
const { fromEntries } = Object;

const [hd, detail] = $('tr', 'table');
const td = [...$('td', detail)];
const entries = [...$('th', hd)].map((th, col) => {
return [$(th).text(), $(td[col]).text()];
})

return fromEntries(entries);
}

function messageDetail_(m) {
const $ = Cheerio.load(m.getBody());
const detail = tableRecord_($);

return [
m.getDate(),
m.getId(),
m.getFrom(),
m.getSubject(),
detail['Item #'],
detail['Description'],
detail['Amount'],
];
}

function LoadColoInvoices() {
console.warn('AMBIENT: SpreadsheetApp');
const doc = SpreadsheetApp.getActive();
const sheet = doc.getSheetByName(Colo.sheet);

const query = doc.getRangeByName(Colo.queryRange).getValue();
console.warn('AMBIENT: GmailApp');
const threads = GmailApp.search(query);
const rows = [];
threads.forEach(thread =>
thread.getMessages().forEach((m, ix) => {
if (ix > 0) {
console.warn('more than 1 thread in message', m);
return;
};
const values = messageDetail_(m);
rows.push(values);
}),
);
sheet.clear();
setRange(sheet, Colo.hd, rows);
}
5 changes: 5 additions & 0 deletions packages/colo17/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"pull": "clasp -P . pull $SCRIPT_ID"
}
}
24 changes: 24 additions & 0 deletions packages/colo17/priceFetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const CoinGecko = {
keyRange: 'priceKey',
priceApi: 'https://pro-api.coingecko.com/api/v3/simple/price'
};

/**
* ack: https://www.coingecko.com/en/api/documentation
*/
function FetchPrice(token='agoric', vs='usd') {
console.warn('AMBIENT: UrlFetchApp');
const { fetch } = UrlFetchApp;
console.warn('AMBIENT: SpreadsheetApp');
const doc = SpreadsheetApp.getActive();
const apiKey = doc.getRangeByName(CoinGecko.keyRange).getValue();

const url = `${CoinGecko.priceApi}?ids=${token}&vs_currencies=${vs}&x_cg_pro_api_key=${apiKey}`;

const resp = fetch(url,
{ headers: {accept: 'application/json'}});
const data = JSON.parse(resp.getContentText());
// console.log(data);
const price = data[token].usd;
return price;
}
4 changes: 4 additions & 0 deletions packages/colo17/sheetTools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function setRange(sheet, hd, rows, hdRow = 1, detailRow = hdRow + 1) {
sheet.getRange(hdRow, 1, 1, hd.length).setValues([hd]);
sheet.getRange(detailRow, 1, rows.length, hd.length).setValues(rows);
}