Google Sheets:实现点击按钮发送单元格内容至Slack
Solution
To send the displayed formula result of a specific cell to Slack instead of hardcoded text, modify your script as follows:
Key Adjustment
Use getDisplayValue()—this method returns the visible text of a cell (including formula-calculated results) rather than the underlying formula itself.
Updated Code
function sendNotification() { // Replace with your full Slack webhook URL const url = "https://hooks.slack.com/services/your-complete-webhook-path"; // Retrieve target cell's displayed value const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); const targetSheet = spreadsheet.getSheetByName("Sheet1"); // Replace with your sheet name const cellContent = targetSheet.getRange("A1").getDisplayValue(); // Replace with your cell reference const params = { method: "post", contentType: "application/json", payload: JSON.stringify({ "text": cellContent // Use cell's displayed value as Slack message }) }; const sendMsg = UrlFetchApp.fetch(url, params); const respCode = sendMsg.getResponseCode(); Logger.log(sendMsg); Logger.log(respCode); }
Critical Notes
- Replace
"Sheet1"with the exact name of your spreadsheet tab (check the bottom of your Google Sheet). - Replace
"A1"with the cell reference containing your formula result (e.g.,"C3"). - Ensure your Slack webhook URL is fully completed (your original code had a partial URL).
Function Breakdown
getActiveSpreadsheet()accesses the spreadsheet bound to your script.getSheetByName()targets the specific sheet holding your data.getDisplayValue()pulls the visible text of the cell, bypassing the formula.
内容的提问来源于stack exchange,提问作者JohnnyMelou
相关产品推荐
相关产品推荐

