Google Apps Script从Google Sheet单单元格向Discord推送数据报错求助:无法将{}解析为字符串/400 Bad Request
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
Initial Error:
"Could not interpret \"{}\" as string."
You were trying to pass a Google SheetsRangeobject directly to Discord'scontentfield. Discord expects a plain string here, not a spreadsheet range object—so it couldn't parse what you sent.400 Bad Request After Modifying Code
When you switched tovar payload = {content:message}, you still had theRangeobject in the payload. When youJSON.stringifythat, 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), usetargetCell.getValue().toString()instead. - Fixed the webhook URL: The base
https://discord.com/api/webhookswon'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

