0

I have a menu item that loads a modeless dialog box which calls an html file that asks the user to click a cell and then click ok. Once the user clicks ok it should run a function in my code.gs file using google.script.run. When I do this on my account everything works seamlessly, however when a user copies the workbook and tries to do, the modeless dialog box opens but when they click ok, the google.script.run part is not working. The "ok" button looks like it is clicked, the dialog box does not close, and nothing else happens.

HTML FILE

    <!DOCTYPE html>
<html>
 <head>
  <!-- Current Version 5.8.21 -->
   <base target="_top">
 </head>
 <body>
   <p>Select Cell for New Step, then Click OK."</p>
   <input type="button" class="button" value="OK" onclick="google.script.run.znewStep();">
 </body>
</html>

then this is the code it is calling:

function znewStep() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getCurrentCell().offset(0, 0).activate();
  spreadsheet.getCurrentCell().offset(-1, 0, 50, 2).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
  spreadsheet.getCurrentCell().offset(0, 0).activate();
  var newStep =  SpreadsheetApp.getUi().prompt("Please enter new step:").getResponseText();
  spreadsheet.getCurrentCell().setValue(newStep);
  spreadsheet.getCurrentCell().offset(1, 0).activate();
};

Like I said everything works fine for me, but when the workbook is copied by others it does not work.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    Welcome to [so]. Please add the textual error message show either on the executions page or on the web browser console. – Rubén May 21 '21 at 01:36
  • Thank you for replying Ruben. When I check the execution log it just shows the showStepDialog as completed (this is the function to open the modeless dialog box), but when I click OK there is nothing on the execution log at all. – Real Pet Behavior - Tara House May 21 '21 at 01:59
  • There is no script that executes the dialog – Cooper May 21 '21 at 02:10
  • 1
    Have you reviewed the web browser console? (this is one of the web browser developer tools) – Rubén May 21 '21 at 02:20
  • 1
    Did the people that you shared it with go into the script editor and authorize the script? – Cooper May 21 '21 at 03:04
  • Did you try adding `"https://www.googleapis.com/auth/script.container.ui"` to the scopes? – Aerials May 21 '21 at 15:29

1 Answers1

0

This script combines the cell selection and the value entry all combined in the html modeless dialog. So you don't need the modal prompt anyfurther.

GS:

If you run it this way then you don't even need the modal prompt they can just enter the data into the html dialog and press ok.

function znewStep(newStep) {
  try {
    var spreadsheet = SpreadsheetApp.getActive();
    spreadsheet.getCurrentCell().offset(0, 0).activate();
    spreadsheet.getCurrentCell().offset(-1, 0, 5, 2).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
    spreadsheet.getCurrentCell().offset(0, 0).activate();
    spreadsheet.getCurrentCell().setValue(newStep);
    spreadsheet.getCurrentCell().offset(1, 0).activate();
    showMyDialog();
  }
  catch (e) {
    SpreadsheetApp.getUi().alert(e);//alerts users to picking too small of a row number 
  }
};


function showMyDialog() {
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutputFromFile('ah1'),'Step');//ah1.html is the name of the file I am using
}

HTML:

<!DOCTYPE html>
<html>
<head>
    <base target="_top">
</head>
<body>
  <p>Select Cell for New Step and enter value then Click OK.</p>
  <input type="text" id="txt1"/>
  <input type="button" class="button" value="OK" onclick="getNewStep();">
  <script>
    function getNewStep() {
      let v = document.getElementById('txt1').value;
      google.script.run.znewStep(v);//send the new value to the znewStep function now
      google.script.host.close();
    }
  </script>
</body>
</html>

But it doesn't really look like you even need the dialog.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • The dialog opens up a pop up to prompt the user to select a cell where they want a new step to be added. Then when they click ok, a pop up opens to ask for what the step should be, then it inserts the new step in the selected cell and shifts all other cells down. Works perfectly in a workbook I own, but when others make a copy it is not working for them. It's the strangest thing I've ever seen happen. – Real Pet Behavior - Tara House May 21 '21 at 02:36
  • Did the people that you shared it with go into the script editor and authorize the script? – Cooper May 21 '21 at 03:03
  • It's bound to the spreadsheet and they did authorize the script (I've also tried with another one of my google accounts with no luck). Every single other feature works just fine. It seems to be both html files that are running the google.script.run.function that are just not working. They can open and see all code as well. – Real Pet Behavior - Tara House May 21 '21 at 03:14
  • I would try just give the code in a text file or send it to them in an email or let them copy it from here and they should be able to enter it and authorize it and it's theirs. – Cooper May 21 '21 at 03:24
  • Works for other users in a browser other than google chrome. Go figure! – Real Pet Behavior - Tara House May 21 '21 at 04:15
  • 1
    I wonder if it has something to do with enabling pop ups? – Cooper May 21 '21 at 05:14