Reputation: 403
I'm automating updates on a google sheet document which comprised from various data sheets. In the Process I'm reading the updates from 'Updates' sheet. and writing them according to different rules to other sheets. Below is my code (main function which is what relevant at the moment):
function mainFunction()
{
//Lock the file while processing is done
var lock = LockService.getScriptLock();
lock.tryLock(15000);
var updatesSheetName ="Updates";
var updatesSheet = SpreadsheetApp.getActive().getSheetByName(updatesSheetName);
if (updatesSheet==null)
{
Logger.log("Retrieve Updates sheet:: Failure");
//TODO : how to break out of execution and report error
}
var updateDataRange = updatesSheet.getDataRange().getValues(); // Read data from 'Updates' row 0 is column names
if (updateDataRange==null)
{
Logger.log("Updates Data retrieve :: Failure");
//TODO : how to break out of execution and report error
}
Logger.log("Retrieve Updates sheet :: Succesful");
Logger.log("Updates Data Matrix retrieve :: Succesful");
var updatesDataToWrite = processData(updateDataRange); // From ExecutionMethod.gs
if (updatesDataToWrite!=null)
{
Logger.log("Processing updates data :: Succesful");
updateDataRange=updatesDataToWrite;
updateDataSheet(updateDataRange,"Updates"); // Write new Updates Matrix to 'Updates' sheet
Logger.log("Clean Updates sheet::succesfuly");
if (updateDataRange.length>1)
{
Logger.log("Not All Updates were processed : Please check Errors");
}
//updateMainSheet(); need to understand logic
}
else if (updatesDataToWrite==null)
{
Logger.log("Processing updates data :: Failure");
}
lock.releaseLock();
}
What I'm trying to figure out is how to recover/stop execution in case of failed reading of data :
updatesSheet==null
How do i stop the execution altogether ? Programmatically? I haven't found anything i can relate/use online or in the google app script reference
Upvotes: 0
Views: 4866
Reputation: 11
I'm not sure about an actual "exit" command, but you can use throw to stop code in it's tracks and generate an error message. Return will exit your current sub-routine without throwing an error message, but the script will continue running if it there is more code after that sub-routine was called. Return does return a value, so you could evaluate that and continue returning your way out of a script.
You can use try and catch to clean up throw errors.
function doThings () {
return ("Success doing things.");
}
function doMoreThings() {
throw ("An error has been thrown doing more things.");
}
function dontDoThis() {
throw ("You won't see this error.");
}
function doAllTheThings () {
try
{
var success = doThings();
doMoreThings();
dontDoThis();
}
catch (error)
{
SpreadsheetApp.getUi().alert(error)
}
SpreadsheetApp.getUi().alert(success)
}
function doItDirty () {
// We will never see the success message because it is shown after a throw happens.
var success = doThings();
doMoreThings();
// this will be the end of the script due to throw in sub-routine.
dontDoThis();
throw (success);
}
Upvotes: 1
Reputation: 1429
If you want to exit out of the function then use return, ie
function someFun () {
if( someCondition ) {
// say you want to stop the whole function, then do this
return
}
// The code below here will not run
}
Upvotes: 0