-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (157 loc) · 5.82 KB
/
Copy pathscript.js
File metadata and controls
192 lines (157 loc) · 5.82 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const apiUrl = "https://script.google.com/macros/s/AKfycbwCc-F4XRMqljsKYA4VzoE6AxhoShHnOoUIyTzQQXQ77Qj_NqNGWM75K8uEVrVYBslGMA/exec";
const form = document.getElementById("recordForm");
const recordsContainer = document.getElementById("records");
const monthInput = document.getElementById("month");
const recordsView = document.getElementById("records-view");
const chartView = document.getElementById("report-view");
const showRecordsBtn = document.getElementById("view-records");
const showChartBtn = document.getElementById("view-report");
let rawData = [];
// 讀取資料並儲存
async function fetchData() {
const response = await fetch(apiUrl);
const data = await response.json();
rawData = data.slice(1); // 移除標題列
loadRecords();
}
function loadRecords() {
recordsContainer.innerHTML = "";
let totalAmount = 0;
const selectedMonth = monthInput.value;
for (let [date, category, amount, note] of rawData) {
const recordMonth = date.substring(0, 7);
if (selectedMonth && recordMonth !== selectedMonth) continue;
const recordElement = document.createElement("div");
recordElement.classList.add("record");
const formattedDate = new Date(date).toLocaleDateString("zh-TW", {
year: "numeric",
month: "2-digit",
day: "2-digit",
weekday: "long" // 加上這行會顯示星期幾
});
recordElement.innerHTML = `
<p><strong>日期:</strong>${formattedDate}</p>
<p><strong>類別:</strong>${category}</p>
<p><strong>金額:</strong>${amount}</p>
<p><strong>備註:</strong>${note}</p>
`;
recordsContainer.appendChild(recordElement);
totalAmount += Number(amount);
}
let totalElement = document.getElementById("totalAmount");
if (!totalElement) {
totalElement = document.createElement("div");
totalElement.id = "totalAmount";
recordsContainer.prepend(totalElement);
}
totalElement.innerHTML = `<h3>總支出:${totalAmount} 元</h3>`;
}
// 送出表單時處理的邏輯
form.addEventListener("submit", async (e) => {
e.preventDefault();
const date = document.getElementById("date").value;
const category = document.getElementById("category").value;
const amount = Number(document.getElementById("amount").value);
const note = document.getElementById("note").value;
if (isNaN(amount) || amount < 0) {
alert("請輸入有效的金額");
return;
}
const newRecord = { date, category, amount, note };
await fetch(apiUrl, {
method: "POST",
body: JSON.stringify(newRecord),
headers: { "Content-Type": "application/json" },
mode: "no-cors"
});
form.reset();
alert("記帳成功!(請到 Google Sheets 查看資料)");
setDefaultDate(); // ← 關鍵!設定下一筆預設日期
setTimeout(fetchData, 2000);
});
// 切換視圖
showRecordsBtn.addEventListener("click", () => {
chartView.style.display = "none";
recordsView.style.display = "block";
loadRecords();
});
showChartBtn.addEventListener("click", () => {
recordsView.style.display = "none";
chartView.style.display = "block";
renderChart();
});
monthInput.addEventListener("change", () => {
loadRecords(); // 更新紀錄
renderChart(); // 更新圖表
});
// 渲染圖表
function renderChart() {
const selectedMonth = monthInput.value;
const categoryTotals = {};
let totalAmount = 0;
for (let [date, category, amount] of rawData) {
const recordMonth = date.substring(0, 7);
if (selectedMonth && recordMonth !== selectedMonth) continue;
amount = Number(amount);
totalAmount += amount;
categoryTotals[category] = (categoryTotals[category] || 0) + amount;
}
const ctx = document.getElementById("categoryChart").getContext("2d");
if (window.categoryChartInstance) {
window.categoryChartInstance.destroy();
}
window.categoryChartInstance = new Chart(ctx, {
type: "pie",
data: {
labels: Object.keys(categoryTotals),
datasets: [{
data: Object.values(categoryTotals),
backgroundColor: ["#FFB5A7", "#B5EAD7", "#CBAACB", "#FFDAC1"]
}]
},
options: {
layout: {
padding: {
top: 20,
bottom: 20,
left: 20,
right: 20
}
},
plugins: {
title: {
display: true,
text: `支出分類比例 (${selectedMonth || "全部"})`,
font: {
size: 18
}
},
legend: {
labels: {
font: {
size: 14
},
color: "#333"
}
}
}
}
});
}
function setDefaultDate() {
const now = new Date();
// 取得 UTC 時間 + 8 小時 = 台北時間
const utc = now.getTime() + now.getTimezoneOffset() * 60000;
const taipeiTime = new Date(utc + 8 * 60 * 60000);
const hours = taipeiTime.getHours();
if (hours < 4) {
taipeiTime.setDate(taipeiTime.getDate() - 1);
}
const formattedDate = taipeiTime.toISOString().split("T")[0];
document.getElementById("date").value = formattedDate;
}
// 載入時執行
window.addEventListener("load", () => {
setDefaultDate();
fetchData(); // 資料載入函數
});