Print a chart from Google Sheets horizontally

384 Views Asked by At

I need to find ways of printing a chart as pdf/image/sheet horizontally.

I print automatically with code.

Now I am printing a pdf created out of a Spreadsheet tab but it always prints vertically despite options settings.

It saves beautiful horizontal pdfs on Drive but then prints them vertically.

As I print statistics I need it to be horizontally.

Here's my code with options:

  var requestData = {
    "oAuthServiceName": "spreadsheets",
    "oAuthUseToken": "always",
  };  
  
  //spreadsheet id
  var ssID = ss.getId()
  
  //sheet id
  var sID = ss.getSheetByName("Print").getSheetId();
   
  //creating pdf  
  var pdf = UrlFetchApp.fetch("https://docs.google.com/spreadsheets/d/"+ssID+"/export?gid="+sID+"&portrait=false&size=letter&fitw=true&format=pdf",requestData).getBlob();  
  
  //folder to created pdf in
  var folder = DriveApp.getFolderById("1RseEezmGT2gSBS9rgdV0E2ggRD5O6dyu")
 
  //creating pdf in this folder with given name
  var file = folder.createFile(pdf).setName("Chart")
  
  var docID = file.getId()

  var printerID = "02f4280c-9e83-da7d-d5ff-ca0f25ca03e3"
  var docName = "Chart"

  //starts printing
  printGoogleDocument(docID, printerID, docName)

And

function printGoogleDocument(docID, printerID, docName) {
  
  var ticket = {
    version: "1.0",
    print: {
      color: {
        type: "STANDARD_COLOR",
        vendor_id: "Color"
      },
      duplex: {
        type: "NO_DUPLEX"
      }
    }
  };

  var payload = {
    "printerid" : printerID,
    "title"     : docName,
    "content"   : DriveApp.getFileById(docID).getBlob(),  
    "contentType": "application/pdf",
    "ticket"    : JSON.stringify(ticket)
  };

  var response = UrlFetchApp.fetch('https://www.google.com/cloudprint/submit', {
    method: "POST",
    payload: payload,
    headers: {
      Authorization: 'Bearer ' + getCloudPrintService().getAccessToken()
    },
    "muteHttpExceptions": true
  });

So I am using here an option portrait=false to print as a landscape. Doesn't work.

Appreciate any solutions for horizontal printing.

1

There are 1 best solutions below

9
TheMaster On

Try including page_orientation in the ticket:

  var ticket = {
    version: "1.0",
    print: {
      color: {
        type: "STANDARD_COLOR",
        vendor_id: "Color"
      },
      duplex: {
        type: "NO_DUPLEX"
      },
      page_orientation: { //Added
        type:1//or type:"LANDSCAPE"
      }
    }
  };

References: