本文目录导读:

- Excel 条件格式(无需编程)
- Google Sheets / Excel 公式
- Python 脚本(通用)
- Microsoft Excel VBA 宏
- JavaScript(网页/Node.js)
- SQL 数据库查询
- 自动化发送通知(Python + SMTP)
- 使用建议
要实现库存预警提示,可以根据你使用的工具或平台选择不同的脚本方案,以下提供几种常见场景的脚本示例,从简单到复杂:
Excel 条件格式(无需编程)
这是最基础的方式:
- 选中库存数量列 → 条件格式 → 突出显示单元格规则 → 小于
- 输入预警值(如 10),设置填充颜色
Google Sheets / Excel 公式
=IF(A2<=10, "⚠️ 库存不足", "✅ 库存充足")
或者结合条件格式:
=AND(A2>0, A2<=10)
Python 脚本(通用)
import json
# 示例库存数据
inventory = {
"商品A": 5,
"商品B": 20,
"商品C": 3,
"商品D": 15
}
# 预警阈值
threshold = 10
print("=== 库存预警报告 ===")
for item, qty in inventory.items():
if qty <= threshold:
print(f"⚠️ {item}: 仅剩 {qty} 件,请及时补货!")
else:
print(f"✅ {item}: {qty} 件,库存正常")
Microsoft Excel VBA 宏
Sub StockWarning()
Dim cell As Range
Dim warningThreshold As Integer
warningThreshold = 10
For Each cell In Range("A2:A100") ' 假设库存数据在A列
If cell.Value <= warningThreshold And cell.Value > 0 Then
' 在B列显示警告
cell.Offset(0, 1).Value = "⚠️ 库存不足"
cell.Interior.Color = RGB(255, 200, 200) ' 红色背景
Else
cell.Offset(0, 1).Value = "✅ 充足"
cell.Interior.Color = RGB(200, 255, 200) ' 绿色背景
End If
Next cell
MsgBox "库存检查完成!"
End Sub
JavaScript(网页/Node.js)
// 库存数据
const inventory = [
{ name: "商品A", stock: 5, threshold: 10 },
{ name: "商品B", stock: 20, threshold: 10 },
{ name: "商品C", stock: 3, threshold: 5 }
];
// 预警函数
function checkStock() {
inventory.forEach(item => {
if (item.stock <= item.threshold) {
console.log(`⚠️ ${item.name} 库存不足!当前: ${item.stock}`);
// 可以添加发送邮件、弹窗等操作
} else {
console.log(`✅ ${item.name} 库存正常`);
}
});
}
checkStock();
SQL 数据库查询
-- 查询需要预警的商品
SELECT
product_name,
current_stock,
min_stock_level
FROM products
WHERE current_stock <= min_stock_level
AND is_active = 1;
-- 或者创建存储过程
CREATE PROCEDURE sp_StockWarning()
BEGIN
SELECT
CONCAT('⚠️ ', product_name, ' 库存不足!当前库存: ', current_stock) AS warning_message
FROM products
WHERE current_stock <= min_stock_level;
END;
自动化发送通知(Python + SMTP)
import smtplib
from email.mime.text import MIMEText
def send_warning_email(low_stock_items):
sender = "your-email@gmail.com"
password = "your-password"
receiver = "manager@company.com"
body = "以下商品库存不足:\n\n"
for item in low_stock_items:
body += f"⚠️ {item['name']}: 剩余 {item['stock']}\n"
msg = MIMEText(body)
msg['Subject'] = "库存预警通知"
msg['From'] = sender
msg['To'] = receiver
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
server.login(sender, password)
server.send_message(msg)
使用建议
- 小型店铺/个人:用 Excel 条件格式或公式最方便
- 中型企业:Python 脚本 + 数据库查询
- 电商平台:用 API + 前端实时提醒
- 需要通知:建议加入邮件/短信/钉钉机器人的接口
如果你有具体的工具(如 Excel、Google Sheets、Python、某个 ERP 系统),告诉我,我可以提供更精确的脚本示例!