This repository has been archived by the owner on Mar 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoney.js
104 lines (95 loc) · 3.43 KB
/
money.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
window.addEventListener('load', () => {
const table = createZaimSheetTable();
const selectButton = document.createElement('button');
selectButton.classList.add('btn', 'btn-success');
selectButton.textContent = 'スプレッドシートをすべて選択';
selectButton.addEventListener('click', e => {
e.preventDefault();
const range = document.createRange();
range.selectNodeContents(table);
const selection = window.getSelection();
selection.empty();
selection.addRange(range);
});
const toggleGroup = document.createElement('div');
toggleGroup.appendChild(selectButton);
toggleGroup.appendChild(table);
const toggleButton = document.createElement('button');
toggleButton.classList.add('btn', 'btn-success');
toggleButton.textContent = 'スプレッドシート形式';
toggleButton.addEventListener('click', e => {
e.preventDefault();
toggleGroup.classList.toggle('hide');
});
toggleGroup.classList.toggle('hide');
const insertRef = document.getElementById('filter_payment_category_menu');
insertRef.parentNode.insertBefore(toggleGroup, insertRef.nextElementSibling);
insertRef.parentNode.insertBefore(toggleButton, toggleGroup);
});
/**
* @returns {Element} table element
*/
function createZaimSheetTable() {
const table = document.createElement('table');
table.appendChild(createRowWithColumns('th', [
'日付',
'大カテゴリ',
'小カテゴリ',
'金額',
'出金',
'入金',
'お店',
'品目',
'メモ',
]));
document.querySelectorAll('tbody.money-list>tr').forEach(tr => {
const columns = [
applyOrEmpty(tr.querySelector('td:nth-child(3)'), e => parseZaimDateString(e.textContent).toLocaleDateString()),
applyOrEmpty(tr.querySelector('td:nth-child(4)>span'), e => e.getAttribute('data-title')),
applyOrEmpty(tr.querySelector('td:nth-child(4)>a'), e => e.textContent),
applyOrEmpty(tr.querySelector('td:nth-child(5)'), e => e.textContent),
applyOrEmpty(tr.querySelector('td:nth-child(6) img'), e => e.getAttribute('alt')),
applyOrEmpty(tr.querySelector('td:nth-child(7) img'), e => e.getAttribute('alt')),
applyOrEmpty(tr.querySelector('td:nth-child(8) span'), e => e.getAttribute('title')),
applyOrEmpty(tr.querySelector('td:nth-child(9) span'), e => e.getAttribute('title')),
applyOrEmpty(tr.querySelector('td:nth-child(10) span'), e => e.getAttribute('title')),
];
table.appendChild(createRowWithColumns('td', columns));
});
return table;
}
/**
* @param {Element} e
* @param {(e: Element) => string} f
* @returns {string}
*/
function applyOrEmpty(e, f) {
if (!(e instanceof Element)) {
return '';
}
return f(e);
}
/**
* @param {string} columnTagName
* @param {Array<string>} columns
* @returns {Element}
*/
function createRowWithColumns(columnTagName, columns) {
const tr = document.createElement('tr');
columns.forEach(column => {
const c = document.createElement(columnTagName);
c.textContent = column;
tr.appendChild(c);
});
return tr;
}
/**
* @param {string} s
* @returns {Date}
*/
function parseZaimDateString(s) {
const y = new Date().getFullYear();
const p = s.split(/[月日]/);
return new Date(y, p[0] - 1, p[1]);
}