สอนทำแจ้งเตือนสินค้าใกล้หมดสต๊อก (Stock Control) จาก Google Sheet ไป ที่ Line App : Line Notify Ep.3

3 min read 2 hours ago
Published on Dec 17, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you will learn how to set up notifications for low-stock items using Google Sheets and the Line Notify app. This integration allows you to receive alerts directly on your Line app, helping you manage your inventory effectively. This guide is perfect for business owners looking to streamline their stock control process.

Step 1: Prepare Your Google Sheet

  • Open the Google Sheet provided in the video: Stock Control Google Sheet.
  • Organize your data in a clear format. Ensure you have columns for:
    • Item name
    • Current stock level
    • Minimum stock level (threshold for low-stock alerts)
  • Input your inventory data accordingly.

Step 2: Create a Google Apps Script

  • In your Google Sheet, navigate to Extensions > Apps Script.

  • Delete any code in the script editor and replace it with the following code:

    function checkStock() {
        var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
        var data = sheet.getDataRange().getValues();
        var message = '';
        
        for (var i = 1; i < data.length; i++) {
            var itemName = data[i][0];
            var currentStock = data[i][1];
            var minimumStock = data[i][2];
            
            if (currentStock < minimumStock) {
                message += 'Warning: ' + itemName + ' is low on stock. Current stock: ' + currentStock + '\n';
            }
        }
        
        if (message) {
            sendLineNotify(message);
        }
    }
    
    function sendLineNotify(message) {
        var token = 'YOUR_LINE_NOTIFY_TOKEN';  // Replace with your Line Notify token
        var options = {
            method: 'post',
            contentType: 'application/x-www-form-urlencoded',
            payload: {
                message: message
            },
            headers: {
                Authorization: 'Bearer ' + token
            }
        };
        UrlFetchApp.fetch('https://notify-api.line.me/api/notify', options);
    }
    
  • Replace 'YOUR_LINE_NOTIFY_TOKEN' with your actual Line Notify token.

Step 3: Set Up Your Line Notify Token

  • Go to the Line Notify website and log in to your Line account.
  • Click on "Generate Token" and select the chat where you want to receive notifications.
  • Copy the generated token and paste it into the script in Step 2.

Step 4: Create a Trigger for Automation

  • Back in the Apps Script window, click on the clock icon (Triggers).
  • Click Add Trigger and set it up as follows:
    • Choose which function to run: checkStock
    • Select event source: Time-driven
    • Select type of time based trigger: Choose your preferred frequency (e.g., Daily, Hourly).
  • Save the trigger.

Conclusion

You have successfully set up a low-stock alert system using Google Sheets and Line Notify. By following these steps, you can automatically receive notifications whenever your inventory falls below the minimum threshold. This process helps you maintain optimal stock levels and prevents stockouts.

Next steps include testing the setup by adjusting stock levels in your Google Sheet and ensuring notifications are received in your Line app.