0

I'm struggling to find an example of a google apps script to export "only" selected sheets to pdf. It seems that it is not possible?

I get various examples to export either a single sheet or the whole spreadsheet. I want to use the script to print a dynamic array of sheets based on data in the spreadsheet. But not all the sheets

Any help will be appreciated!

Nico Roos
  • 35
  • 4
  • 1
    Have you tried hiding the sheets before the conversion? – tehhowch Mar 15 '18 at 19:30
  • 3
    Possible duplicate of [Generate PDF of only one sheet of my spreadsheet](https://stackoverflow.com/questions/49197358/generate-pdf-of-only-one-sheet-of-my-spreadsheet) – Jack Brown Mar 15 '18 at 20:28

1 Answers1

1

I haven't tested this but this should give you a start.

function sheetsToPDF() {
  var ss=SpreadsheetApp.getActive();
  var shts=ss.getSheets();
  for(var j=0;j<shts.length;j++){
    var sh=shts[j];
    var rg=sh.getDataRange()
    var vA=rg.getValues();
    var s='';
    for(vari=0;i<vA.length;i++){
      s+=vA[i].join(',') + '\n';
    }
    sheetToPDF(sh.getName(),s);
  }
}

function sheetToPDF(name,content){
  var s='';
  for(vari=0;i<vA.length;i++){
    s+=vA[i].join(',') + '\n';
  }
  DriveApp.createFile(name, content, MimeType.PDF)
}

It just creates a csv row with a carriage return at the end of the line and then creates a pdf file with those rows. I've never done this before so you may have to solve some other problems but it's a start any way.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • This should probably work for numeric sheets, but if the sheet contains any formatting or graphic elements, they won't be included in the conversion. – tehhowch Mar 16 '18 at 06:05
  • Thanks will give it a try! – Nico Roos Mar 16 '18 at 06:17
  • It’s just meant to be a starting point. I recommend you read the online reference to learn how all the functions work and if you need to add additional features , try to figure them out on your own. – Cooper Mar 16 '18 at 14:00