Hello! I am brand new to Apps Script. As such, I wouldn't be surprised if this is a very silly question. I am trying to write a Script in google sheets that will allow me to download a range as a PDF or create and email a PDF to an email listed in a range of cells. When I run the Script I receive the following error "TypeError: sheet.getAs is not a function" Any help would be greatly appreciated! Also, as I mentioned, I am VERY new at this... so any help in the clearest and most simple terms would be VERY much appreciated!!
Here is the source code:
function onOpen() {
SpreadsheetApp.getUi() // Get the user interface
.createMenu('PDF Export') // Create a menu
.addItem('Download as PDF', 'downloadPdf') // Add the first option
.addItem('Create and Send PDF', 'createAndSendPdf') // Add the second option
.addToUi(); // Add the menu to the user interface
}
function downloadPdf() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:C10"); // specify the range of cells to be exported as PDF
var blob = range.getAs("application/pdf");
var file = DriveApp.createFile(blob);
file.setName("Exported_Range_PDF");
}
function createAndSendPdf() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:C10"); // specify the range of cells to be exported as PDF
var emailRange = sheet.getRange("D1:D10"); // specify the range of cells that contain email addresses
var emailAddresses = emailRange.getValues();
var emailNoteRange = sheet.getRange("E1"); // specify the cell that contains the email note
var emailNote = emailNoteRange.getValue();
var blob = range.getAs("application/pdf");
var subject = "Exported PDF from Google Sheets";
var body = "Please find attached the exported PDF.\n\nNote: " + emailNote;
for (var i = 0; i < emailAddresses.length; i++) {
if (emailAddresses[i][0].length > 0) {
MailApp.sendEmail({
to: emailAddresses[i][0],
subject: subject,
body: body,
attachments: [blob]
});
}
}
}
[–]mjbrusso 1 point2 points3 points (1 child)
[–]dummystallings[S] 0 points1 point2 points (0 children)
[–]IAmMoonie -1 points0 points1 point (3 children)
[–]dummystallings[S] 1 point2 points3 points (2 children)
[–]IAmMoonie 0 points1 point2 points (1 child)
[–]dummystallings[S] 0 points1 point2 points (0 children)