0

我有一个菜单项,它加载一个无模式对话框,该对话框调用一个 html 文件,该文件要求用户单击一个单元格,然后单击确定。一旦用户单击确定,它应该使用 google.script.run 在我的 code.gs 文件中运行一个函数。当我在我的帐户上执行此操作时,一切都可以无缝运行,但是当用户复制工作簿并尝试执行此操作时,会打开无模式对话框,但是当他们单击确定时,google.script.run 部分不起作用。“确定”按钮看起来像是被点击了,对话框没有关闭,没有其他任何事情发生。

文件

    <!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>

那么这是它正在调用的代码:

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();
};

就像我说的一切对我来说都很好,但是当工作簿被其他人复制时,它就不起作用了。

4

1 回答 1

0

此脚本将单元格选择和值条目组合在 html 无模式对话框中。因此,您不再需要模态提示。

GS:

如果您以这种方式运行它,那么您甚至不需要模式提示,他们只需将数据输入到 html 对话框中并按确定即可。

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>

但看起来你甚至不需要对话框。

于 2021-05-21T02:17:18.947 回答