2

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?

enter image description here

    //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);

1 Answer 1

2

Issue:

  • Setting file name after creating file

    folder.createFile(pdfBlob).setName(fileName);   
    

Solution:

  • Set the blob name before creating file:

    folder.createFile(pdfBlob.setName(fileName));
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.