我有以下脚本成功创建PDF并发送电子邮件,但是,pdf似乎有一些错误,因为它出现空白。当我调试此功能时,它很干净,所以我不知道如何找到导致PDF为空白的特定问题。我是谷歌脚本的新手,所以任何帮助将不胜感激。我会提供打开PDF时收到的错误的屏幕截图,但我没有足够的代表:(
function emailPDF() {
SpreadsheetApp.flush();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var url = ss.getUrl().replace(/edit$/, '');
//additional parameters for exporting the sheet as a pdf
var url_ext = 'export?exportFormat=pdf&format=pdf' + //export as pdf
'&size=letter' + //paper size
'&portrait=true' + //orientation, false for landscape
'&fitw=true' + //fit to width, false for actual size
'&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional
'&gridlines=false' + //false = hide gridlines
'&fzr=false' + //do not repeat row headers (frozen rows) on each page
'&gid='; //leave ID empty for now, this will be populated in the FOR loop
var token = ScriptApp.getOAuthToken();
//make an empty array to hold your fetched blobs
var blobs = [];
//.fetch is called for each sheet, the response is stored in var blobs[]
for (var i = 0; i < sheets.length; i++) {
var sheetname = sheets[i].getName();
//if the sheet is one that you don't want to process,
//continue' tells the for loop to skip this iteration of the loop
if (sheetname == "Dropdown Data")
continue;
if (sheetname == "Data Log")
continue;
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
}
}
options.muteHttpExceptions = true;
//grab the blob for the sheet
var response = UrlFetchApp.fetch(url + url_ext + sheets[i].getSheetId(), options);
//convert the response to a blob and store in our array
blobs.push(response.getBlob().setName(sheets[i].getName() + '.pdf'));
var array_blob = response.getBlob().setName(sheets[i].getName() + '.pdf');
}
//Email Evaulation Details
var chtm_name = ss.getRange('D5').getValue();
var evaluated_by = ss.getRange('D6').getValue();
var email_to = ss.getRange('E5').getValue();
// send email
var subject = "Customer Happiness Audit Form"
var message = "Hello " + chtm_name + " - Attached is your audit, completed by " + evaluated_by
MailApp.sendEmail(email_to, subject, message, {
attachments: [array_blob]
});
}
答案 0 :(得分:0)
如果您只需要将整张表格导出为pdf,则可以使用DriveApp。代码看起来像这样:
function emailPDF() {
SpreadsheetApp.flush();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var url = ss.getUrl().replace(/edit$/, '');
//Email Evaulation Details
var chtm_name = ss.getRange('D5').getValue();
var evaluated_by = ss.getRange('D6').getValue();
var email_to = ss.getRange('E5').getValue();
var pdf = DriveApp.getFileById(ss.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:'ConvertedSpreadsheet.pdf',content:pdf, mimeType:'application/pdf'};
// send email
var subject = "Customer Happiness Audit Form";
var message = "Hello " + chtm_name + " - Attached is your audit, completed by " + evaluated_by;
MailApp.sendEmail(email_to, subject, message, {
attachments: [attach]
});
}
**要使用单张纸张导出文件,方法应为:
// Create a new Spreadsheet and copy the current sheet into it.
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var projectname = SpreadsheetApp.getActiveSpreadsheet();
sheet = originalSpreadsheet.getActiveSheet();
sheet.copyTo(newSpreadsheet);
// Find and delete the default "Sheet 1", after the copy to avoid triggering an apocalypse
newSpreadsheet.getSheetByName('Sheet1').activate();
newSpreadsheet.deleteActiveSheet();
// Create a copy of the new file, as a pdf
var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:'Weekly Status.pdf',content:pdf, mimeType:'application/pdf'};
// Send the freshly constructed email
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
// Delete the wasted sheet we created, so our Drive stays tidy.
DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);