我想使用复选框启动模式

问题描述 投票:0回答:1

我想在复选框的值更改为 True 后启动模式,我已经尝试为此创建函数,但是一旦我选中了复选框,它就不会显示任何内容。我不断地修改和调整我的代码,但我实在找不到哪里出错了。请有人帮助我处理我的代码。谢谢!

这是我创建的所有功能代码:

    let folderId = '1uQjcvYKOdbpnD3AybwN4KTKQeFfun_wB';

    function onOpen() {
        let ui = SpreadsheetApp.getUi();
        ui.createMenu('Script Menu')
        .addItem('Upload Files','uploadFile')
        .addToUi();
    }


    function uploadFile() {
        let window = HtmlService.createHtmlOutputFromFile('Upload.html');
        window.setWidth(600);
        window.setHeight(400);
        SpreadsheetApp.getUi().showModalDialog(window,"Upload File");
    }


    function saveFile(e) {
        let ss = SpreadsheetApp.getActiveSpreadsheet();
        let blob = Utilities.newBlob(e.bytes,e.mimeType,e.filename);
        let folder = DriveApp.getFolderById(folderId);
        let file = folder.createFile(blob);

        let fileName = file.getName();
        let fileUrl = file.getUrl();

        let sheet = ss.getSheetByName("Request Input").getRange('B10');

        sheet.setValue(fileUrl);

        return [fileName,fileUrl];

    }

    function checkBox() {

      const activeCell = SpreadsheetApp.getActiveSpreadsheet().getActiveCell()
      const reference = activeCell.getA1Notation()
      const sheetName = activeCell.getSheet().getName()
      const activeValue = activeCell.getValue()

      if (reference == "B10" && sheetName == "Request Input" && activeValue == true)
      {

        uploadFile();
        activeCell.setValue(false)
    
      }

    }

这是我的函数代码的 HTML 代码:

<!DOCTYPE html>
<html>

<head>
  <base target="_top">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>

<body>

  <div class="container">
    <div id="form">
      <div id="fileUpload" class="mb-3">
        <input id="file" type="file" onchange="saveFile(this)" />
      </div>
      <div id="progressSpinner" class="spinner-border" role="status" style="display: none;"></div>
    </div>
  </div>


  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous">
  </script>

  <script>
    function saveFile(f) {
      document.getElementById('progressSpinner').style.display = 'block';
      const file = f.files[0];
      const fr = new FileReader();
      fr.onload = function (e) {
        const obj = {
          filename: file.name,
          mimeType: file.type,
          bytes: [...new Int8Array(e.target.result)]
        };
      //google.script.run.withSuccessHandler(data => success(data)).saveFile(obj);
      google.script.run.withSuccessHandler(google.script.host.close).saveFile(obj);
      }
    fr.readAsArrayBuffer(file);

    }

    function success(data) {
      document.getElementById('progressSpinner').style.display = 'none';
      document.getElementById('fileUpload').innerHTML = `<strong>File Uploaded Successfully</strong><br /><a target="_blank" href="${data[1]}">${data[0]}</a>`
    }
  </script>
</body>

</html>

我已经尝试过更改名称,但仍然不起作用。

google-sheets google-apps-script modal-dialog
1个回答
0
投票

尝试使用可安装的 onEdit 触发器

function onMyEdit(e) {
  e.source.toast("Entry")
  if (e.range.getSheet().getName() == "Sheet0" && e.range.columnStart == 1 && e.range.rowStart ==1 && e.value == "TRUE") {
    e.source.toast("Here");
    SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("ah4"), "Dialog")
  }
}

您需要权限才能打开对话框。

表0:

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.