调用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(); // 这是我的电子表格链接,包含表单回复和缺勤通知模板工作表 // 在这里添加你的电子表格链接 // 或者你可以替换链接中"d/"和"/edit"之间的文本 // 我的链接中这段文本是:1Kj4U5hrIPiRAjceHkySUlLFXjckh75R-KVqn0IsNrjA const ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1ktAc1lf1lhQ9xf04sRm18l-8GQurW4eqnqrldbBNYj8/edit"); // 我们将从"缺勤通知"工作表的A1单元格获取邮箱地址 // 如果你的工作表名称或单元格位置不同,请修改对应的引用 const value = ss.getSheetByName("NOA PDF").getRange("a1").getValue(); const email = value.toString(); // 邮件主题 const subject = '缺勤通知表单'; // 邮件内容 const body = "你好Cerian,<br/><br/>请查收附件中的缺勤通知表单。<br/>该表单已自动保存至病假文件夹,但需要归类到对应员工的文件夹中。<br/><br/>病假文件夹链接:https://drive.google.com/drive/folders/1SCPGzH4fz7ryna7SSuXSTtYbJlJ52r0U?usp=sharing."; // 同样是你的电子表格链接,但末尾改为"/export" // 修改为你的电子表格链接,但保留"/export"部分 const url = 'https://docs.google.com/spreadsheets/d/1ktAc1lf1lhQ9xf04sRm18l-8GQurW4eqnqrldbBNYj8/export?'; const exportOptions = 'exportFormat=pdf&format=pdf' + // 导出为PDF格式 '&size=a4' + // 纸张尺寸为A4,也可以使用letter或legal '&portrait=true' + // 纵向排版,使用false表示横向 '&fitw=true' + // 适配页面宽度,设为false则使用实际尺寸 '&sheetnames=false&printtitle=false' + // 隐藏可选页眉和页脚 '&pagenumbers=false&gridlines=false' + // 隐藏页码和网格线 '&fzr=false' + // 每页不重复显示行表头(冻结行) '&gid=852132144'; // 目标工作表ID,请替换为你的工作表ID // 你可以在链接栏中找到工作表ID // 选中要打印的工作表,查看链接,末尾的gid数值就是工作表ID var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}}; // 生成PDF文件 var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob(); // 发送带PDF附件的邮件 GmailApp.sendEmail(email, subject, body, { htmlBody: body, attachments: [{ fileName: "缺勤通知表单" + ".pdf", content: response.getBytes(), mimeType: "application/pdf" }] }); // 将PDF保存至Drive,文件名使用员工姓名(B9单元格内容) const nameFile = ss.getSheetByName("NoA PDF").getRange("b9").getValue().toString() +".pdf" DriveApp.createFile(response.setName(nameFile)); var files = DriveApp.getRootFolder().getFiles(); while (files.hasNext()) { var file = files.next(); var destination = DriveApp.getFolderById("1SCPGzH4fz7ryna7SSuXSTtYbJlJ52r0U"); destination.addFile(file); var pull = DriveApp.getRootFolder(); pull.removeFile(file); } }
内容的提问来源于stack exchange,提问作者Raffi Mostajab
相关产品推荐
相关产品推荐

