Generate PDF from a Google Spreadsheet

1

I would like to know how I generate a spreadsheet from a spreadsheet in google .

I already have a script done, to send emails every day, with information, however, I would like these emails to be downloaded as .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 horizontally, which command should I apply?

    
asked by anonymous 09.12.2016 / 18:11

1 answer

0

I made this code that should meet your need I was not able to execute exactly as you want but I assumed from the beginning that your function returns the html with the table ready ....

I used this jsFidle for help

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><buttonid="export">Export</button>
    
09.12.2016 / 18:21