html popup google.script.run works for me but not others who make a copy of the workbook

246 Views Asked by At

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.

1

There are 1 best solutions below

6
On

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.