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 hidden or 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run this script it is also required to enable the
Drive
service in the Google Apps Script configurations of the project.