Trying to call a function from the return function of a dialog

119 views
Skip to first unread message

John Savoury

unread,
May 26, 2022, 7:21:34 PM5/26/22
to Google Apps Script Community
function fetchFileParameters(mType) {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var html='<html><head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"><script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script></head>';
    
    html+='<body><br />';

    html+='<input type="hidden" id="hd" value="'+mType+'">';

    html+='<label for="yr">Enter YEAR</label><br />';
    html+='<input type="text" id="yr" /><br /><br />';

    html+='<label for="tm">Enter TERM (eg. Term 2)</label><br />';
    html+='<input type="text" id="tm" /><br />';

    html+=Utilities.formatString('<br /><input type="radio" name="rgroup" id="%s" value="%s" onChange="getSelected();" />%s','id'Number(1),'radio' + Number(1),' Copy <br />');

    html+=Utilities.formatString('<br /><input type="radio" name="rgroup" id="%s" value="%s" onChange="getSelected();" />%s','id'Number(2),'radio' + Number(2),' Move <br />');
    
  html+='<script>';
  html+='function getSelected(){';
    html+='var selected=document.querySelector(\'input[name="rgroup"]:checked\').value;';
    html+='var pYear=document.getElementById("yr").value;';
    html+='var pTerm=document.getElementById("tm").value;';
    html+='var pType=document.getElementById("hd").value;';    
    html+='google.script.run.withSuccessHandler(function(){google.script.host.close();}).displayChoice(selected,pYear,pTerm,pType);}</script></body></html>'
  var ui=HtmlService.createHtmlOutput(html).setHeight(220).setWidth(250);
  SpreadsheetApp.getUi().showModalDialog(ui'Copy/Move Files');  
}

var copyMove;
var yr;
var tm;
var mType;
function displayChoice(selected,pYear,pTerm,pType){    
  copyMove = selected;
  yr = pYear;
  tm = pTerm;
  mType = pType;
  goFurther(mType);
  //if(pType=="Splits") copyFilesByPupilName();
  //if(pType=="Clones") copyFilesToAllPupils();
  
SpreadsheetApp.getUi().alert(mType);
}

The above code works perfectly, but if I uncomment either

if(pType=="Splits") copyFilesByPupilName(); OR
if(pType=="Clones") copyFilesToAllPupils();

the dialog remains open and then nothing happens.  I can run either function by calling it directly, so there are no errors in them, so why can I not call them from within the displayChoice function.

I notice also that if I put a breakpoint in the displayChoice function it is ignored.

Any ideas?

Clark Lind

unread,
May 27, 2022, 9:36:11 PM5/27/22
to Google Apps Script Community
Don't know if it matters, but Apps script can be pretty finicky. 
Try putting brackets around the execution piece:
 if(pType=="Splits") { copyFilesByPupilName() };
 if(pType=="Clones")  {  copyFilesToAllPupils()  }  ;

John Savoury

unread,
May 27, 2022, 11:58:00 PM5/27/22
to Google Apps Script Community
Thanks @cwl
I have tried that but it does not make any difference.  I just want to use the returned variables in the other functions.  I coded that way because the modal dialog doesn't wait for fetchFileParameters to finish running before moving on with the rest of the code before the variables are set.  Everything stops even if I remove the *if* around copyFilesByPupilName function or the other function, yet they run properly without error if called from the menu.

Clark Lind

unread,
May 28, 2022, 8:45:06 AM5/28/22
to Google Apps Script Community
If you can share a sanitized version, that will show the whole context. 
Do the two functions return anything, or just execute and quit? Maybe try adding return at the end of each. It could be displayChoice() is hung up and can't indicate it was successful back to the google.script.run successhandler.
I'm just guessing without seeing the whole thing.

John Savoury

unread,
May 28, 2022, 5:40:33 PM5/28/22
to Google Apps Script Community
Thanks again @cwl
I put in return and now the dialog closes.  I had thought that if there was no return it would just go back to where it came from anyway.  I have changed the code slightly to the following:

var copyMove;
var yr;
var tm;
var mType;
function displayChoice(selectedpYear,pTerm,pType){ 
copyMove = selected;
yr = pYear;
tm = pTerm;
mType = pType;
if(pType=="Splits"copyFilesByPupilName();
//if(pType=="Clones") copyFilesToAllPupils();
SpreadsheetApp.getUi().alert(mType);
return;
}

function processFiles(){
  fetchFileParameters(true);
}

I call processFiles from the menu.  I put a breakpoint on the line if(pType=="Splits"copyFilesByPupilName();, but the code does not stop there, but it does display the alert, and doesn't go to the function copyFilesByPupilName which has the same alert at the top of it and a breakpoint.

The first part of the copyfiles function looks like this:

function copyFilesByPupilName(){ //Splits
  var ui = SpreadsheetApp.getUi();
  ui.alert(mType);
  var YEAR_FOLDER = yr;
  var TERM_FOLDER = tm;
  if(copyMove == "radio2"){
    copyMove = 'Move';
  }else{
    copyMove = 'Copy';
  }     
  var DOCUMENT_FOLDER = "Splits";  
  var mDocID = getFolderIdFromName(DOCUMENT_FOLDER);
  var mYearID = getFolderIdFromName(YEAR_FOLDER);

All the examples I have seen of dialogs just show an alert of the values returned, but I have not seen any example of actually using the value in another function except for putting values into a spreadsheet.

Clark Lind

unread,
May 29, 2022, 8:34:20 AM5/29/22
to Google Apps Script Community
Hard to say. It is always better to be very explicit with Apps script. The Spreadsheet UI may not have it programmed in to return, or it may be working at the browser level or some other weird Google magic.. lol
Reply all
Reply to author
Forward
0 new messages