0

Would anyone have any pointers as to how to convert a Google sheets file to a .xls file through Google App Script?

Many thanks

Here's what I theorized, but the logic is flawed. I've also attempted the Blobs approach as well and it seems most files are defaulted to application/pdf.

function gs2xls() {
  var gSheets = DocsList.getFilesByType('spreadsheet');
  for(var i = 0; i < gSheets.length; i++){
    var gSheetFile = gSheets[i]; //File
    var gSheetSS = SpreadsheetApp.open(gSheetFile); //Spreadsheet of gs file
    var gSheet = gSheetSS.getActiveSheet();
    var xlsSS = SpreadsheetApp.create(gSheetFile.getName() + ".xls");
    xlsSS.setActiveSheet(gSheet);
  }  
}
  • See http://stackoverflow.com/questions/27277058/how-can-i-convert-a-google-docs-file-to-an-excel-file-with-google-docs-script/27281729#27281729. – Mogsdad Dec 03 '14 at 21:15
  • Here's a similar question with an up-to-date answer. http://stackoverflow.com/questions/31809987/google-app-scripts-email-a-spreadsheet-as-excel – Christiaan Westerbeek Oct 08 '15 at 15:08

2 Answers2

1

Google Apps Script can't export anything else than pdf natively but the document API does so you can use urlFetch with parameters to get what you want.

The code is shown with explanations in this post : Google apps script to email google spreadsheet excel version

It works pretty well.

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
0

That approach will never work because: 1) You are creating and empty google spreadsheet, not a file of type xls. 2) you are setting as active sheet an invalid sheet (from another spreadsheet whivh is not what setactivesheet does).

The most you will be able to achieve is to show the user an export link that downloads the file as xls. Google how to make the export link.

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36