Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save phillypb/7f8b5082d86dacf32f60d785c1117da2 to your computer and use it in GitHub Desktop.

Select an option

Save phillypb/7f8b5082d86dacf32f60d785c1117da2 to your computer and use it in GitHub Desktop.
function convertToPDF() {
// ID of current Google doc
var docID = '123456789';
// ID of new folder location
var folderID = '987654321';
// create PDF version of doc *************************************************
var docFile = DriveApp.getFileById(docID);
var blobFile = docFile.getAs('application/pdf');
var pdfVersion = DriveApp.createFile(blobFile);
// get ID of PDF version
var pdfVersionID = pdfVersion.getId();
// get PDF file
var pdfFile = DriveApp.getFileById(pdfVersionID);
// get location of where PDF file currently exists (so 'My Drive')
var parents = pdfFile.getParents();
/* add PDF file to new folder location (so is ignored from above 'parents'
and hence will not be removed from this new location) */
DriveApp.getFolderById(folderID).addFile(pdfVersion);
/* once PDF file has moved, remove it from any other locations so it
only exists in the new location */
while (parents.hasNext()) {
var parent = parents.next();
parent.removeFile(pdfFile);
}
// delete the original Google doc file
docFile.setTrashed(true);
//end of create PDF version of doc ******************************************
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment