Javascript 如何向Google Doc加载项添加接受/拒绝所有建议

Javascript 如何向Google Doc加载项添加接受/拒绝所有建议,javascript,google-apps-script,google-docs,Javascript,Google Apps Script,Google Docs,我正在创建一个带有各种菜单项的GSuite附加组件,希望用户能够接受/拒绝其当前文档中的所有建议 可在以下位置找到执行此操作的客户端javascript代码: 我试图将它以带有两个按钮的提示的形式纳入我的加载项中。以下是应用程序脚本代码: function suggestions() { var suggestions = [HTML as per below] var htmlOutput = HtmlService.createHtmlOutput(suggestions)

我正在创建一个带有各种菜单项的GSuite附加组件,希望用户能够接受/拒绝其当前文档中的所有建议

可在以下位置找到执行此操作的客户端javascript代码:

我试图将它以带有两个按钮的提示的形式纳入我的加载项中。以下是应用程序脚本代码:

function suggestions() {
    var suggestions = [HTML as per below]
    var htmlOutput = HtmlService.createHtmlOutput(suggestions)
    .setWidth(300)
    .setHeight(100);
    ui.showModalDialog(htmlOutput, 'Accept/Reject All Suggestions');
}
以下是html:

        <!DOCTYPE html>
        <html>
          <head>
            <base target="_top">
            <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
          </head>
        <body>
        <form>
        <div class="block" id="button-bar">
          <button class="blue" id="accept-all">Accept All Suggestions</button>
        </div>
        <div class="block" id="button-bar">
          <button class="blue" id="reject-all">Reject All Suggestions</button>
        </div>
        </form>
        <script>
        document.getElementById("accept-all").onclick = accept-all();
        document.getElementById("reject-all").onclick = reject-all();
        function accept-all() { 
         google.script.host.close();
         //below code from https://blog.crozdesk.com/accept-all-changes-in-google-docs-with-one-click/
         var d=document.getElementsByClassName("docos-accept-suggestion"); 
         d = Array.prototype.slice.call(d); 
         d.forEach(function(n){ 
          var e = document.createEvent("MouseEvents"); 
          e.initEvent("click", true, false); 
          n.dispatchEvent(e,true); 
          e = document.createEvent("MouseEvents"); 
          e.initEvent("mousedown", true, false);
          n.dispatchEvent(e,true);
          e = document.createEvent("MouseEvents");
          e.initEvent("mouseup", true, false); 
          n.dispatchEvent(e,true); 
         }); 
        }
        function reject-all() { 
         google.script.host.close();
         //below code from https://blog.crozdesk.com/accept-all-changes-in-google-docs-with-one-click/         
         var d=document.getElementsByClassName("docos-reject-suggestion"); 
         d = Array.prototype.slice.call(d); 
         d.forEach(function(n){ 
          var e = document.createEvent("MouseEvents"); 
          e.initEvent("click", true, false); 
          n.dispatchEvent(e,true); 
          e = document.createEvent("MouseEvents"); 
          e.initEvent("mousedown", true, false);
          n.dispatchEvent(e,true);
          e = document.createEvent("MouseEvents");
          e.initEvent("mouseup", true, false); 
          n.dispatchEvent(e,true); 
         }); 
        }
        </script>
        </body>
        </html>

接受所有建议
拒绝所有建议
document.getElementById(“全部接受”).onclick=accept-all();
document.getElementById(“全部拒绝”).onclick=reject-all();
函数accept-all(){
google.script.host.close();
//以下代码来自https://blog.crozdesk.com/accept-all-changes-in-google-docs-with-one-click/
var d=document.getElementsByClassName(“docos接受建议”);
d=Array.prototype.slice.call(d);
d、 forEach(函数(n){
var e=document.createEvent(“MouseEvents”);
e、 initEvent(“单击”,真,假);
n、 调度事件(e,真);
e=document.createEvent(“MouseEvents”);
e、 initEvent(“mousedown”,true,false);
n、 调度事件(e,真);
e=document.createEvent(“MouseEvents”);
e、 initEvent(“mouseup”,真,假);
n、 调度事件(e,真);
}); 
}
函数reject-all(){
google.script.host.close();
//以下代码来自https://blog.crozdesk.com/accept-all-changes-in-google-docs-with-one-click/         
var d=document.getElementsByClassName(“docos拒绝建议”);
d=Array.prototype.slice.call(d);
d、 forEach(函数(n){
var e=document.createEvent(“MouseEvents”);
e、 initEvent(“单击”,真,假);
n、 调度事件(e,真);
e=document.createEvent(“MouseEvents”);
e、 initEvent(“mousedown”,true,false);
n、 调度事件(e,真);
e=document.createEvent(“MouseEvents”);
e、 initEvent(“mouseup”,真,假);
n、 调度事件(e,真);
}); 
}
html在提示中正确显示,但当我按下“接受所有建议”或“拒绝所有建议”按钮时,浏览器中会打开一个新选项卡,指向:空白页。尽管两个函数中都有“google.script.host.close();”,但提示不会关闭

这是可能的还是我在打一场失败的战斗?谢谢你的指点。如果没有提示(例如,在应用程序脚本函数本身中)有一种更简单的方法来实现这一点,也很高兴听到关于这方面的建议


非常感谢

您是否尝试删除“form”元素?你并不真的需要它,因为没有用户输入。由于按钮被包装在标记中,因此单击它们也会引发表单的“提交”事件,该事件将重定向到“操作”属性中指定的URL。通常将“action”属性设置为“”将重定向到页面本身,但这在GAS中似乎不起作用

显然,GAS生成的表单有默认的“action”属性,您不能像这样覆盖它

您还可以将每个按钮包装在自己的标记中,然后在提交表单后防止重定向,如下所示

window.onload = function(){

var form = document.getElementById('form1');
form1.addEventListener('submit', function(event){

  event.preventDefault();
  //call the function for this form


});

}

在这种情况下,按钮不需要“onclick”处理程序。

谢谢Anton。删除表单元素停止了新选项卡的打开,但现在当我单击按钮时,什么都没有发生-提示甚至没有关闭,这表明它甚至没有达到“google.script.host.close();”的程度。FWIW-您的第二个建议恢复到原来的行为(即打开新选项卡,提示不关闭,没有任何事情发生)。