Search code examples
javascriptgoogle-apps-scriptweb-applicationsmodal-dialogclipboard

Add content of showModalDialog() to the clipboard Google Script


I have formatted data being added to a Modal Dialog when I click a button

I want to the content of the showModalDialog() to be automatically added to the clipboard when I click the button as well

The modal is being generated with the below code, and temp is the output I want added to the clipboard

//Output to Html
 var htmlOutput = HtmlService
              .createHtmlOutput(temp)
              .setSandboxMode(HtmlService.SandboxMode.IFRAME)
              .setWidth(600)
              .setHeight(500);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Filter OptionList Maker');

Edit; Ok, I guess maybe the Modal Dialog might be beside-the-point and the proper question might be how to add the formatted string temp to the clipboard

Here is an example of what I mean by formatted string

filter {
  target: element;
  as: dropdown;
  padding: 5;
  summary: "Network Practice";
  default: show-all;
  multiple: true;

  option {
   label: "< 1 year";
   selector: element["NETWORK PRACTICE"="< 1 year"];
  }
  option {
   label: "1-3 years";
   selector: element["NETWORK PRACTICE"="1-3 years"];
  }
  option {
   label: "3-10 years";
   selector: element["NETWORK PRACTICE"="3-10 years"];
  }
  option {
   label: "> 10 years";
   selector: element["NETWORK PRACTICE"=">10 years"];
  }
 }

I have searched on how to do this but I have found no solution

Thanks


Solution

  • You can create a textarea in html and copy data in it to clipboard using a button inside html.

    Snippet:

    copy.html:

    <textarea id="copy"><?=temp?></textarea>
    <button>Copy</button>
    <script type="text/javascript">
      let t = document.getElementById('copy');
      let copy = () => {
        t.select();
        document.execCommand('copy');
      };
      copy();//try copying without user click 
      let bt = document.querySelector('button');
      bt.addEventListener('click', copy);
    </script>
    

    code.gs

    //Output to Html
    var template = HtmlService.createTemplateFromFile('copy');
    template.temp = temp;
    var htmlOutput = template.evaluate();
    SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Filter OptionList Maker');
    
    

    To Read: