0
votes

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!

1
Have you tried hiding the sheets before the conversion? - tehhowch

1 Answers

1
votes

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.