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

Google Apps Script处理超5万字符字符串遇执行错误求解决方案

解决超长字符串输入的Error resuming script execution问题

问题根源

Browser.inputBox存在内置字符长度限制(远低于50000字符),当粘贴超长内容时,超出限制就会触发Error resuming script execution错误。要绕过这个限制,直接用HTML模态对话框的textarea接收用户输入是更可靠的方案——textarea支持的字符长度远高于inputBox,且能直接通过google.script.run把内容传回脚本处理。

修正后的代码

Code.gs

function testDialog() {
  // 直接加载HTML对话框,跳过Browser.inputBox
  var htmlOutput = HtmlService.createHtmlOutputFromFile('copy')
    .setWidth(610)
    .setHeight(810);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, '粘贴超长HTML内容');
}

// 接收前端传来的超长内容并处理
function handleLongString(userInput) {
  console.log('接收的内容长度:', userInput.length);
  // 这里可添加业务逻辑,比如写入单元格(注意:单个单元格最多支持50000字符,超量需拆分或存到Drive)
  SpreadsheetApp.getActiveSheet().getRange('A1').setValue(userInput.substring(0, 50000));
}

copy.html

<div style="padding: 10px;">
  <button id="top" style="margin-bottom: 25px;">复制内容</button>
  <input style="width:25%;height:30px;" type="button" value="关闭" onClick="google.script.host.close();" />
  
  <!-- 用于粘贴超长内容的textarea -->
  <textarea id="user-input-box" cols="100" rows="30" placeholder="在此粘贴超长HTML内容..."></textarea>
  
  <!-- 提交按钮,将内容传回后端脚本 -->
  <button id="submit" style="margin-top: 25px; background: #4285F4; color: white;">提交内容</button>
  <button id="bottom" style="margin-top: 25px;">复制内容</button>
  <input style="width:25%;height:30px;" type="button" value="关闭" onClick="google.script.host.close();" />
</div>

<script>
  // 提交内容到后端
  document.getElementById('submit').addEventListener('click', function() {
    var userInput = document.getElementById('user-input-box').value;
    google.script.run
      .withSuccessHandler(function() {
        alert('内容提交成功!');
        google.script.host.close();
      })
      .withFailureHandler(function(error) {
        alert('提交失败:' + error.message);
      })
      .handleLongString(userInput);
  });

  // 复制功能
  let t = document.getElementById('user-input-box');
  let copy = () => {
    t.select();
    document.execCommand('copy');
    alert('内容已复制到剪贴板');
  };
  
  document.getElementById('top').addEventListener('click', copy);
  document.getElementById('bottom').addEventListener('click', copy);
</script>

<style>
textarea {
  padding: 8px;
  margin: 10px 0;
  border: 1px solid #ddd;
  width: 100%;
  box-sizing: border-box;
}

textarea:focus {
  outline: none;
  border-color: #4285F4;
}

button {
  height: 30px;
  width: 68%;
  font-weight: bold;
  background: #00ff00;
  border: none;
  cursor: pointer;
  display: inline-block;
}

button:hover {
  opacity: 0.8;
}

#submit {
  background: #4285F4;
  color: white;
}
</style>

关键说明

  1. 移除Browser.inputBox:直接用HTML对话框的textarea接收输入,彻底绕开inputBox的字符限制。
  2. google.script.run传值:通过前端脚本把textarea里的超长内容传回后端函数,该方法支持的字符长度远超过inputBox限制。
  3. 单元格限制注意:Google Sheets单个单元格最多支持50000字符,若内容超量,需拆分到多个单元格,或保存到Google Drive文本文件中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 13:30:39