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

Deseo generar pdf con encabezado y pie de paginas desde app inventor como base de una sheet

Please explain in more detail, show an image or diagram about what you want to achieve.

Buenas tardes mi amigo @TIMAI2 recuerda por favor que la vez pasada me ayudaste a generar pdf de una hoja de sheet y guardarlo en drive, ahora quiero que cada hoja pdf sea de una rango de 25 registros o filas del sheet pero a la vez que cada hoja de pdf contenga el mismo encabezado y el mismo pie de página que serían celdas del sheet, todas las hojas pdf se generen en un solo libro pdf

It sounds like you would need to create a template sheet, with the header and footer, and the correct formatting to accept data/cells from another sheet to create the sheet you wish to export to pdf, and to then be able to do this for as many data sheets as you have ?

Tengo una hoja de datos, osea, son 6 hojas, pero por cada hoja generaría un libro pdf con los 25 registros y los encabezados y pie de páginas.
Pero cada hoja sheet puede tener más de 25 registros, osea 100 por ejemplo pero en la hoja pdf solo quiero de 25 con encabezado y pie de pagina

With sheets that have more than 25 records, e.g. 100, which 25 records do you want, the first 25, or a selection based upon criteria, or something else?

Once I have this, i can build up and example for you that can do this.

Deseo que sean todos los registros pero dividos por 25 osea si tengo 100 filas en sheet generarĂ­a 4 hojas de pdf (25 cada uno) pero en un solo libro pdf (con 4 hojas internas)

OK, you don't ask for much :wink:

This will take a while to work up.

Estaré muy agradecido mi gran amigo @TIMAI2

Quedo muy atento a tu gran apoyo mi amigo @TIMAI2 :slight_smile: :cowboy_hat_face:

OK, the following google apps script will generate a pdf from each of 6 sheets with data, 25 records at a time, for sheets that contain up to 100 records.

SPREADSHEET:

It is important that the template sheet is the last sheet on the spreadsheet, and all other sheets must be data sheets

EXAMPLE PDFs

Sheet5-2.pdf (45.6 KB)
Sheet3-4.pdf (50.7 KB)
Sheet3-2.pdf (50.1 KB)
Sheet1.pdf (49.5 KB)

All the pdfs generated to a folder on google drive:

SCRIPT - you will need to feed the web app with the Spreadsheet ID, the Template sheet gid, and the Folder ID, from your app project
function doGet(e) {
  var ssId = e.parameter.id;
  var tplGid = e.parameter.gid;
  var fdrId = e.parameter.folderId
  
  var ss = SpreadsheetApp.openById(ssId);
  var tpl = ss.getSheetByName('Template');
  var sheets = ss.getSheets();
  sheets.pop();

  for (var i=0;i<sheets.length;i++) {
    var data = sheets[i].getRange('A1:F25').getValues();
    tpl.getRange('A5:F29').setValues(data);
    SpreadsheetApp.flush();
    createPdfFromUrl(sheets[i].getName(),ssId,tplGid,fdrId);
  } 

  for (var i=0;i<sheets.length;i++) {
    var rng = sheets[i].getDataRange().getValues();
    var rngLength = rng.length;
    if (rngLength > 25) {
      var data = sheets[i].getRange('A26:F50').getValues();
      tpl.getRange('A5:F29').setValues(data);
      SpreadsheetApp.flush();
      createPdfFromUrl(sheets[i].getName() + "-2",ssId,tplGid,fdrId);
    } 
  }

    for (var i=0;i<sheets.length;i++) {
    var rng = sheets[i].getDataRange().getValues();
    var rngLength = rng.length;
    if (rngLength > 50) {
      var data = sheets[i].getRange('A51:F75').getValues();
      tpl.getRange('A5:F29').setValues(data);
      SpreadsheetApp.flush();
      createPdfFromUrl(sheets[i].getName() + "-3",ssId,tplGid,fdrId);
    } 
  }

    for (var i=0;i<sheets.length;i++) {
    var rng = sheets[i].getDataRange().getValues();
    var rngLength = rng.length;
    if (rngLength > 75) {
      var data = sheets[i].getRange('A76:F100').getValues();
      tpl.getRange('A5:F29').setValues(data);
      SpreadsheetApp.flush();
      createPdfFromUrl(sheets[i].getName() + "-4",ssId,tplGid,fdrId);
    } 
  }  
return ContentService.CreateTextOutput("pdf files created")
}


function createPdfFromUrl(fname,ssId,tplGid,fdrId) {
  var pdfUrl = "https://docs.google.com/spreadsheets/d/" + ssId + "/export?format=pdf&size=0&fzr=true&portrait=false&gid=" + tplGid;
  var folder = DriveApp.getFolderById(fdrId);
  var blob = UrlFetchApp.fetch(pdfUrl).getBlob().setName(fname + ".pdf");
  var newFile = folder.createFile(blob);
}

This script took @ 30 seconds to run from my computer.

Muy agradecido @TIMAI2
Pero mi idea era, de este documento:

El rango A1:G11 sea el encabezado para cada hoja pdf (y todas se generen en un solo libro), desde el a12 sean mis registros, y ya sin dar relevancia de cuántos registros me salgan por hoja, pero si que cada hoja tenga también el pie de pagina que está en el rango A1001:G1005

TĂş crees que me puedas ayudar?

I do not understand what you want. More explanation required.

Meantime:

A man walked into a shop, and asked for four apples, two oranges and three bananas. The shop keeper dutifully went over to the fruit section, selected the best fruit of each variety, weighed and priced each fruit type and bagged them up. He then returned to the shop counter, where the man was waiting, and a queue of customers was forming behind him. The shopkeeper said to the man, "here are the four apples, two oranges and three bananas you asked for". The man replied, "I wanted a pineapple, and some plums, I do not want the fruit you have already picked, weighed, priced and bagged." The shopkeeper gave the man a look of exasperation and held up his hands in dismay.

What did the shopkeeper do next?

4 Likes

Buen dĂ­a @TIMAI2
De mi hoja que te mandé mis registros empiezan en la fila 12 quiero que mis pdf generados tomen el encabezado A(rango A1:G11) y el pie de página (rango A1001:G1005) y vaya tomando mis registros desde la fila 12, sé que van a generar varias hojas de pdf porque son varios registros.
Espero haberme explicado mejor

Do you just have one sheet with records, a header and a footer, and the number of records can vary across time, and when you want to create pdf /pdfs you want a full set of pdfs for each batch of 25 records, or just the latest records. if you have just one sheet, then the approach to producing the pdfs can be quite different.

Until I fully understand what it is you want, it is difficult to offer a solution. I do not want to do any more work on this unnecessarily....(given that this is also not really an AI2 question!!)

Que me genere los pdf por los registros actuales que existan, pueden variar, a veces puede haber 10, a veces 100, a veces 200.
Y sĂ­ va relacionado a APP INVENTOR porque quiero adaptarlo

Tengo un libro sheet con varias hojas pero solo quiero que me genere pdf de una hoja en especifico.
La cantidad de registros puede variar y quiero que tone todos los registro

I have a solution in my head, just need to convert it to a web app...:wink:

Te agradecería mil veces más mi amigo @TIMAI2