Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
从 Notion 数据库账本中统计支出情况,见 https://blog.imalan.cn/archives/track-your-expense-with-notion/
// ==WidgetScript==
// @name 🐼 本期支出
// @description 从 Notion 数据库中统计支出情况
// @icon system:chart.bar.xaxis
// @iconBgColor #04B921
// @param api {string="https://api.imalan.cn/expense/stat"} API - URL to your metrics data API
// @param token_v2 {string=""} Notion token - 先浏览器登录 Notion,cookie 中名为 token_v2 的字段
// @param db_url {string=""} Database 链接
// @param total_b {string=""} 总支出预算
// @param coi_b {string=""} 控制支出预算
// @param large_b {string=""} 大额支出次数限制
// @param large_th {string=""} 大额支出阈值 - 多少钱算大额?
// @param include {string="食"} 包含标签 - 筛选重点控制支出时要包含的标签,英文逗号间隔
// @param exclude {string="三餐"} 排除标签 - 筛选重点控制支出时要排除的标签,英文逗号间隔
// @version 0.0.1
// ==/WidgetScript==
const globalTextStyle = {
fontDesign: 'rounded',
lineLimit: 1,
}
const metricBlock = (metric, isSmall) => {
return VStack([
Text({text: metric.title}, {...globalTextStyle, fontSize: isSmall ? 18: 14, opacity: 0.5}),
Text({text: metric.value}, {...globalTextStyle, padding: '6 20', fontSize: isSmall ? 28: 24, fontWeight: 'bold'}),
Text({text: metric.subValue}, {...globalTextStyle, fontSize: isSmall ? 16: 13, fontWeight: 'bold', foregroundColor: metric.subValueColor})
])
}
async (parameters, context) => {
if (context.family === 'medium') {
let api = parameters.api + '?token_v2=' + parameters.token_v2 + '&db_url=' + encodeURI(parameters.db_url) +'&total_b=' + parameters.total_b +'&coi_b=' + parameters.coi_b + '&large_b=' + parameters.large_b + '&large_th=' + parameters.large_th + '&include=' + encodeURI(parameters.include) + '&exclude=' + encodeURI(parameters.exclude)
const res = await fetch(api)
const data = JSON.parse(res)
console.log(data)
render(
VStack([
Text({text: data.title}, {...globalTextStyle, fontSize: 14, opacity: 0.6}),
Spacer(),
HStack([
metricBlock(data.metrics[0]),
...(data.metrics[1] ? [metricBlock(data.metrics[1])]: []),
...(data.metrics[2] ? [metricBlock(data.metrics[2])]: []),
], {spacing: 6}),
Spacer(),
Text({text: data.footnote}, {...globalTextStyle, fontSize: 12, opacity: 0.25}),
], {padding: '12 0'})
)
} else {
render(
Text({text: "请使用中型小组件"}, {...globalTextStyle, fontSize: 16, opacity: 0.6})
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment