8
n8n 中文网amn8n.com

使用Google Sheets的自动化药房库存警报(低库存和过期日期)

中级

这是一个Document Extraction, Multimodal AI领域的自动化工作流,包含 7 个节点。主要使用 Code, Cron, Wait, EmailSend, GoogleSheets 等节点。 使用 Google Sheets 的药房库存警报(低库存和过期药品)

前置要求
  • Google Sheets API 凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "YP981T0Pa3Ab6Dwg",
  "meta": {
    "instanceId": "dd69efaf8212c74ad206700d104739d3329588a6f3f8381a46a481f34c9cc281",
    "templateCredsSetupCompleted": true
  },
  "name": "使用Google Sheets的自动化药房库存警报(低库存和过期日期)",
  "tags": [],
  "nodes": [
    {
      "id": "2697ea1c-7dcd-44eb-bf86-b3889ed5025d",
      "name": "每日库存检查",
      "type": "n8n-nodes-base.cron",
      "position": [
        -1000,
        440
      ],
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "hour": 9
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "93f65b9b-ee7f-4343-bea2-afcae41cc399",
      "name": "获取股票数据",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -780,
        440
      ],
      "parameters": {
        "range": "PharmacyInventory!A:E",
        "options": {},
        "sheetId": "{{your_google_sheet_id}}",
        "authentication": "serviceAccount"
      },
      "credentials": {
        "googleApi": {
          "id": "ScSS2KxGQULuPtdy",
          "name": "Google Sheets- test"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "a71b7215-8278-49e1-8b32-74385c65b77c",
      "name": "更新Google Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -120,
        440
      ],
      "parameters": {
        "range": "PharmacyInventory!A:E",
        "options": {},
        "sheetId": "{{your_google_sheet_id}}",
        "operation": "update",
        "authentication": "serviceAccount"
      },
      "credentials": {
        "googleApi": {
          "id": "ScSS2KxGQULuPtdy",
          "name": "Google Sheets- test"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "5697579e-94b6-4fbb-8c66-b009cb7e7aaa",
      "name": "发送邮件警报",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        100,
        440
      ],
      "webhookId": "cd618a9b-1709-42c4-a56a-7e7cb823a6c7",
      "parameters": {
        "text": "=Hello Pharmacist,\n\nThis is an automated notification from the Pharmacy Inventory Monitoring System.\n\nThe following medicines require immediate attention as of {{ $now.format('YYYY-MM-DD') }}:\n\nLOW STOCK ALERTS:\n{{ $json.lowStockList }}\n\nEXPIRY ALERTS (Near expiry or expired):\n{{ $json.expiryList }}\n\nACTION REQUIRED:\n- Please restock medicines with low quantity levels\n- Remove or return expired/near-expiry items to ensure patient safety\n- Update inventory levels in the Google Sheet after taking action\n\nFor questions about inventory management or system updates, please contact the pharmacy operations team.\n\nBest regards,\nAutomated Pharmacy Inventory System\nSunrise Pharmacy Operations\n\n---\nThis is an automated message. Please do not reply to this email.\n",
        "options": {},
        "subject": "Pharmacy Inventory Alert - Low Stock & Expiry Notice - {{ $now.format('YYYY-MM-DD') }}",
        "toEmail": "pharmacist@sunrisepharmacy.com",
        "fromEmail": "inventory-alerts@sunrisepharmacy.com",
        "emailFormat": "text"
      },
      "credentials": {
        "smtp": {
          "id": "G1kyF8cSWTZ4vouN",
          "name": "SMTP -test"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "1d189b60-955c-4f26-ab16-8055166e8415",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1000,
        -40
      ],
      "parameters": {
        "color": 4,
        "width": 1020,
        "height": 280,
        "content": "## 📌 自动化药房库存监控工作流"
      },
      "typeVersion": 1
    },
    {
      "id": "e062c1ab-c264-4eaa-9a9f-91354cc2bd30",
      "name": "等待所有数据",
      "type": "n8n-nodes-base.wait",
      "position": [
        -560,
        440
      ],
      "webhookId": "ff6fa958-553d-4dd6-816d-d69ed73a49fd",
      "parameters": {},
      "typeVersion": 1.1
    },
    {
      "id": "10f4705c-3f93-4190-bb9b-86151ee49834",
      "name": "检查过期日期和低库存",
      "type": "n8n-nodes-base.code",
      "position": [
        -340,
        440
      ],
      "parameters": {
        "jsCode": "// Get all input items from Google Sheets\nconst items = $input.all();\nconst alerts = [];\nconst lowStockItems = [];\nconst expiryItems = [];\n\nfor (const item of items) {\n  const data = item.json;\n\n  // Skip header row or empty entries\n  if (!data.medicine_name || data.medicine_name === 'medicine_name') {\n    continue;\n  }\n\n  // Stock threshold (default 10 units if not specified)\n  const threshold = data.threshold || 10;\n\n  // Expiry warning in days (30 days before expiry)\n  const expiryWarningDays = 30;\n\n  // Check Low Stock\n  if (data.stock_quantity && parseInt(data.stock_quantity) <= threshold) {\n    const lowStockAlert = {\n      medicine_name: data.medicine_name,\n      stock_quantity: data.stock_quantity,\n      threshold: threshold,\n      type: \"Low Stock\",\n      alert_message: `LOW STOCK: ${data.medicine_name} has only ${data.stock_quantity} units remaining (threshold: ${threshold})`\n    };\n    \n    alerts.push(lowStockAlert);\n    lowStockItems.push(`• ${data.medicine_name}: ${data.stock_quantity} units (threshold: ${threshold})`);\n  }\n\n  // Check Expiry Date\n  if (data.expiry_date) {\n    const today = new Date();\n    const expiryDate = new Date(data.expiry_date);\n    const daysLeft = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));\n\n    if (daysLeft <= expiryWarningDays) {\n      const expiryAlert = {\n        medicine_name: data.medicine_name,\n        expiry_date: data.expiry_date,\n        days_left: daysLeft,\n        type: daysLeft > 0 ? \"Near Expiry\" : \"Expired\",\n        alert_message: daysLeft > 0\n          ? `NEAR EXPIRY: ${data.medicine_name} expires in ${daysLeft} days (${data.expiry_date})`\n          : `EXPIRED: ${data.medicine_name} expired ${Math.abs(daysLeft)} days ago (${data.expiry_date})`\n      };\n      \n      alerts.push(expiryAlert);\n      expiryItems.push(daysLeft > 0 \n        ? `• ${data.medicine_name}: Expires in ${daysLeft} days (${data.expiry_date})`\n        : `• ${data.medicine_name}: EXPIRED ${Math.abs(daysLeft)} days ago (${data.expiry_date})`);\n    }\n  }\n}\n\n// Create summary for email\nconst emailSummary = {\n  lowStockList: lowStockItems.length > 0 ? lowStockItems.join('\\n') : 'No low stock items found.',\n  expiryList: expiryItems.length > 0 ? expiryItems.join('\\n') : 'No items near expiry or expired.',\n  totalAlerts: alerts.length,\n  timestamp: new Date().toISOString()\n};\n\n// Return all alerts with email summary\nif (alerts.length > 0) {\n  return [{ json: emailSummary }, ...alerts.map(alert => ({ json: alert }))];\n} else {\n  // Return empty summary if no alerts\n  return [{ json: emailSummary }];\n}"
      },
      "typeVersion": 2
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "89c25bc6-c09d-433d-85ff-88f9baad4581",
  "connections": {
    "Fetch Stock Data": {
      "main": [
        [
          {
            "node": "Wait For All Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Stock Check": {
      "main": [
        [
          {
            "node": "Fetch Stock Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait For All Data": {
      "main": [
        [
          {
            "node": "Check Expiry Date and Low Stock",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Google Sheet": {
      "main": [
        [
          {
            "node": "Send Email Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Expiry Date and Low Stock": {
      "main": [
        [
          {
            "node": "Update Google Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 文档提取, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量7
分类2
节点类型6
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Oneclick AI Squad

Oneclick AI Squad

@oneclick-ai

The AI Squad Initiative is a pioneering effort to build, automate and scale AI-powered workflows using n8n.io. Our mission is to help individuals and businesses integrate AI agents seamlessly into their daily operations from automating tasks and enhancing productivity to creating innovative, intelligent solutions. We design modular, reusable AI workflow templates that empower creators, developers and teams to supercharge their automation with minimal effort and maximum impact.

外部链接
在 n8n.io 查看

分享此工作流