If you need to be 100% accurate or handle files with multiple script versions, you should use the REST API. You cannot do this with the basic SpreadsheetApp service; you must enable the Apps Script API in your Google Cloud Project.
The Logic: You can query for projects where the parentId matches your spreadsheet ID.
function getBoundScriptId(spreadsheetId) {
const url = https://script.googleapis.com/v1/projects?parentId=${spreadsheetId};
const options = {
headers: { Authorization: 'Bearer ’ + ScriptApp.getOAuthToken() },
muteHttpExceptions: true
};
const response = UrlFetchApp.fetch(url, options);
const data = JSON.parse(response.getContentText());
return data.projects ? data.projects[0].scriptId : “No bound script found”;
}