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

求助:HTML表单传PDF至OCR API对接Google Sheets报错排查

问题诊断与修复步骤

1. 修复函数名称不匹配的核心错误

前端JavaScript调用的函数是procesarFormulario,但后端Apps Script的函数名是procesForm,名称不匹配会直接导致调用失败,触发未捕获错误。修改后端函数名与前端调用保持一致:

function procesarFormulario(form) { // 改名匹配前端调用
   Logger.log(form); // 用Logger.log记录,可在Apps Script编辑器「查看>日志」中查看
   var archivo = form.file;
   // ... 后续代码
}

2. 正确处理上传的文件Blob

通过google.script.run传递的表单文件,form.file是包含bytes、type、name属性的对象,不能直接用Utilities.newBlob(archivo)生成Blob,需按属性构造:

// 替换原bytes获取代码
var blob = Utilities.newBlob(archivo.bytes, archivo.type, archivo.name);
var bytes = blob.getBytes();

3. 添加API请求错误捕获与日志

当前代码无错误处理,API请求可能因密钥错误、网络问题等抛出异常,新增try-catch块并开启异常响应捕获:

if (archivo != null) {
  try {
    var blob = Utilities.newBlob(archivo.bytes, archivo.type, archivo.name);
    var bytes = blob.getBytes();

    var url = "https://api.mindee.net/v1/parsing/parsers/TypeInvoiceV4";
    var apiKey = "你的实际API密钥"; // 建议通过脚本属性存储,不要硬编码
    var headers = {
      "Authorization": "Bearer " + apiKey,
      "Content-Type": "application/octet-stream"
    };
    var options = {
      "method": "post",
      "headers": headers,
      "payload": bytes,
      "muteHttpExceptions": true // 开启后可获取详细错误响应
    };
    var response = UrlFetchApp.fetch(url, options);
    Logger.log("API响应状态码: " + response.getResponseCode());
    Logger.log("API响应内容: " + response.getContentText());

    if (response.getResponseCode() === 200) {
      var parsedData = JSON.parse(response.getContentText());
      var hojaDatos = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      // 提取具体字段存储,避免直接存入对象显示[object Object]
      hojaDatos.appendRow([
        parsedData.document.id,
        parsedData.document.inference.prediction.total_amount,
        new Date()
      ]);
    } else {
      Logger.log("API请求失败: " + response.getContentText());
    }
  } catch (e) {
    Logger.log("处理出错: " + e.message + " 行号: " + e.lineNumber);
  }
}

4. 前端添加调用反馈

为google.script.run添加成功/失败回调,方便调试和用户反馈:

<script>
  document.getElementById("myForm").addEventListener("submit", function(e) {
    e.preventDefault();
    var form = document.getElementById("myForm");
    
    google.script.run
      .withSuccessHandler(function() {
        alert("文件上传处理成功!");
      })
      .withFailureHandler(function(error) {
        alert("处理失败: " + error.message);
        console.log(error);
      })
      .procesarFormulario(form);
    
    form.reset();
  });
</script>

5. 额外优化建议

  • 密钥安全:不要硬编码API密钥,通过Apps Script「项目设置>脚本属性」存储,用PropertiesService.getScriptProperties().getProperty("MINDEE_API_KEY")调用。
  • 数据存储:parsedData.document是复杂对象,需提取业务需要的具体字段(如发票金额、编号)存入表格,避免显示无效值。
  • 前端验证:可添加文件大小、格式的额外验证,减少无效请求。
调试步骤
  1. 打开Apps Script编辑器,查看「日志」确认form对象结构、API响应内容。
  2. 前端测试时打开浏览器控制台(F12),检查JS错误。
  3. 验证Mindee API密钥有效性,确认端点TypeInvoiceV4是正确的解析器ID。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 02:34:54