API & Webhooks AutomationDifficulty: Intermediate⚡ onEdit or Scheduled

Post Instant Slack / Google Chat Webhook Alerts from Sheets

Sends real-time formatted notifications into Slack or Google Chat spaces whenever critical spreadsheet records (e.g. VIP lead, budget threshold) are updated.

Required OAuth Scopes: https://www.googleapis.com/auth/script.external_requests
Code.gs (Apps Script V8 Runtime)
function postWebhookAlert(messageText) {
  const webhookUrl = 'PASTE_YOUR_SLACK_OR_CHAT_WEBHOOK_URL_HERE';
  const payload = JSON.stringify({
    text: '🚨 *GoogleSolution Alert:* ' + messageText
  });
  
  const options = {
    method: 'post',
    contentType: 'application/json',
    payload: payload,
    muteHttpExceptions: true
  };
  
  UrlFetchApp.fetch(webhookUrl, options);
}

function checkBudgetThresholdAndNotify() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Budget');
  const currentSpend = sheet.getRange('B2').getValue();
  const maxLimit = sheet.getRange('B3').getValue();
  
  if (currentSpend > maxLimit) {
    postWebhookAlert('Monthly ad spend ($' + currentSpend + ') has exceeded allocated budget ($' + maxLimit + ')!');
  }
}

Line-by-Line Code Explanation

Line 2-6: Configures incoming webhook URL and constructs JSON notification payload.
Line 8-15: Executes HTTP POST via `UrlFetchApp.fetch` without requiring third-party libraries.
Line 18-24: Example evaluation checking budget thresholds and triggering alert.

Step-by-Step Setup & Deployment

  1. Generate an Incoming Webhook URL in your Slack or Google Chat workspace settings.
  2. Paste the URL into Line 2.
  3. Set an hourly trigger on `checkBudgetThresholdAndNotify`.

Best Practices for Quota & Rate Limit Management

When deploying this automation in Google Workspace, adhere to these operational principles:

  • Batch Reads & Writes: Never execute getValue() or setValue() inside loops. Read ranges into memory in 1 call, process arrays in V8 memory, and commit changes with a single setValues() call.
  • Execution Timeouts: Apps Script terminates scripts exceeding 6 minutes. For multi-thousand row datasets, implement batch pagination with resume timestamps stored in PropertiesService.
  • OAuth Permissions: Scope your manifest (appsscript.json) narrowly to prevent security prompts from alarming external team members.

Frequently Asked Questions

Is Google Apps Script completely free to use?

Yes. Google Apps Script is built into all free Google accounts and Google Workspace editions. There are no server fees or subscription costs for running scripts within daily quotas.

Can I run this script on an automated schedule without keeping my computer on?

Yes. Time-driven triggers execute on Google's cloud infrastructure. Your script will run reliably even when your computer is closed or offline.

More API & Webhooks Automation Snippets