I am currently running a Google Apps Script which helps me to execute a function automatically once the sheet is open. But I don't want it runs automatically ALWAYS. Sometimes I want NOT TO run the script, and I want to call another function which will kill the automatically running one or all the functions temporary until I Open the file again.
Here is the function is run automatically, and bottom is the function I want to call to kill or stop the onSelectionChange()
function:
function onSelectionChange(e) {
const prop = PropertiesService.getScriptProperties();
const previousSheet = prop.getProperty("previousSheet");
const range = e.range;
//const a1Notation = range.getA1Notation();
const sheetName = range.getSheet().getSheetName();
if (sheetName != previousSheet && sheetName=="Balance Life Today") {
BalanceLifeTodayUpdate();
}else if (sheetName != previousSheet && sheetName=="Values Power Success") {
ValuesPowerSuccessUpdate();
}else if (sheetName != previousSheet && sheetName=="To-Do List") {
ToDoListUpdate();
}
prop.setProperty("previousSheet", sheetName);
}
function stopScript() {
PropertiesService.getScriptProperties().setProperty('STOP');
return 'Kill Signal Issued';
}
CodePudding user response:
Here is an onOpen() that I use to add menu items. I added some commands after that. If the day of the week is Saturday it will not run anything beyond that command.
function onOpen(e) {
var menu = SpreadsheetApp.getUi().createMenu("Test");
menu.addItem("Show Test","showTest");
menu.addItem("Run Test","runTest");
menu.addToUi();
let today = new Date();
today = today.getDay();
if( today === 6 ) return; // if today is Saturday do nothing
// do what ever else you want to do
}
CodePudding user response:
There is an error on
PropertiesService.getScriptProperties().setProperty('STOP');
setProperty
requires two parameters, the property name and the property value.
Also your onSelectChange
function doesn't read this property. You should add a statement to read it. Once this function has the 'STOP' value, you might use a if statement to force the "exit". I.E. you might add the following statement after const prop = PropertiesService.getScriptProperties();
(assuming that the value that the property name is 'STOP'
and the value to exit is 'yes'
.
if(prop.getProperty('STOP') === 'yes') return;
Reference