Currently my script saves the PDF with the name export.pdf and then renames it to the value I have in one of the cells of my spreadsheet, the problem with that is that my website's API ends up taking the file when it is created and all are collected like export.pdf instead of the name I really need.
Is there an option to create the PDF with the correct name instead of renaming it?
//Create PDF
SpreadsheetApp.flush();
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/' +
'IDSPREADSHEETIDSPREADSHEETIDSPREADSHEET' +
'/export?format=pdf' +
'&size=0' +
'&portrait=true' +
'&fitw=true' +
'&top_margin=0' +
'&bottom_margin=0' +
'&left_margin=0' +
'&right_margin=0' +
'&sheetnames=false&printtitle=false' +
'&pagenum=false' +
'&gridlines=false' +
'&fzr=FALSE' +
'&gid=' +
'IDPAGEIDPAGEIDPAGEIDPAGE';
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
var pdfBlob = docurl.getBlob();
//...get token and Blob (do not create the file);
var fileName = ss.getSheetByName("Gerais").getRange("H2").getValue();
//Access or create the 'Squads' folder;
var folder;
var folders = DriveApp.getFoldersByName("Squads");
if(folders.hasNext()) {
folder = folders.next();
}else {
folder = DriveApp.createFolder("Squads");
}
//Remove duplicate file with the same name;
var existing = folder.getFilesByName(fileName);
if(existing.hasNext()) {
var duplicate = existing.next();
if (duplicate.getOwner().getEmail() == Session.getActiveUser().getEmail()) {
var durl = 'https://www.googleapis.com/drive/v3/files/'+duplicate.getId();
var dres = UrlFetchApp.fetch(durl,{
method: 'delete',
muteHttpExceptions: true,
headers: {'Authorization': 'Bearer '+token}
});
var status = dres.getResponseCode();
if (status >=400) {
} else if (status == 204) {
folder.createFile(pdfBlob).setName(fileName);
}
}
} else {
folder.createFile(pdfBlob).setName(fileName);
