Sheet a pdf con encabezado y pie de pagina (Sheet to pdf with header and footer)

OK, here is the google apps script web app that I believe matches with your requirements:

SCRIPT
function doGet(e) {
  var ssId = e.parameter.sheetId;
  var folderId = e.parameter.folderId;
  var shGid = e.parameter.gid;
  var shName = e.parameter.sheetName;
  var recordsNo = e.parameter.recNo;
  var fname = e.parameter.baseFilename;
  var ss = SpreadsheetApp.openById(ssId);
  var sh = ss.getSheetByName(shName);
  var rngLength = sh.getRange("A12:A1011").getValues().filter(String).length;
  var num = Math.ceil(rngLength/recordsNo);
  for (var i=0;i<num;i++) {
    sh.hideRows(12,1000);
    sh.showRows(12+(recordsNo*i),recordsNo);
    SpreadsheetApp.flush();
    var pdfUrl = "https://docs.google.com/spreadsheets/d/" + ssId + "/export?format=pdf&size=0&fzr=true&portrait=true&gid=" + shGid;
    var folder = DriveApp.getFolderById(folderId);
    var blob = UrlFetchApp.fetch(pdfUrl).getBlob().setName(fname + (i+1) + ".pdf");
    var newFile = folder.createFile(blob);
  }
sh.showRows(12,1000);
return ContentService.CreateTextOutput("pdf files created")
}

You should note the following:

  1. There are six parameters you need to feed the script from the app.
  2. I have tested with a recordsNo of 25 per pdf.
  3. I set up the sheet in the spreadsheet to have 11 rows for the header, 6 rows for the footer, and 1000 rows in-between for data. There are hard coded ranges and values in the script if you want to change this (A2:A1011 and 12,1000)
  4. Taking 2 and 3 above, the script can then generate up to 40 pdf files, but bear in mind there is a script execution time limit of 6 minutes. (My test of generating 4 pdf files from 100 records took 10 seconds)

Voy a probarlo y cualquier cosa requiero tu soporte @TIMAI2

Eres un capo @TIMAI2 :partying_face: :partying_face: :partying_face:
Muy feliz yo, y gracias por el soporte.
Puedes darme el último soporte de que estos pdf salgan en un solo libro pdf?
Se podrá?

Yes, this is possible. Do you want to keep the individual pdf files, or can they be trashed?

Tirarlos a la papelera por favor.
Y como respuesta tenga la URL del libro PDF generado.

Quedo atento a tu apoyo amigo :slight_smile:

Requiero de tu apoyo @TIMAI2 por favor :pensive:

Por favor estimado @TIMAI2 :pray:t2:

No time to develop your app for you at the moment, but here is how to do it:

No entiendo mucho esos script's, tú crees que si espero, me puedas apoyar?

Por favor amigo @TIMAI2

OK, found some time, and now we hopefully have a solution, if I have understood your requirements correctly. The following will generate a single pdf, with multiple pages consisting of a header and footer and up to 25 records per page.

My tests were with a google spreadsheet that contained just one sheet, with a header (A1:F11) a data section (A12:F1011) for upto 1000 records, and a footer (A1012:F1017).

The google apps script web app will generate a new sheet for each 25 records, with a header and footer, then hide the original records sheets, and generate a single pdf from the remaining sheets. I have annotated the script as best I can to explain what each line of the code does.

The google apps script needs to be fed 5 parameters:

  1. The spreadsheet ID
  2. The sheet name
  3. The folder ID
  4. The records number (25)
  5. The pdf name

The google apps script will return the ID of the newly created pdf file, which can then be accessed by inserting it to this url:

https://drive.google.com/file/d/<fileId here>/view

SCRIPT
function doGet(e) {
  var fileId;
  var ss = SpreadsheetApp.openById(e.parameter.ssId); //get the spreadsheet
  var sh = ss.getSheetByName(e.parameter.shName); //get the sheet with the records
  var rngLength = sh.getRange("A12:A1011").getValues().filter(String).length; //get the number of records on the sheet
  var num = Math.ceil(rngLength/e.parameter.recNo); //get the ceiling (round up) for the number of pages
  for (var i=0;i<num;i++) {
    sh.hideRows(12,1000); //hide all the individual record rows
    sh.showRows(12+(25*i),25); // show the records based upon the recrod numbers
    sh.copyTo(ss); //copy the original sheet to a new sheet
    SpreadsheetApp.flush(); //set all operations on the spreadsheet
  } //create a new sheet for each set of records, based on the records number,
  sh.hideSheet(); //hide the sheet with the recrods in it
  var folder = DriveApp.getFolderById(e.parameter.folderId); //get the folder to store the pdf
  var url_base = 'https://docs.google.com/spreadsheets/d/' + e.parameter.ssId; //the base url
  var url_ext = '/export?exportFormat=pdf&format=pdf'
      + '&id=' + e.parameter.ssId; // !important - sets the spreadsheet to be converted to a pdf (instead of just the first sheet)
      + '&size=7'      // paper size A4 = 7
      + '&portrait=true'    // orientation, false for landscape
      + '&fitw=true'        // fit to width, false for actual size
      + '&sheetnames=false&printtitle=false&pagenumbers=false'  //hide optional headers and footers
      + '&gridlines=true'  // show gridlines
      + '&horizontal_alignment=CENTER' //centre range on page
      + '&fzr=false';       // do not repeat row headers (frozen rows) on each page
      // all the options for the pdf, including the requirement to set the entire spreadsheet to the pdf
  var options = { headers: { 'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(), } } //authorisation
  var response = UrlFetchApp.fetch(url_base + url_ext, options); //now fetch the spreadsheet
  var blob = response.getBlob().setName(e.parameter.pdfName + '.pdf'); //create a blob of the spreadsheet with the filename
  fileId = folder.createFile(blob).getId(); //create the pdf file and get the file ID
  SpreadsheetApp.flush(); //set all operations on the spreadsheet
  sh.showSheet(); //show the original records sheet
  sh.showRows(12,1000); //show all the record rows
  var sheets = ss.getSheets(); //get all the sheets for the spreadsheet
  sheets.shift(); //remove the first sheet (the original records sheet)
  for (var i = 0;i<sheets.length;i++) {
  ss.deleteSheet(sheets[i]);
  } //delete all the generated sheets
  return ContentService.createTextOutput(fileId); //return the file ID as a string
}
BLOCKS example

Here also an example output pdf file, created from 165 records in the Records sheet, using 25 records per sheet, which generated 7 pages. This took @ 7 seconds to create.

testPDF.pdf (75.1 KB)

Credits should go to this SO Q&A,and the contributors there, which helped me put part of this together

I trust you can make use of this, or adapt it for your needs

Muy agradecido @TIMAI2 @SO @Q&A estoy muy agradecido con su apoyo. :slight_smile:

Solamente me queda encontrar la forma en que mi cuota de api drive sea extensa, ya que mi APP INVENTOR genera firmas con canva y los guarda al Folder Drive y en la hoja sheet en la cual generaré estos reportes PDF, pero he leido que solo te deja crear 250 docs. por día y mi necesidad se ajusta a 5000 por día, ya que es una app para uso de agroindustrial con firmas de los colaboradores al entregar un presente por días festivos.

Sin más extenderme @TIMAI2 has sido parte de mi proyecto.

Does it work for you ?

Sobre el script que me proporcionaste voy a probarlo, es una buena salida (aunque deseaba que se genere en la misma hoja sheet filtrando los registros como en el script anterior brindado), pero no puedo ser mal agradecido así que lo pondré en marcha :muscle:t2:

Sobre las cuotas limites de api drive, no sé si me podrá permitir crear 5000 firmas de archivos .png por día, o en realidad en algunos días específicos durante el año

Si funciona.
Pero existe la forma en que el libro pdf sea generado desde la hoja de donde están los registros sin necesidad de crear hojas temporales nuevas?

Tal cual como el script que me brindarte antes pero fusionando esos pdf que se obtenían.

Then my work is done.

Ummm ok @TIMAI2 gracias.
Veré la forma de adecuarlo a que no me genere hojas temporales :raised_hands:

Hola @TIMAI2 nuevamente.
No logré concretar mi duda, el script que me brindaste hace que copie de 25 en 25 registros en nuevas hojas y luego se eliminen, oculta la hoja activa o de origen, el tema es que tengo más que una hoja en el libro sheet, no hay la posibilidad que me puedas apoyar de que se genere el libro pdf con 25 registros cada hoja pdf sin tener que agregar hojas temporales sheet's??

In the script, hide sheets you do not want in the pdf, then unhide them again once the pdf is created. This is already being done for Sheet1.