Convert Google spreadsheets to Excel using AppScript and email

I asked a question on how to convert Google spreadsheet to succeed with AppScript 2015

and was guided by the answer in this other post https://stackoverflow.com/a/464829/

It seemed like it worked, but I had a new problem: the created file is smaller than it should be, and it does not open (corrupted). Here is the complete code:

function crearBackUp() {

//pone en la variable -sheet- a la hoja activa
var sheet = SpreadsheetApp.getActiveSpreadsheet();

//obtiene la fecha para ponerle al nombre del archivo
var d = new Date();
var mes = d.getMonth() + 1;
var anio = d.getFullYear();

var nombre = "backUP_BaseDatos_BN_"+mes.toString()+anio.toString();

//creo una copia y obtengo el ID para pasarlo a Drive
var theBKP = sheet.copy(nombre);
var theID = theBKP.getId();

//Abro el arch con Drive y exporto a XLS
var file = Drive.Files.get(theID);
var url = file.exportLinks[MimeType.MICROSOFT_EXCEL];

var response = UrlFetchApp.fetch(url);      
var doc = response.getBlob();

//crea el archivo con DriveApp pasasndole el Blob
var theFile = DriveApp.createFile(doc).setName(file.title + '.xlsx');

//Manda el mail
MailApp.sendEmail('email@gmail.com', 'Subject','Mensaje',   {
  filename: nombre,
  attachments: theFile
  })
}

I wonder if this is a simpler or more direct method, since this is a pretty workaround for me.

+1
source share
1 answer

, , xls xls , ..

, , , Drive Advanced Service html excel, , html, . , UrlFetch, .

:

function crearBackUp() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();

  var d = new Date();
  var mes = d.getMonth() + 1;
  var anio = d.getFullYear();      
  var nombre = "backUP_BaseDatos_BN_"+mes.toString()+anio.toString();

  var file = Drive.Files.get(sheet.getId());
  var url = file.exportLinks[MimeType.MICROSOFT_EXCEL];

  var response = UrlFetchApp.fetch(url,
    {headers:{Authorization:"Bearer "+ScriptApp.getOAuthToken()}});
  var doc = response.getBlob();
  doc.setName(nombre+ '.xlsx');

  MailApp.sendEmail('example@gmail.com', 'Subject', 'Mensaje', {attachments:[doc]});
}
+2

All Articles