Skip to content

Instantly share code, notes, and snippets.

@oshliaer
Last active May 24, 2021 10:36
Show Gist options
  • Save oshliaer/dbe99c193e5b6b0c7b61 to your computer and use it in GitHub Desktop.
Save oshliaer/dbe99c193e5b6b0c7b61 to your computer and use it in GitHub Desktop.
Batch export to PDF #gas
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'])
}
}
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
Copy link

there is no need in moveToFolder, you can just create file in folder:
DriveApp.getFolderById(folderId).createFile(blob)

@oshliaer
Copy link
Author

oshliaer commented Feb 25, 2017

@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.

@ovich89
Copy link

ovich89 commented May 24, 2021

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