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

调用Google Sheets导出PDF时返回400错误,请求技术帮助

问题:Google Apps Script导出PDF时返回400错误

我是法国用户,运行一段实现「Google Sheets导出为PDF、发送邮件并保存至Drive」功能的Google Apps Script代码时,触发以下错误:

Exception: Request failed for https://docs.google.com returned code 400. Truncated server response: <meta nam... (use muteHttpExceptions option to examine full response)

附上完整代码,恳请技术人士帮忙排查解决,谢谢!

function emailSpreadsheetAsPDF() {
  DocumentApp.getActiveDocument();
  DriveApp.getFiles();

  // 这是我的电子表格链接,包含表单回复和缺勤通知模板工作表
  // 在这里添加你的电子表格链接
  // 或者你可以替换链接中&quot;d/&quot;和&quot;/edit&quot;之间的文本
  // 我的链接中这段文本是:1Kj4U5hrIPiRAjceHkySUlLFXjckh75R-KVqn0IsNrjA
  const ss = SpreadsheetApp.openByUrl(&quot;https://docs.google.com/spreadsheets/d/1ktAc1lf1lhQ9xf04sRm18l-8GQurW4eqnqrldbBNYj8/edit&quot;);

  // 我们将从&quot;缺勤通知&quot;工作表的A1单元格获取邮箱地址
  // 如果你的工作表名称或单元格位置不同,请修改对应的引用
  const value = ss.getSheetByName(&quot;NOA PDF&quot;).getRange(&quot;a1&quot;).getValue();
  const email = value.toString();

  // 邮件主题
  const subject = '缺勤通知表单';

  // 邮件内容
  const body = &quot;你好Cerian,&lt;br/&gt;&lt;br/&gt;请查收附件中的缺勤通知表单。&lt;br/&gt;该表单已自动保存至病假文件夹,但需要归类到对应员工的文件夹中。&lt;br/&gt;&lt;br/&gt;病假文件夹链接:https://drive.google.com/drive/folders/1SCPGzH4fz7ryna7SSuXSTtYbJlJ52r0U?usp=sharing.&quot;;

  // 同样是你的电子表格链接,但末尾改为&quot;/export&quot;
  // 修改为你的电子表格链接,但保留&quot;/export&quot;部分
  const url = 'https://docs.google.com/spreadsheets/d/1ktAc1lf1lhQ9xf04sRm18l-8GQurW4eqnqrldbBNYj8/export?';

  const exportOptions =
    'exportFormat=pdf&amp;format=pdf' + // 导出为PDF格式
    '&amp;size=a4' + // 纸张尺寸为A4,也可以使用letter或legal
    '&amp;portrait=true' + // 纵向排版,使用false表示横向
    '&amp;fitw=true' + // 适配页面宽度,设为false则使用实际尺寸
    '&amp;sheetnames=false&amp;printtitle=false' + // 隐藏可选页眉和页脚
    '&amp;pagenumbers=false&amp;gridlines=false' + // 隐藏页码和网格线
    '&amp;fzr=false' + // 每页不重复显示行表头(冻结行)
    '&amp;gid=852132144'; // 目标工作表ID,请替换为你的工作表ID
  // 你可以在链接栏中找到工作表ID
  // 选中要打印的工作表,查看链接,末尾的gid数值就是工作表ID
  
  var params = {method:&quot;GET&quot;,headers:{&quot;authorization&quot;:&quot;Bearer &quot;+ ScriptApp.getOAuthToken()}};
  
  // 生成PDF文件
  var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
  
  // 发送带PDF附件的邮件
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments: [{
            fileName: &quot;缺勤通知表单&quot; + &quot;.pdf&quot;,
            content: response.getBytes(),
            mimeType: &quot;application/pdf&quot;
        }]
    });

  // 将PDF保存至Drive,文件名使用员工姓名(B9单元格内容)
  const nameFile = ss.getSheetByName(&quot;NoA PDF&quot;).getRange(&quot;b9&quot;).getValue().toString() +&quot;.pdf&quot;
  DriveApp.createFile(response.setName(nameFile));
  var files = DriveApp.getRootFolder().getFiles();
    while (files.hasNext()) {
        var file = files.next();
        var destination = DriveApp.getFolderById(&quot;1SCPGzH4fz7ryna7SSuXSTtYbJlJ52r0U&quot;);
        destination.addFile(file);
        var pull = DriveApp.getRootFolder();
        pull.removeFile(file);   
}
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 05:16:05