Created
April 13, 2023 19:23
-
-
Save paoloconi96/ff69fe9fa2be0ae3a7f91e726ef9813d to your computer and use it in GitHub Desktop.
Simple Apps Script to automate the "Export As PDF" action of a Google Docs document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function main() { | |
const document = DocumentApp.getActiveDocument(); | |
const pdfFileId = ScriptProperties.getProperty('pdfFileId'); | |
if (!docWasUpdatedAfterPdf(document, pdfFileId)) { | |
return | |
} | |
updatePdfFileContent(document.getAs('application/pdf'), pdfFileId); | |
} | |
function updatePdfFileContent(fileContent, fileId) { | |
Drive.Files.update({}, fileId, fileContent); | |
} | |
function docWasUpdatedAfterPdf(document, pdfFileId) { | |
pdfLastUpdated = DriveApp.getFileById(pdfFileId) | |
.getLastUpdated(); | |
docLastUpdated = DriveApp.getFileById(document.getId()) | |
.getLastUpdated(); | |
return pdfLastUpdated < docLastUpdated; | |
} |
To run this script it is also required to enable the Drive
service in the Google Apps Script configurations of the project.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script should be attached to a Google Docs document to export it in a existing PDF file.
The initial content of the PDF file is not relevant and it can also be empty.
The PDF file id (used by Google Drive to identify the document) should be inserted in a property for the script with the name
pdfFileId
.