Printing a page from a Google Sheet using a script

3,627 views
Skip to first unread message

claude_...@hotmail.com

unread,
Mar 6, 2022, 9:51:35 AM3/6/22
to Google Apps Script Community
I want to use a script to print (either on paper or in a PDF file) a page from a Google sheet.

I looked at a few examples that I found and tried to execute them with no success so far.

For instance with the one pastes below I get the error "Syntax error: Unexpected token ']' " on the 1st row of the script as soon as I try so same it:

PrintService[ ] services = PrinterJob.lookupPrintServices();

Why do I get a syntax error or is there a better script example ?

Thank you

------------------------------------------------------------------

complete script example:

// lookup printers available on this machine

PrintService[] services = PrinterJob.lookupPrintServices();

// loop through the printers

for (int index = 0; index < services.length; index++)

{

 // find the Microsoft XPS Document Writer

 if (services[index].getName().equalsIgnoreCase("Microsoft XPS Document Writer"))

 {

   // create a new PrinterJob

   PrinterJob pjob = PrinterJob.getPrinterJob();

   // set the print service as the Microsoft XPS Document Writer

   pjob.setPrintService(services[index]);

   // create a new HashPrintRequestAttributeSet

   HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();

   // set the output file as a destination

   attributes.add(new Destination(new File("c:/output.xps").toURI()));

   // set the attributes to the printerjob

   pjob.print(attributes);

   // MyPrintable will have to be defined to implement the print method

   pjob.setPrintable(new MyPrintable(), new PageFormat());

  }

}


Edward Ulle

unread,
Mar 6, 2022, 11:36:34 AM3/6/22
to Google Apps Script Community
What script environment are you using?  Google App Script (GAS)? If so GAS is javascript.  And I'm not aware of  PrinterJob.lookupPrintServices(); in GAS.  Within Google Spreadsheet there is no way to print a file through GAS.  You can save the spreadsheet to PDF and then print from PDF viewer or Chrome.
Reply all
Reply to author
Forward
0 new messages