I have multiple function in Google sheet where it transfers data from one spreadsheet to another.
function Transfer(){
Sparkasse();
PayoneerHK();
PayoneerDG();
N26();
Amex();
Airwallex();
Wise();
Paypal(); }
but sometimes there are scenarios where not all of this has data, sometimes just 1 sheet has a data, and when I run this, it results to error and stops the whole script.
how will I able to skip a function if it resulted to error and run the next one?
CodePudding user response:
For each of your 8 function wrap the code in a try {} catch() {} block. That way if there is an error in any one of them it will be caught and simply ignored.
function Transfer(){
Sparkasse();
PayoneerHK();
PayoneerDG();
N26();
Amex();
Airwallex();
Wise();
Paypal();
}
function Sparkasse() {
try {
// your code here
}
catch(err) {
return null;
}
}
function PayoneerHK() {
try {
// your code here
}
catch(err) {
return null;
}
}
Reference
CodePudding user response:
Unfortunately there is no way to catch all the possible errors that might interrupt the script execution, i.e. maximum execution time exceeded, so the best is to do your best to prevent them i.e. by using if statements to run certain parts of your code if the required conditions are present otherwise exit "gracefully" / "elegantly".
Besides doing your best to prevent the errors happen, you could use try..catch statements to handle several run-time exceptions especially those that might not be worthy to spend too much programming time on prevent them to happen.
Since the transfer
function is not getting a response from the functions being called, it might be better to keep all the try..catch in the transfer
function, this way it might be easier to maintain.
You might also find useful to log the error messages, so you can confirm that the error were those that are expected to happen.
function Transfer(){
try {
Sparkasse();
} catch(error) {
console.log(error.message, error.stack);
}
try {
PayoneerHK();
} catch(error) {
console.log(error.message, error.stack);
}
try {
PayoneerDG();
} catch(error) {
console.log(error.message, error.stack);
}
try {
N26();
} catch(error) {
console.log(error.message, error.stack);
}
try {
Amex();
} catch(error) {
console.log(error.message, error.stack);
}
try {
Airwallex();
} catch(error) {
console.log(error.message, error.stack);
}
try {
Wise();
} catch(error) {
console.log(error.message, error.stack);
}
try {
Paypal();
} catch(error) {
console.log(error.message, error.stack);
}
}
Resources