I have been trying for hours now to code a simple AppsScript.
When a function on a WebApp server has successfully finished, I want to send the results back to my client (index.html) and call the next function on the server.
For some reason, I cannot call a new server function from within my callback function. The first callback is executed - Alert "Callback1" is shown - but the new server function createSheetsLogfile(selectedLabel)
is not called after.
What am I doing wrong? Is it not possible to call a function on the server from within a callback function?
Thank you very much for your help! Best wishes, Ben
index.html
<button onclick="archiveSBEmails()">Start archiving</button>
<script>
var selectedLabel = "";
function archiveSBEmails(){
selectedLabel = "Testlabel_1";
google.script.run.withSuccessHandler(callbackFolderCreated).createFolder(selectedLabel);
}
function callbackFolderCreated(){
alert("Callback1");
google.script.run.withSuccessHandler(callbackLogfileCreated).createSheetsLogfile(selectedLabel);
}
function callbackLogfileCreated(logfileURL){
alert("Callback2");
google.script.run.withSuccessHandler(callbackBackupCompleted).BackupGmailToDrive(selectedLabel);
}
function callbackBackupCompleted(){
/...
}
</script>
code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function createFolder(selectedLabel){
//function is called from index.html
// some code ...
return true;
}
function createSheetsLogfile(selectedLabel){
//function is called from index.html
//some code...
return logfileURL;
}
function BackupGmailToDrive(selectedLabel){
//function is called from index.html
//some code..
return true;
}
I declared and initialized a global variable outside of any function earlier in the code which was then reset to "" again everytime a function in code.gs was called. To solve the problem, I deleted the global variable and introduced another parameter to be handed over to the functions where needed.