'GAS export a range to PDF

Newbie situation... Am unable to find an 'export to pdf' parameter to control &scale percent which is available when exporting/printing manually.

Have found a list of approx. 24 parameters to control many aspects but the &scale parameter only offers the following 4 choices: 1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page.

It seems as though I'm missing an important 5th choice. Any help would be much appreciated.

Below is the export function from my script. (unfortunately, I don't remember where I found it) I've modified it slightly for my use and it works fine for exporting a range to a PDF except when I want the exported range to fit neatly on a page and none of the '&scale' options do what I want. The problem exists both when trying to increase or decrease the presented size of the range as it appears on paper.

function exportPDF() {
  sheetTabNameToGet = mytab;
  ss = SpreadsheetApp.getActiveSpreadsheet();//Apps Script project is bound to a G-Sheet
  ssID = ss.getId();
  sh = ss.getSheetByName(sheetTabNameToGet);
  sheetTabId = sh.getSheetId();
  url_base = ss.getUrl().replace(/edit$/,'');
  exportUrl = url_base + 'export?exportFormat=pdf&format=pdf' +
    '&gid=' + sheetTabId + '&id=' + ssID +
    '&range=' + myrange + 
    '&format=pdf' + 
    '&size=letter' +  //A3/A4/A5/B4/B5/letter/tabloid/legal/statement/executive/folio
    '&scale=4' +                      //1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
    '&portrait=true' +                //true= Portrait / false= Landscape
    '&top_margin=0.50' +              //All four margins must be set!
    '&bottom_margin=0.00'  +          //All four margins must be set!
    '&left_margin=0.00'  +            //All four margins must be set!
    '&right_margin=0.00'  +           //All four margins must be set!
    '&gridlines=false'  +             //hide gridlines
    '&printnotes=false'  +            //true/false
    '&pageorder=2'  +                 //1= Down,then over / 2= Over,then down
    '&pagenumbers=false'  +           //hide optional headers and footers
    '&sheetnames=false' +             //hide optional sheetname // header, false for none
    '&horizontal_alignment=CENTER' +  //LEFT/CENTER/RIGHT
    '&vertical_alignment=TOP' +       //TOP/MIDDLE/BOTTOM
    '&printtitle=false' +             //hide optional title //true/false
    '&fitw=false' +                   //fit to width, false for actual size
    '&fith=false' +                   //fit to height, false for actual size
    '&fzc=false'  +                   //true/false
    '&fzr=false' +                    //dont repeat row headers (frozen rows) on each page
    '&attachment=false';              //true/false
  //Logger.log('exportUrl: ' + exportUrl)
  options = {
    headers: {
      'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
    }
  }
  options.muteHttpExceptions = true;//Make sure this is always set
  response = UrlFetchApp.fetch(exportUrl, options);
  //Logger.log(response.getResponseCode())
  if (response.getResponseCode() !== 200) {
    ui.alert("Error reported in\n\n'function exportPDF()'\n\nexporting Sheet to PDF!\n\nResponse Code: " + response.getResponseCode());
    return;
  }
  blob = response.getBlob();
  blob.setName(myname)
  // Create the PDF file
  pdfFile = DriveApp.createFile(blob);
  //Logger.log('pdfFile ID: ' +pdfFile.getId())
  SpreadsheetApp.getUi()
  .alert('A new PDF file named\n\n'+myname+'\n\nHas been saved in:\n\n"My Drive" folder.');
{done()}
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source