You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script从Google Sheet单单元格向Discord推送数据报错求助:无法将{}解析为字符串/400 Bad Request

Fixing Your Google Sheets to Discord Webhook Issue

Let's break down what's causing those errors and get your bot sending messages (with role mentions!) correctly.

First, Why You're Getting Those Errors

  1. Initial Error: "Could not interpret \"{}\" as string."
    You were trying to pass a Google Sheets Range object directly to Discord's content field. Discord expects a plain string here, not a spreadsheet range object—so it couldn't parse what you sent.

  2. 400 Bad Request After Modifying Code
    When you switched to var payload = {content:message}, you still had the Range object in the payload. When you JSON.stringify that, it converts the range's internal structure (like its column/row position) into a string, which is not what Discord's API is looking for. Also, your webhook URL was incomplete—you need the full URL with your webhook ID and token, not just the base endpoint.

Corrected Code with Explanations

Here's the fixed version, with comments explaining each change:

function onEdit() { 
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; 
  // Target cell I2 directly, no need to activate or use selection
  var targetCell = sheet.getRange('I2'); 
  // Get the visible text from the cell (use getValue() if you need raw data instead)
  var cellContent = targetCell.getDisplayValue(); 

  // Add your role mention here—use <@&ROLE_ID> format (replace ROLE_ID with your actual role ID)
  // Example: "<@&123456789012345678> Reminder: " + cellContent
  var messageToSend = "<@&你的角色ID> " + cellContent;

  // Replace this with your FULL Discord webhook URL (includes webhook ID and token)
  var discordWebhookUrl = "https://discord.com/api/webhooks/你的WebhookID/你的WebhookToken"; 

  // Now content is a plain string, which Discord accepts
  var payload = JSON.stringify({content: messageToSend}); 
  var params = { 
    method: "POST", 
    payload: payload, 
    muteHttpExceptions : true, 
    contentType: "application/json" 
  }; 
  var response = UrlFetchApp.fetch(discordWebhookUrl, params); 
  Logger.log(response.getContentText()); 
}

Key Changes to Note:

  • Got rid of unnecessary steps: You don't need to activate the range or use getSelection()—just directly fetch the cell's value.
  • Used the right method for content: getDisplayValue() returns the exact text that shows up in your spreadsheet (great for dates, formatted numbers, etc.). If you need the raw underlying value (like a number instead of a formatted string), use targetCell.getValue().toString() instead.
  • Fixed the webhook URL: The base https://discord.com/api/webhooks won't work—you need the full URL from your Discord server's webhook settings.
  • Added role mention support: The <@&ROLE_ID> format tells Discord to mention the specified role. You can find your role ID by enabling Developer Mode in Discord, then right-clicking the role and selecting "Copy ID".

Extra Tips:

  • Add a check to avoid sending empty messages:
    if (cellContent.trim() === "") {
      Logger.log("Cell is empty, skipping message send.");
      return;
    }
    
  • Make sure your webhook has permission to send messages in the target channel and mention roles.

内容的提问来源于stack exchange,提问作者VerbalVerbose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 04:22:56