Skip to content

Instantly share code, notes, and snippets.

@paoloconi96
Created April 13, 2023 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paoloconi96/ff69fe9fa2be0ae3a7f91e726ef9813d to your computer and use it in GitHub Desktop.
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
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;
}
@paoloconi96
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment