Welcome folks today in this blog post we will be emailing the data
of whole google sheet as a pdf document in spreadsheet editor. All the full source code of the application is shown below.
Get Started
In order to get started you need to go to google sheets and create a new sheet as shown below. And after that you need to insert some data as well in that sheet so that we will email the data to the user.
Now we need to draw a button guys inside the google spreadsheet so that when we click that button our script will run in the background to send the sheet data as an pdf to the email address. You can insert the button as shown below
Now we need to assign the script to this button. So whenever we click this button what script should run. We will name our script as emailsheet
as shown below
Now we need to enter this function
that we have assigned inside the google apps script
editor. Open it using the menu as shown below
Now we will be writing the app script
code guys. So just copy paste the below code
code.gs
1 2 3 4 5 6 7 8 9 10 11 12 |
function emailsheet(){ var ss = SpreadsheetApp.getActiveSpreadsheet(); var filename = "A Copy of " + ss.getName(); var MyNewBook = ss.copy(filename); var MyUser = "harshadchopra1997@gmail.com"; MailApp.sendEmail({ to: MyUser, subject: 'TEST title', body: 'TEST body', attachments: [MyNewBook.getBlob().setName(filename + ".pdf")] }) } |
As you can see we are getting all the data of the current google spreadsheet
and then sending it as the pdf document to the specified email address. Just change the email to your target email.
And now guys if you to the google sheets and if you try to press the button your script will run you need to allow the permissions as shown below