Reputation: 783
I'm automating some processes within Google Apps Script.
I have created a few functions that are currently independent from each other. However now, I want to regroup them so's they are triggered within the "master" function.
function master(){
// code that imports data from a form and organises it
runMeAfterMaster();
}
function runMeAfterMaster(){
// code that should run after master
}
Both are in the same script file, both work independently but I can't seem to just be able to "invoke" or call my other function within the master one.
Please Help!
Upvotes: 4
Views: 20320
Reputation: 49
I ran into a similar issue when my function names started with lower case letters.
the error I got was: TypeError: randomNumber is not a function
my code:
/*
goal of the script is to highlight project rotators
*/
function HightlightProject()
{
// get random number
var randomNumber = randomNumber();
}
function randomNumber()
{
// get cell from certain spread sheet
var cell = SpreadsheetApp.getActiveSpreadsheet().getRange("fundation rotator!B7");
// random number
var number = Math.floor(Math.random() * 100);
// set random number
cell.setValue(number);
}
if I capitalize randomNumber to RandomNumber, the function names turn pink, and it works.
/*
goal of the script is to highlight project rotators
*/
function HightlightProject()
{
// get random number
var randomNumber = RandomNumber();
}
function RandomNumber()
{
// get cell from certain spread sheet
var cell = SpreadsheetApp.getActiveSpreadsheet().getRange("fundation rotator!B7");
// random number
var number = Math.floor(Math.random() * 100);
// set random number
cell.setValue(number);
}
Upvotes: 1
Reputation: 783
Harold's tips put me on the right path. The path to testing things out.
I placed my code in a code editor (in my case Brackets) and ran it through JSLint. To find any errors.
No errors there, even though the curly bracket at the end of the function name was red. It turns out over 100 lines of code in google apps and the curly bracket turns red. Under 100 lines you're good to go!
Unfortunately i couldn't debug.
So i decided to make it simpler and call both functions within a new one. That didn't work...and I still ignore why.
function MasterOfAll(){
Master();
runAfterMaster();
}
Second thing I did was to simply regroup both code of each function in a new one and call it. That worked.
function MasterOfAll(){
//Code from the Master function.
//Code from the runAfterMasterFunction.
}
Needless to say, I'm not a big fan of this solution (unclear, messy) but it works!
Upvotes: 0
Reputation: 3337
What you wrote is ok runMeAfterMaster look to be invoked, I suppose your problem is elsewhere have you decorated your functions with Logger.log?
eg:
function master(){
// code that imports data from a form and organises it
Logger.log("I'm in master now");
runMeAfterMaster();
Logger.log("still in master but after calling runMeAfterMaster");
}
function runMeAfterMaster(){
Logger.log("I'm in runMeAfterMaster now");
// code that should run after master
Logger.log("getting out of runMeAfterMaster");
}
if you can't use Logger.log because the function is triggered automatically (and you can't have a look at the logs) yoo can replace it by your own loggin function that write everything in a spreadsheet:
function logit(message) {
SpreadsheetApp.openById("SPREADSHEET_ID").getActiveSheet().appendRow([new Date(),message]);
}
then it will become:
function master(){
// code that imports data from a form and organises it
logit("I'm in master now");
runMeAfterMaster();
logit("still in master but after calling runMeAfterMaster");
}
function runMeAfterMaster(){
logit("I'm in runMeAfterMaster now");
// code that should run after master
logit("getting out of runMeAfterMaster");
}
Upvotes: 0