Last active
March 30, 2023 07:25
-
-
Save AlanDecode/185b13791b451d4416d9564b9d0621b8 to your computer and use it in GitHub Desktop.
从 Notion 数据库账本中统计支出情况,见 https://blog.imalan.cn/archives/track-your-expense-with-notion/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==WidgetScript== | |
// @name 🐼 本期支出 | |
// @description 从 Notion 数据库中统计支出情况 | |
// @icon system:chart.bar.xaxis | |
// @iconBgColor #04B921 | |
// @param api {string="https://api.imalan.cn/notion_bkp/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 total_exclude {string="排除"} 总支出排除标签 - 筛选总支出时要排除的标签,英文逗号间隔 | |
// @param coi_b {string=""} 控制支出预算 | |
// @param coi_include {string="食"} 重点监控包含标签 - 筛选重点控制支出时要包含的标签,英文逗号间隔 | |
// @param coi_exclude {string="排除"} 重点监控排除标签 - 筛选重点控制支出时要排除的标签,英文逗号间隔 | |
// @param large_b {string=""} 大额支出次数限制 | |
// @param large_th {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 + '&coi_include=' + encodeURI(parameters.coi_include) + '&coi_exclude=' + encodeURI(parameters.coi_exclude) + '&total_exclude=' + encodeURI(parameters.total_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