You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Google Apps Script Web应用设置执行状态通知

解决方案

问题出在你当前的 doGet 函数会先执行所有 Drive 操作,完成后才返回 HTML 页面,导致用户在等待过程中看不到任何提示。要实现实时的「处理中」通知,需要将页面加载和后台操作分离,通过客户端异步调用服务器端函数。

步骤1:修改服务器端代码

将原本在 doGet 里的文件处理逻辑抽成独立函数,doGet 仅负责返回初始页面:

function doGet() {
  // 直接返回HTML页面,不执行耗时操作
  return HtmlService.createTemplateFromFile('Index')
    .evaluate()
    .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

// 独立的文件处理函数,供客户端调用
function createFolderAndFiles() {
  var newFolder     = DriveApp.createFolder('FOLDER APLIKASI PKL KELAS XXXX - SILAHKAN DI RENAME -');
  var folderKelasID = newFolder.getId();
  var folderKelas   = DriveApp.getFolderById(folderKelasID);
  var masterSSID    = "1e8Sn1fRurEUB9l0Xb65iRYLi-yYBfgkRLJcKI2xxxx";
  var masterDOCID   = "107nzTmin2skbXXBTU3vdZsy-ePKY5r7HuhLfngxxxxx";
  var copySSId      = DriveApp.getFileById(masterSSID).makeCopy('SPREADSHEET MASTER  JURNAL PKL').getId();
  var copyDOCId     = DriveApp.getFileById(masterDOCID).makeCopy('MASTER JURNAL PKL').getId();
  var formURL       = SpreadsheetApp.openById(copySSId).getSheetByName("Form Responses").getFormUrl();
  var formID        = FormApp.openByUrl(formURL).getId();
  
  DriveApp.getFileById(copySSId).moveTo(folderKelas);
  DriveApp.getFileById(copyDOCId).moveTo(folderKelas);
  DriveApp.getFileById(formID).moveTo(folderKelas);  
  DriveApp.getFileById(formID).setName('FORM  LAPORAN PKL');
  
  // 返回成功信息供客户端显示
  return "文件夹和文件已创建完成!";
}

步骤2:创建/修改 Index.html 文件

在 HTML 中先显示「处理中」提示,然后通过 google.script.run 异步调用服务器端函数,完成后更新提示内容:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <style>
      .notification {
        font-size: 18px;
        padding: 20px;
        text-align: center;
        margin: 50px auto;
        width: 300px;
        border: 1px solid #ccc;
        border-radius: 8px;
      }
      .processing {
        color: #007bff;
      }
      .completed {
        color: #28a745;
      }
      .error {
        color: #dc3545;
      }
    </style>
  </head>
  <body>
    <div id="status" class="notification processing">处理中...</div>

    <script>
      // 页面加载完成后调用服务器函数
      window.onload = function() {
        google.script.run
          .withSuccessHandler(function(message) {
            // 处理成功时更新提示
            var statusDiv = document.getElementById('status');
            statusDiv.textContent = message;
            statusDiv.className = 'notification completed';
          })
          .withFailureHandler(function(error) {
            // 处理失败时显示错误信息
            var statusDiv = document.getElementById('status');
            statusDiv.textContent = "处理失败:" + error.message;
            statusDiv.className = 'notification error';
          })
          .createFolderAndFiles(); // 调用服务器端函数
      };
    </script>
  </body>
</html>

注意事项

  1. 部署 Web 应用时,确保设置为「以访问该应用的用户身份执行」(如果需要用户使用自己的 Drive 权限)。
  2. 修改代码后需要重新部署 Web 应用,生成新的部署版本。
  3. google.script.run 是异步执行的,不会阻塞页面加载,因此用户能立即看到「处理中」提示。

内容的提问来源于stack exchange,提问作者PSAB Plalangan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 10:43:31