如何在Google Sheets中发送含固定区域+最新行的PDF邮件?
实现Google Sheets导出固定区域+最新行并合并为PDF的方案
方案一:直接扩展现有PDF导出逻辑(多范围导出)
这个方案基于你现有的代码,利用Google Sheets导出API支持多范围的特性,仅需两步修改即可实现需求:
- 获取最新行范围:在
sendReport函数中,先定位表单响应表的最后一行,拼接出最新行的J到S列范围,再与固定区域用逗号合并。 - 复用导出函数:Google Sheets的导出URL支持多个范围用逗号分隔,原有
exportRangeToPDf函数无需额外修改,直接传入合并后的范围即可。
修改后的完整代码:
var ss = SpreadsheetApp.getActiveSpreadsheet(); function sendReport() { var sheetTabNameToGet = "Form response master"; var sh = ss.getSheetByName(sheetTabNameToGet); // 获取最新一行的J-S列范围 var lastRow = sh.getLastRow(); var latestRowRange = `J${lastRow}:S${lastRow}`; // 合并固定区域与最新行范围 var combinedRange = "J1:S1," + latestRowRange; var pdfBlob = exportRangeToPDf(combinedRange, sheetTabNameToGet); var message = { to: "example@example.com", subject: "Monthly sales report", body: "Hi team,\n\nPlease find the monthly report attached.\n\nThank you,\nBob", name: "Bob", attachments: [pdfBlob.setName("Monthly sales report")] } MailApp.sendEmail(message); } function exportRangeToPDf(range, sheetTabNameToGet) { var blob,exportUrl,options,pdfFile,response,sheetTabId,ssID,url_base; ssID = ss.getId(); sh = ss.getSheetByName(sheetTabNameToGet); sheetTabId = sh.getSheetId(); url_base = ss.getUrl().replace(/edit$/,''); exportUrl = url_base + 'export?exportFormat=pdf&format=pdf' + '&gid=' + sheetTabId + '&id=' + ssID + '&range=' + range + // 直接传入合并后的多范围 '&size=A4' + // 纸张尺寸 '&portrait=false' + // 横向排版 '&fitw=true' + // 适配宽度 '&sheetnames=true&printtitle=false&pagenumbers=true' + // 隐藏标题、显示页码 '&gridlines=false' + // 隐藏网格线 '&fzr=false'; // 不重复表头 options = { headers: { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(), } } options.muteHttpExceptions = true;// 启用异常捕获 response = UrlFetchApp.fetch(exportUrl, options); if (response.getResponseCode() !== 200) { console.log("Error exporting Sheet to PDF! Response Code: " + response.getResponseCode()); return; } blob = response.getBlob(); return blob; }
方案二:生成HTML表格后转PDF(布局更可控)
如果需要自定义表格样式(比如行间距、字体、边框),可以先构建HTML表格,再转换为PDF,这种方式能完全控制内容顺序和样式:
修改后的完整代码:
var ss = SpreadsheetApp.getActiveSpreadsheet(); function sendReport() { var sheetTabNameToGet = "Form response master"; var sh = ss.getSheetByName(sheetTabNameToGet); // 获取固定区域(J1:S1)的数据 var fixedRangeData = sh.getRange("J1:S1").getValues()[0]; // 获取最新一行(J-S列最后一行)的数据 var lastRow = sh.getLastRow(); var latestRowData = sh.getRange(`J${lastRow}:S${lastRow}`).getValues()[0]; // 构建HTML表格,可自定义样式 var htmlContent = ` <html> <body> <table border="1" cellpadding="8" cellspacing="0" style="font-family: Arial, sans-serif;"> <!-- 固定区域行 --> <tr style="background-color: #f0f0f0;"> ${fixedRangeData.map(cell => `<td>${cell || ''}</td>`).join('')} </tr> <!-- 最新数据行 --> <tr> ${latestRowData.map(cell => `<td>${cell || ''}</td>`).join('')} </tr> </table> </body> </html> `; // 将HTML转换为PDF Blob var pdfBlob = HtmlService.createHtmlOutput(htmlContent) .setWidth(800) // 适配A4横向宽度 .setHeight(200) .getAs('application/pdf'); var message = { to: "example@example.com", subject: "Monthly sales report", body: "Hi team,\n\nPlease find the monthly report attached.\n\nThank you,\nBob", name: "Bob", attachments: [pdfBlob.setName("Monthly sales report")] } MailApp.sendEmail(message); }
方案对比
- 方案一:代码改动小,复用原有逻辑,导出格式依赖Sheet本身设置,适合快速实现需求的场景。
- 方案二:样式可控性强,可自由调整表格外观,适合需要统一输出格式的场景。
内容的提问来源于stack exchange,提问作者comiconor
相关产品推荐
相关产品推荐

