使用 onEdit() 将 sheet 保存为 pdf
Save a sheet as pdf with onEdit()
我正在尝试将 google 电子表格保存为 pdf 格式,并在每次编辑电子表格中的特定单元格时将其邮寄给自己。我有将电子表格保存为 pdf 并通过电子邮件发送的脚本,它可以工作,我还有一个 onEdit(e) 脚本,它完全符合我的要求。但是当我把它们放在一起时,它不起作用。这是脚本:
function onEdit(e) {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("booking");
var index = sheet1.getRange('O5').getValue();
if(index == '#N/A') return;
var email = "myemail@gmail.com";
var subject = "Subject";
var sheet3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Invoice_ENG");
var pdfname = ("PDF.pdf");
var message = ("Hello!");
//Since I can't export only one sheet, I create a new spreadsheet, copy the sheet
//into the new spreadsheet and save the new spreadsheet as pdf
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
sheet3.copyTo(newSpreadsheet);
//I erase the default Sheet1 from the new Spreadsheet
var ss = newSpreadsheet.getSheetByName('Sheet1');
ss.activate();
newSpreadsheet.deleteActiveSheet();
//Create pdf --- THIS IS WHERE THE SCRIPT SOMEHOW STOPS!
var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:pdfname,content:pdf, mimeType:'application/pdf'};
//Send mail
GmailApp.sendEmail(email, subject, message, {attachments:[attach]});
//Erase the newly created spreadsheet
DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);
}
"export as pdf" 任务是否太多而无法使用 onEdit 函数完成?因为我在我每天触发的另一个脚本中做完全相同的事情并且它工作正常。
您应该使用可安装的 onEdit 触发器,因为使用简单的触发器无法发送电子邮件。
ScriptApp.newTrigger("onEdit")
.forSpreadsheet(SpreadsheetApp.getActive())
.onEdit()
.create();
//Since I can't export only one sheet, I create a new spreadsheet, copy the sheet
//into the new spreadsheet and save the new spreadsheet as pdf
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
sheet3.copyTo(newSpreadsheet);
//I erase the default Sheet1 from the new Spreadsheet
var ss = newSpreadsheet.getSheetByName('Sheet1');
ss.activate();
newSpreadsheet.deleteActiveSheet();
我认为 activate 是一种用于工作表而不是电子表格的方法。您可以从 SpreadsheetApp 打开电子表格。
//Create pdf --- THIS IS WHERE THE SCRIPT SOMEHOW STOPS!
var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:pdfname,content:pdf, mimeType:'application/pdf'};
我认为您需要在这两个语句之间使用 DriveApp 创建 pdf 文件。 DriveApp.createFile
可能
我正在尝试将 google 电子表格保存为 pdf 格式,并在每次编辑电子表格中的特定单元格时将其邮寄给自己。我有将电子表格保存为 pdf 并通过电子邮件发送的脚本,它可以工作,我还有一个 onEdit(e) 脚本,它完全符合我的要求。但是当我把它们放在一起时,它不起作用。这是脚本:
function onEdit(e) {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("booking");
var index = sheet1.getRange('O5').getValue();
if(index == '#N/A') return;
var email = "myemail@gmail.com";
var subject = "Subject";
var sheet3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Invoice_ENG");
var pdfname = ("PDF.pdf");
var message = ("Hello!");
//Since I can't export only one sheet, I create a new spreadsheet, copy the sheet
//into the new spreadsheet and save the new spreadsheet as pdf
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
sheet3.copyTo(newSpreadsheet);
//I erase the default Sheet1 from the new Spreadsheet
var ss = newSpreadsheet.getSheetByName('Sheet1');
ss.activate();
newSpreadsheet.deleteActiveSheet();
//Create pdf --- THIS IS WHERE THE SCRIPT SOMEHOW STOPS!
var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:pdfname,content:pdf, mimeType:'application/pdf'};
//Send mail
GmailApp.sendEmail(email, subject, message, {attachments:[attach]});
//Erase the newly created spreadsheet
DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);
}
"export as pdf" 任务是否太多而无法使用 onEdit 函数完成?因为我在我每天触发的另一个脚本中做完全相同的事情并且它工作正常。
您应该使用可安装的 onEdit 触发器,因为使用简单的触发器无法发送电子邮件。
ScriptApp.newTrigger("onEdit")
.forSpreadsheet(SpreadsheetApp.getActive())
.onEdit()
.create();
//Since I can't export only one sheet, I create a new spreadsheet, copy the sheet
//into the new spreadsheet and save the new spreadsheet as pdf
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
sheet3.copyTo(newSpreadsheet);
//I erase the default Sheet1 from the new Spreadsheet
var ss = newSpreadsheet.getSheetByName('Sheet1');
ss.activate();
newSpreadsheet.deleteActiveSheet();
我认为 activate 是一种用于工作表而不是电子表格的方法。您可以从 SpreadsheetApp 打开电子表格。
//Create pdf --- THIS IS WHERE THE SCRIPT SOMEHOW STOPS!
var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:pdfname,content:pdf, mimeType:'application/pdf'};
我认为您需要在这两个语句之间使用 DriveApp 创建 pdf 文件。 DriveApp.createFile
可能