function placeOrder(row) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Validate the row number before proceeding
if (!row || row <= 0) {
Logger.log(Invalid row number: ${row}
);
return;
}
Logger.log(Row number provided: ${row}
);
const data = sheet.getRange(row, 1, 1, 8).getValues()[0]; // Fetch data from the selected row
Logger.log(Data fetched from the sheet: ${JSON.stringify(data)}
);
const stockCode = data[1]; // e.g., āNSE:RELIANCEā or āBOM:506879ā
const orderType = data[3]; // LIMIT or MARKET
const transactionType = data[4]; // BUY or SELL
const quantity = data[5];
const price = data[6];
const [exchange, tradingSymbol] = stockCode.split(ā:ā);
Logger.log(Stock Code: ${stockCode}, Exchange: ${exchange}, Trading Symbol: ${tradingSymbol}
);
const apiUrl = āhttps://api.dhan.co/ordersā;
const accessToken = āeyJ0eXAiOiJKV1QiLCJhbGcUxMiJ9.eyJpc3MiOiJkaGFuIiwicGFydG5lcklkIjoiIiwiZXhwIjoxNzI5NTg4NzkwLCJ0NvbnN1bWVyVHlwZSI6IlNFTEYiLCJ3ZWJob29rVXJsIjoiIiwiZGhhbkNsaWVudElkIjoiMTAwMDY3Nzk3MiJ9.sAhBe_AT2pā;
const clientId = ā12345678ā;
const payload = {
dhanClientId: clientId,
correlationId: ${new Date().getTime()}
,
transactionType: transactionType.toUpperCase(),
exchangeSegment: exchange === āNSEā ? āNSE_EQā : āBSE_EQā,
productType: āCNCā,
orderType: orderType,
validity: āDAYā,
tradingSymbol: tradingSymbol,
quantity: quantity,
price: orderType === āLIMITā ? price : undefined,
};
Logger.log(Payload being sent: ${JSON.stringify(payload)}
);
const options = {
method: āPOSTā,
headers: {
āAcceptā: āapplication/jsonā,
āContent-Typeā: āapplication/jsonā,
āaccess-tokenā: accessToken,
},
payload: JSON.stringify(payload),
muteHttpExceptions: true,
};
try {
const response = UrlFetchApp.fetch(apiUrl, options);
const result = JSON.parse(response.getContentText());
Logger.log(Response received: ${response.getContentText()}
);
if (response.getResponseCode() === 200) {
Logger.log(`Order placed successfully: ${JSON.stringify(result)}`);
} else {
Logger.log(`Error placing order: ${result.message}`);
}
} catch (error) {
Logger.log(Request failed: ${error.toString()}
);
}
}
function testPlaceOrder() {
placeOrder(2); // Replace ā2ā with the row number you want to test
}
i have added this in Google Apsscript , While placing Order Getting below Error
Response received: {āerrorCodeā:āINTERNAL_SERVER_ERRORā,āhttpStatusā:āINTERNAL_SERVER_ERRORā,āinternalErrorCodeā:āRS-9005ā,āinternalErrorMessageā:null}
Please guide if any mistake