Last active
May 24, 2021 10:36
-
-
Save oshliaer/dbe99c193e5b6b0c7b61 to your computer and use it in GitHub Desktop.
Batch export to PDF #gas
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 onInstall(e){} | |
var PREFERENCES = [ | |
{ | |
fileId: 'ID_GOOGLE_DRIVE_TYPE_FILE', | |
toFolderId: 'ID_FOLDER' | |
}, | |
{ | |
fileId: 'ID_GOOGLE_DRIVE_TYPE_FILE', | |
toFolderId: 'ID_FOLDER' | |
} | |
] | |
function batch(){ | |
for(var item in PREFERENCES){ | |
exportToPDF_(PREFERENCES[item]['fileId'], PREFERENCES[item]['toFolderId']) | |
} | |
} |
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 exportToPDF_(fileId, folderId){ | |
var source = DriveApp.getFileById(fileId) | |
var blob = source.getAs('application/pdf'); | |
var file = DriveApp.createFile(blob); | |
file.setName(source.getName() + '.pdf'); | |
if(folderId) { | |
try{ | |
var d = DriveApp.getFolderById(folderId); | |
moveToFolder_(file, d); | |
} | |
catch(e){ | |
} | |
} | |
return file; | |
} | |
function moveToFolder_(file, folder){ | |
folder.addFile(file); | |
DriveApp.getRootFolder().removeFile(file); | |
} |
@yureckey thanks!
I'm not sure that this will save my calls. In any case, I need to remove the file from the root folder.
Is ther a possibility to export only one sheet from a file with multiple sheets? This script exports all the sheets from the spreadsheet.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
there is no need in moveToFolder, you can just create file in folder:
DriveApp.getFolderById(folderId).createFile(blob)