Google Script for extracting part of a sheet with an image on cells to a pdf

105 Views Asked by At

I'm trying to export part of a sheet (called "Certificado") to PDF. Just the range A1:H46. However, it exports all the sheet up to this cell in one page of the PDF and the rest of the sheet follows on a second page. So I would like to either get rid of that second page or just export the range A1:H46. This last option is what I'm trying to do in the following code creating and deleting a new sheet, but with no success. Just in case it is important, my sheet to export includes an image.

I've read in other questions to hide rows that I don't want in the exported PDF, but I cannot hide those rows as I need them in the sheet to input other info that links to other sheet.

Here is the Sheet input. Here is the PDF I'm getting. The output I want would be just the first page of this PDF, which is up to the row H46 in the sheet. Thanks a lot!

var originalSpreadsheet = SpreadsheetApp.getActive();

var sourcesheet = originalSpreadsheet.getSheetByName("Certificado");
var sourcerange = sourcesheet.getRange('A1:H46');
var sourcevalues = sourcerange.getValues();

var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export"); //cualquier nombre
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sheet = sourcesheet.copyTo(newSpreadsheet);
var destrange = sheet.getRange('A1:H46');

destrange.setValues(sourcevalues);
newSpreadsheet.getSheetByName('Hoja 1').activate();
newSpreadsheet.deleteActiveSheet();

var pdf = DriveApp.getFileById(newSpreadsheet.getId());
var theBlob = pdf.getBlob().getAs('application/pdf').setName(sourcesheet.getRange('C34').getValue());


var folder = DriveApp.getFolderById('10JsCe-lNb2_8RIUu34IKCaz_wBGIAja3');
var newFile = folder.createFile(theBlob);

DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);
1

There are 1 best solutions below

0
Pit On

Thank you @Tanaike for you help. Here is one possible answer to my question for anyone interested in it.

function exportPdf() {

var originalSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var hojacertificado = originalSpreadsheet.getSheetByName("Certificado")

var ssID = "1vIqeBvCPa4QnscyYC16dlLQHnx2SFoRlKAY1mKXe0wA";
  var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
                                                        "?format=pdf&"+
                                                        "size=0&"+
                                                        "fzr=true&"+
                                                        "portrait=true&"+
                                                        "scale=4&"+
                                                        "fitw=true&"+
                                                        "horizontal_alignment=CENTER&"+
                                                        "vertical_alignment=MIDDLE&"+
//                                                        "top_margin=0.50&"+
//                                                        "bottom_margin=0.50&"+
//                                                        "left_margin=0.50&"+
//                                                        "right_margin=0.50&"+
                                                        "gridlines=false&"+
                                                        "printtitle=false&"+
                                                        "sheetnames=false&"+
                                                        "pagenum=UNDEFINED&"+
                                                        "gid=686403559&"+
                                                        "ir=false&"+
                                                        "ic=false&"+
                                                        "r1=0&"+
                                                        "c1=0&"+
                                                        "r2=46&"+ // Row 46
                                                        "c2=8&"+ // Column H
                                                        "attachment=true";
                                                        
  var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
  
  // Setting a file name from a cell
  var response = UrlFetchApp.fetch(url, params).getBlob().setName(hojacertificado.getRange('C34').getValue());

  // Saving to drive
  DriveApp.getFolderById("10JsCe-lNb2_8RIUu34IKCaz_wBGIAja2").createFile(response);
}