2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GAS チェックボックスでダイアログ開閉

Last updated at Posted at 2023-02-06

下のようなものを作る。

  • A列にチェックボックスを並べる
  • チェックボックスをクリックしたら、複数選択できるダイアログボックスを表示
  • 選択された数値をチェックした行のC列に入力する。
    _GAS.gif

コード

code.gs

function showDialog(e){
  // ⎾A列以外またはCheckBoxがオフ⏌ならば何もしない。
  if (e.range.columnStart !== 1 || e.value === 'FALSE') return;
  // HTMLを組み立て.
  let html = HtmlService.createTemplateFromFile('index');
  html.row = e.range.rowStart;
  html.col = e.range.columnStart;
  html = html.evaluate().setWidth(120).setHeight(140);
  SpreadsheetApp.getUi().showModalDialog(html, '入カ値を選択');
}

function inputValues(items, row){
  SpreadsheetApp.getActive().getActiveSheet().getRange('C' + row).setValue(items);
}

index.html

<!DOCTYPE html>
<html>

<head>
  <base target="_top">
</head>
<style>
  .box select {
    width: 100px;
  }
</style>

<body>
  <div class="box">
    <select id="items" multiple>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
    </select>
  </div>
  <div>
    <button id="inputdata">入カ</button>
  </div>
  <script>
    document.getElementById("inputdata").addEventListener("click", submit);
    function submit() {
      const items = document.getElementById('items');
      const selectedItems = Array.from(items, (item) => [item.selected, item.value] ).filter(e => e[0]).map(e => e[1]);
      console.log(selectedItems)
      google.script.run.inputValues(selectedItems.join(','),<?= row ?>);
      google.script.host.close();
    }
  </script>
</body>

</html>

トリガーで、「編集時」に「showDialog」が起動するように設定しておくこと。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?