2017-08-29 6 views
2

私は様々なメニュー項目を持つGSuiteアドオンを作成しており、ユーザーが現在のDoc内のすべての提案を受け入れるか拒否できるようにしたいと考えています。これを行うには追加する方法Google Docアドオンにすべての提案を受け入れる/拒否する

クライアント側のJavaScriptコードはここで見つけることができます:https://blog.crozdesk.com/accept-all-changes-in-google-docs-with-one-click/

私は2つのボタンと、プロンプトの形で私のアドオンにこれを組み込むために試してみました。ここでAppsスクリプトのコードは次のとおりです。

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> 

HTMLはプロンプト内に正しく提示し、私は「同意するすべての提案」または「拒否すべての提案」のいずれかを押すとされますボタンをクリックすると、ブラウザに新しいタブが開き、https://[many-alphanumerics]-script.googleusercontent.com/userCodeAppPanel?と空白のページが表示されます。 「google.script.host.close();」にもかかわらず、プロンプトが閉じない両方の機能の中にある。

これは可能ですか、敗戦と戦っていますか?任意のポインタに感謝します。プロンプトなしでこれを行う簡単な方法(Apps Script関数自体など)がある場合は、その旨を喜んでお聞きください。

ありがとうございました!

答えて

1

「フォーム」要素を削除しようとしましたか?ユーザーの入力がないので、実際には必要ありません。あなたのボタンは<フォーム>タグで囲まれているので、それらをクリックすると、<フォームの 'action'属性で指定されたURLにリダイレクトされるフォームの 'submit'イベントが発生します。通常、action属性を ""に設定すると、ページ自体にリダイレクトされますが、これはGASでは機能しません。

明らかに、GAS生成フォームにはデフォルトの 'action'属性があり、このように上書きできません。

また、あなたが必要とするこの場合、ボタンの「onclickの」ハンドラはありません。この

window.onload = function(){ 

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

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


}); 

} 

のようにフォームを送信した後にリダイレクト防ぐその後、独自の< form>タグで各ボタンをラップし、可能性があります。

+0

ありがとうございました。フォームの要素を削除すると、新しいタブが開かれなくなりましたが、ボタンをクリックしても何も起こりません。プロンプトが閉じずに "google.script.host.close(); " FWIW - 2番目の提案が元の動作に戻ります(新しいタブが開き、プロンプトが閉じず、何も起こりません)。 – no1knows

関連する問題