Generate PDF from a Google Spreadsheet

Asked

Viewed 1,055 times

2

I would like to know how I send a spreadsheet from a spreadsheet on google.

I already have one script done, to send emails every day with information, however, I would like these emails to be downloaded in the form of .PDF, how can I do this?

I already have this script ready:

function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange(2, 1, 1000, 10);
var data = dataRange.getValues();
var message = '';

message += geraTabela(20, 30, data); 
message += geraTabela(15, 20, data);
message += geraTabela(0, 15, data);

 MailApp.sendEmail(
'email para qual eu envio', 
'Pagamentos em vencimento',
'Utilize um leitor de e-mail compatível.',
{ htmlBody : message });
}

function geraTabela(inicio, fim, data) {
// Inicio da tabela 1
var message = '';

message += '<h3>Processos que vencerão em '+fim+' dias</h3>';
message += '<table border=1 cellpadding=4>';
message += '<tr><th>PGNET</th><th>Nome da Parte</th><th>Nº Administrativo</th></tr>';

for (i in data) {
var row = data[i];
if (row[4] && row[4] > inicio && row[4] <=fim) {
  message += '<tr>';
  message += '<td>' + row[6] + '</td>'; 
  message += '<td>' + row[1] + '</td>';
  message += '<td>' + row[8] + '</td>';
  message += '</tr>';
}
}

message += '</table>';

// Fim da tabela 1
return message;
}

I would like to know how to generate this information in .PDF, preferably horizontal, which command should I apply?

1 answer

0

I made this code that should suit your need I could not execute exactly as you want but I assumed that your function returns html with the table ready....

I used this jsFidle as an aid

window.export.onclick = function() {

  if (!window.Blob) {
    alert('Your legacy browser does not support this action.');
    return;
  }

  var html, link, blob, url, css;

  // EU A4 use: size: 841.95pt 595.35pt;
  // US Letter use: size:11.0in 8.5in;

  css = (
    '<style>' +
    '@page WordSection1{size: 841.95pt 595.35pt;mso-page-orientation: landscape;}' +
    'div.WordSection1 {page: WordSection1;}' +
    'table{border-collapse:collapse;}td{border:1px gray solid;width:5em;padding:2px;}' +
    '</style>'
  );

  html = myFunction();
  blob = new Blob(['\ufeff', css + html], {
    type: 'application/msword'
  });
  url = URL.createObjectURL(blob);
  link = document.createElement('A');
  link.href = url;
  // Set default file name. 
  // Word will append file extension - do not add an extension here.
  link.download = 'Document.doc';
  document.body.appendChild(link);
  if (navigator.msSaveOrOpenBlob) navigator.msSaveOrOpenBlob(blob, 'Document.doc'); // IE10-11
  else link.click(); // other browsers
  document.body.removeChild(link);
};



function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var dataRange = sheet.getRange(2, 1, 1000, 10);
  var data = dataRange.getValues();
  var message = '';

  message += geraTabela(20, 30, data);
  message += geraTabela(15, 20, data);
  message += geraTabela(0, 15, data);

  MailApp.sendEmail(
    'email para qual eu envio',
    'Pagamentos em vencimento',
    'Utilize um leitor de e-mail compatível.', {
      htmlBody: message
    });
}

function geraTabela(inicio, fim, data) {
  // Inicio da tabela 1
  var message = '';

  message += '<h3>Processos que vencerão em ' + fim + ' dias</h3>';
  message += '<table border=1 cellpadding=4>';
  message += '<tr><th>PGNET</th><th>Nome da Parte</th><th>Nº Administrativo</th></tr>';

  for (i in data) {
    var row = data[i];
    if (row[4] && row[4] > inicio && row[4] <= fim) {
      message += '<tr>';
      message += '<td>' + row[6] + '</td>';
      message += '<td>' + row[1] + '</td>';
      message += '<td>' + row[8] + '</td>';
      message += '</tr>';
    }
  }

  message += '</table>';

  // Fim da tabela 1
  return message;
}
table {
  border-collapse: collapse;
}
td {
  border: 1px gray solid;
  padding: 4px;
  width: 5em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Demo</h2>
<button id="export">Export</button>

  • I ran, and ended up giving this error, how do I fix it? Referenceerror: "window" is not set. (line 1, file "Code")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.