How to Create a PDF and Email from Sheet data using Google Apps Script code lesson
Generate a PDF document on the fly and send that document as an attachment to an email with all the data coming from a spreadsheet. Standalone script connecting to a spreadsheet for data and updating the selected spreadsheet.
- Get the ID of your spreadsheet that contains the data.
- Use the id and open the sheet by the id. SpreadsheetApp.openById(id);
- Select the sheet in the spreadsheet with the data you want, get the values without the headings as an array of rows.
- Loop through the rows of data, using the data from the Spreadsheet to create some HTML code.
- Use the Apps Script Utilities to create a blob of the html content. Utilities.newBlob(html,MimeType.HTML);
- Set a name for the blob. blob.setName(`${row[0]} ${row[1]}.pdf`);
- Get the email address, subject to send the PDF to.
- Using MailApp send an email to the user, with html for the body and subject. Using the attachments property in the email add the blob of PDF. attachments:[blob.getAs(MimeType.PDF)]
- Update the spreadsheet row data with a value that the PDF was sent.
- Check the inbox for the PDF and the email.
It is showing this error-
Exception: Invalid email: State
(anonymous) @ Code.gs:14
pdfMaker @ Code.gs:6
Please help.