Of course. Here is your current script, updated to include the ticker-based sheet routing logic.
This version maintains your existing structure and logic for the "Alerts" sheet while adding the new capability to automatically sort each alert into a separate sheet named after its ticker.
Updated Google Apps Script
Replace your current script in the Apps Script editor with this new version.
function doPost(e) {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
try {
const masterSheet = spreadsheet.getSheetByName("Alerts");
if (!masterSheet) {
throw new Error("Sheet named 'Alerts' not found. Please check spelling.");
}
const data = JSON.parse(e.postData.contents);
const tickerName = data.ticker;
if (!tickerName) {
throw new Error("Webhook data is missing the 'ticker' field.");
}
const newRow = [
new Date(),
data.ticker,
data.interval,
data.indicator_type,
'',
'',
'',
''
];
switch(data.indicator_type) {
case 'CTS-Trend':
newRow[4] = data.signal;
break;
case 'CTS-RSI':
newRow[5] = data.signal;
break;
case 'CTS-Stochastic':
newRow[6] = data.signal;
break;
case 'CTS-Exit':
newRow[7] = data.signal;
break;
}
masterSheet.appendRow(newRow);
let tickerSheet = spreadsheet.getSheetByName(tickerName);
if (!tickerSheet) {
tickerSheet = spreadsheet.insertSheet(tickerName);
const headers = [
"Timestamp", "Ticker", "Interval", "Indicator", "Trend", "Confirm", "Entry", "Exit"
];
tickerSheet.appendRow(headers);
tickerSheet.getRange("A1:H1").setFontWeight("bold");
tickerSheet.setFrozenRows(1);
}
tickerSheet.appendRow(newRow);
return ContentService
.createTextOutput(JSON.stringify({ "status": "success", "message": `Data logged to 'Alerts' and '${tickerName}'` }))
.setMimeType(ContentService.MimeType.JSON);
} catch (error) {
Logger.log(error.toString());
return ContentService
.createTextOutput(JSON.stringify({ "status": "error", "message": error.toString() }))
.setMimeType(ContentService.MimeType.JSON);
}
}
What We Changed:
Spreadsheet Variable: The script now gets the entire spreadsheet object at the beginning, allowing it to interact with any sheet, not just "Alerts".
Variable: It now requires the ticker from your TradingView message and will throw an error if it's missing, as it's crucial for the new logic.
Master Log: Your original logic remains intact. The script first posts the formatted row to your main "Alerts" sheet.
Sheet Creation/Discovery: The new section then checks if a sheet named after the ticker already exists.
If not, it creates one, adds a formatted header row, and freezes it.
If it does exist, it simply prepares to write to it.
Append to Ticker Sheet: The script appends the same newRow to the ticker-specific sheet.
Updated Success Message: The response sent back to TradingView now confirms that the data was logged to both the master sheet and the specific ticker sheet.
Required Actions:
Save and Deploy: Save this new script and, most importantly, re-deploy it by going to Deploy > New deployment.
Copy New Webhook URL: This deployment will generate a new webhook URL.
Update in TradingView: You must update all of your TradingView alerts to use this new URL for the changes to take effect.
Comments
Post a Comment