8

I've been working on a survey for about a month now (this is my first Google Apps Script project, also I should mention that it's a web based app), and I only have a few things left to do (Creating a print button is one of them). Before I go on, I will give you some info about the survey. There are 6 pages(Each page corresponds to a button-I'm using buttons instead of the menu because I found out about it recently...) and only 1 page can be viewed at a time.

The problem is that I need to create the print button...which will print all of the pages.

I've been looking for a Google Apps Script example for about 3-4 days now and I can't find anything...If I didn't make it clear enough please let me know and I will try to give more details..

Rubén
  • 34,714
  • 9
  • 70
  • 166
Dave
  • 115
  • 1
  • 1
  • 4
  • When you talk about 6 pages , are these Google documents or web pages ? – Serge insas Jun 25 '12 at 17:57
  • Oh sorry, I should have mentioned that. The "pages" are flex tables witch I make visible/invisible depending on which button is pressed. So I have 6 flex tables for each page and then I have one master flex table which goes in a form(I use the e.parameter to save stuff into the database) – Dave Jun 25 '12 at 18:00

3 Answers3

8
    function printPdf() {
    SpreadsheetApp.flush();
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();

    var gid = sheet.getSheetId();

    var pdfOpts = '&size=A4&fzr=false&portrait=false&fitw=true&gridlines=false&printtitle=false&shee        tnames=false&pagenum=UNDEFINED&attachment=false&gid='+gid;


    var row2 = 29;
    var printRange = '&c1=0' + '&r1=0' + '&c2=7' + '&r2='+row2; // B2:APn
    var url = ss.getUrl().replace(/edit$/, '') + 'export?format=pdf' + pdfOpts + printRange;

    var app = UiApp.createApplication().setWidth(200).setHeight(50);
    app.setTitle('Print this sheet');

    var link = app.createAnchor('Download PDF', url).setTarget('_new');

    app.add(link);

    ss.show(app);
     }

This is a code that worked for me. It's modified from another thread.

sheepoCoding
  • 117
  • 2
  • 3
4

I guess you know that Google Apps Script applications have no way to print anything from inside your application since it runs on Google servers and not on you computer. Spreadsheets and documents integrate a print utility in their own environment but webapps (and sites) you develop with GAS don't have this environment.

Your 'print button' could probably create a pdf document that you'll be able to print easily.

Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • 2
    You can have a Google Document created that serves as a template. Each time you move to the next page, you can populate this template. And when you reach the last page, you can give the link to the populated document as a PDF (as suggested by Serge) – Srik Jun 26 '12 at 04:23
  • 1
    I didn't print anything yet but I did make a pdf document and mailed it to myself(Now I only have to give it to the user somehow and I am set). – Dave Jun 27 '12 at 21:13
1

You can print via Google Script. This post by Amit Agarwal explains how:

The key is making sure that you have a printer set up with Google Print. I have a printer with no wifi capability but is connected to an outdated Mac that exclusively runs Google print.

iampre409
  • 167
  • 1
  • 10