Invoice Project: Export as PDF and email, while saving PDF to a specific Drive folder
Published (updated: ) in code.
This is part two of my previously mentioned invoice project. The first part of the project was adding in the ability to pull data from a Stripe invoice across the Stripe API into a pre-formatted Google Sheet invoice template. This article will discuss the second part of the project: What to do with the invoice once generated.
Saving to a Specific Drive Folder
The first script inside of Google Apps Script pulls data from an invoice from the Stripe API and populates the data in a Google Sheet invoice template. After the first script runs and the invoice is generated, the second script I added for this project has two parts.
The first part of the script grabs the invoice from Google Sheets and saves it as a PDF. The script then saves the PDF invoice to a specific folder within Google Sheets. This is helpful because I want to keep a copy of the invoices generated for each customer for future reference. An example would be having a folder for “ABC Company” where all invoices generated by the script would be stored.
This specific comment on StackOverflow gave me the outline of the code to use in the script to save the file as a PDF to a specific folder in Google Drive. The link to the actual code’s GitHub repo I wrote is at the bottom of the article.
Emailing a Copy of the PDF
The second part of this new script saves a PDF copy of the generated invoice and emails it to a designated email address. Right now I’m just using it to email a copy of the invoice to myself so I have a copy. In the future I can see myself using the customer’s email address and emailing them a copy via only one click from inside of the Sheet template.
This article was very helpful in giving me an outline of code to use for this part of the script. I modified it slightly using the code in the linked article above because I didn’t want to save to the root Google Drive folder, as used in this article, but rather save to a specific folder designated for the user.
The article in the paragraph above also saves the entire Google Sheet as a ZIP file and emails, which I didn’t need. I only wanted a specific tab from the Google Sheet and only as a PDF. So I also used the code in this StackOverflow comment to modify the original code.
New Functionality Launched!
The new script is now live and working great! All I have to do is run the second script after the invoice is generated and a copy of the PDF invoice is sent via email, then a copy of the PDF is also saved to a specific folder in Google Drive. All with one click of a button inside of Sheets!
I believe I’ll have a master copy of the Sheet template with the scripts. I’ll then make a copy and save it too a Drive folder for each specific customer. I can then modify both the template and the scripts to the specific customer’s specifications.
You can view the GitHub repo here. This specific script is in a file called “export_to_pdf_and_email.js” within the repo.