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

Google Apps Script+HtmlService上传文件大小限制是否有官方说明?

Google表格宏上传文件大小限制问题

我用带宏的Google表格上传Zip文件并导入数据,但部分场景上传失败,推测存在文件大小限制,想知道具体限制数值,以及是否有官方文档说明?

上传大文件时控制台报错:

Connection failure due to HTTP 0

经测试,约9MB的文件上传失败,8MB的文件可正常上传。测试文件通过以下命令生成:

$ dd if=/dev/urandom bs=8m count=1 of=works
$ dd if=/dev/urandom bs=9m count=1 of=fails

相关代码如下:

appsscript.json

{
  "timeZone": "Asia/Tokyo",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "sheets": {
    "macros": [{
      "menuName": "showModal",
      "functionName": "showModal"
    }]
  }
}

Code.gs

function showModal() {
  const html = HtmlService.createTemplateFromFile("upload").evaluate();
  SpreadsheetApp.getUi().showModalDialog(html, "Upload file");
}

function uploadFile(e) {
  return `Uploaded: ${e.filename}`;
}

upload.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form>
      <input type="file" name="file" multiple />
    </form>
  </body>
  <script>
  const elemForm = document.querySelector("form");
  const elemFile = document.querySelector("input[name=file]");
  const handlerFormChange = function(event) {
    event.preventDefault();
    elemFile.disabled = true;
    for (const file of elemFile.files) {
      const fr = new FileReader();
      fr.onload = function(e) {
        const obj = {
          filename: file.name,
          bytes: [...new Int8Array(e.target.result)],
        };
        google.script.run.withSuccessHandler(response => {
          alert(`[SUCCESS] ${response}`);
        })
        .withFailureHandler(response => {
          console.error(response);
          alert(`[FAIL] See console`);
        }).uploadFile(obj);
      };
      fr.readAsArrayBuffer(file);
    }
  };
  elemForm.addEventListener("change", handlerFormChange);
  </script>
</html>

问题解答

  1. 具体大小限制
    你遇到的8MB成功、9MB失败的情况,对应Google Apps Script中google.script.run接口的请求 payload 最大限制——官方规定该接口的请求上限为10MB。但因为你将二进制文件转成Int8Array数组传递时,序列化过程会额外占用部分空间,所以实际可成功传输的文件大小会略低于10MB,和你的测试结果完全匹配。

  2. 官方文档说明
    该限制属于Google Apps Script的核心服务限制,官方明确标注了google.script.run的请求大小上限为10MB,同时还有执行时间、API调用频次等其他配套限制。

  3. 大文件上传优化方案
    如果需要上传更大的文件,建议改用Google Drive API直接将文件上传到云端硬盘,再在脚本中读取Drive文件内容。这种方式不受google.script.run的大小限制,单文件上传最大可达5TB(实际受账号存储空间配额约束)。

内容的提问来源于stack exchange,提问作者Takuya HARA

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 17:51:29