GAS
1function copyToClipbord(){
2 var htmlOutput = HtmlService.createTemplateFromFile("popup");
3 htmlOutput.copytext = "コピーする文言を設定する";
4 var html = htmlOutput.evaluate();
5
6 var ss = SpreadsheetApp.getActiveSpreadsheet();
7 ss.show(html); //メッセージボックスとしてを表示する
8}
popup.html
HTML
1<!DOCTYPE html>
2<html>
3 <head>
4 <base target="_top">
5 </head>
6 <body>
7 <h2>コピーしますか?</h2>
8 <button type="button" onclick="copyText(<?=copytext ?>)">はい</button>
9 <button type="close" onclick="google.script.host.close()">いいえ</button>
10
11 <script>
12 async function copyText(txt) {
13 navigator.clipboard.writeText(txt);
14 google.script.host.close();
15 };
16 </script>
17 </body>
18</html>
19
こんな感じでいけました。
スプレッドシートのメッセージボックスとしてHTMLを指定し、そのHTML側でクリップボードにコピーするスクリプトを実行するという形になります。
文字列のコピーだけではなく、「はい」「いいえ」の選択で何かしらの処理を行いたい場合
GAS
1function copyToClipbord(){
2 var htmlOutput = HtmlService.createTemplateFromFile("popup");
3 htmlOutput.copytext = 'コピーする"文言"を設定する';
4 var html = htmlOutput.evaluate();
5
6 var ss = SpreadsheetApp.getActiveSpreadsheet();
7 ss.show(html); //メッセージボックスとしてを表示する
8}
9
10function dialogResult(result) {
11 if (result =='YES') {
12 Browser.msgBox('はいが押されました');
13 } else if (result =='NO') {
14 Browser.msgBox('いいえが押されました');
15 }
16}
HTML
1<!DOCTYPE html>
2<html>
3 <head>
4 <base target="_top">
5 </head>
6 <body>
7 <h2>コピーしますか?</h2>
8 <!--<input type="hidden" id="copyText" value="<?=copytext ?>" />-->
9 <button type="button" onclick="copyText(<?=copytext ?>)">はい</button>
10 <button type="close" onclick="returnResult('NO')">いいえ</button>
11
12 <script>
13 function copyText(txt) {
14 navigator.clipboard.writeText(txt);
15 returnResult('YES');
16 };
17
18 function returnResult(result) {
19 google.script.run.dialogResult(result);
20 google.script.host.close();
21 }
22 </script>
23 </body>
24</html>
このようにボタンが押された時にスクリプトを呼び出すようにし、そこで押されたボタンにより処理を行うような形にできます。