Add content of showModalDialog() to the clipboard Google Script

馋奶兔 提交于 2019-11-28 12:37:26

问题


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


回答1:


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:

  • Interact with clipboard
  • HTML Template


来源:https://stackoverflow.com/questions/57681314/add-content-of-showmodaldialog-to-the-clipboard-google-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!