Your issue can be solved by using an onOpen(e)
trigger and by hosting your python script
on Google Cloud.
1. Use the onOpen(e)
trigger
The onOpen(e)
trigger is used to create the menu MY MENU
in your Spreadsheet each time the Spreadsheet opens. Moreover, the submenu item TRIGGER THE PYTHON SCRIPT
will have the function runScript()
associated with it and each time you click it, that function will be run.
The above behavior can be achieved by using this snippet in Apps Script
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('MY MENU')
.addItem('TRIGGER THE PYTHON SCRIPT', 'runScript')
.addToUi();
}
2. Host your Python script on Google Cloud
You should host your Python Script in Google Cloud as a Cloud Function
and run the code.
The runScript()
is the function triggered by TRIGGER THE PYTHON SCRIPT
menu from above and the snippet looks something like this.
function runScript() {
var params = {
'method': 'post',
'headers': {
'contentType': 'application/json',
'payload': '{"name":"Name"}'
}
};
var pyScript = UrlFetchApp.fetch('https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/FUNCTION_NAME', params);
}
Note: You might have to change the params
based on your script and your desired actions.
Reference
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…