This tutorial explains you how to Suspend Execution of a Google App Script while it is running.
Users frequently asked Mail Merge for the ability to stop the merge process (and the underlying Google Script) after pressing the start button.
Every Google Apps Script has a 5-minute maximum execution time limit, beyond which the script will immediately terminate. If you are manually executing a script from the Google Script Editor, you can click “Cancel” to stop it from running, but if the script is running through an HTML Service-powered web app or as a Google Add-on, this option is not available.
Code to Suspend Execution of a Google App Script:
Here is a little example that will demonstrate how to abruptly Suspend Execution of a Google App Script that is currently running outside of the Script Editor. The concept is to create a property that is activated when the Stop button is pushed. The currently executing script keeps an eye on this property value and pauses if it is set to “STOP”.
The HTML File
<button onclick="start()">Start</button>
<button onclick="stop()">Stop</button>
<script>
function start() {
google.script.run.SuccessHandler(running).startScript();
}
function running(e) {
console.log('Script is running');
}
function stop() {
google.script.run.withSuccessHandler(stopped).stopScript();
}
function stopped() {
console.log('Script has stopped');
}
</script>
The Server (HTML is served as a web app)
function startScript() {
do {
Logger.log('Script running');
Utilities.sleep(5000);
} while (keepRunning());
return 'OK';
}
function keepRunning() {
var status = PropertiesService.getScriptProperties().getProperty('run') || 'OK';
return status === 'OK' ? true : false;
}
function stopScript() {
PropertiesService.getScriptProperties().setProperty('run', 'STOP');
return 'Kill Signal Issued';
}
function doGet(e) {
PropertiesService.getScriptProperties().setProperty('run', 'OK');
return HtmlService.createHtmlOutputFromFile('html').setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
from thetechxp https://ift.tt/ILmXjpJ
via IFTTT
Comments
Post a Comment