-1
function exportSpreadsheet() {
  var destination = SpreadsheetApp.create('Temp');
  var dest1 = destination.getActiveSheet().setName('출고입력 거래처정보(상단)');
  var dest2 = destination.insertSheet('출고입력 폼목정보(하단)');  
  var source1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('출고입력 거래처정보(상단)').getDataRange();
  var source2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('출고입력 폼목정보(하단)').getDataRange();
  var source1Data = source1.getValues();
  var source2Data = source2.getValues(); 
  var s1Rows = source1.getNumRows();
  var s1Columns = source1.getNumColumns();  
  var s2Rows = source2.getNumRows();
  var s2Columns = source2.getNumColumns();   

  // Copy Data to Temp file
  for(var i = 0; i<s1Rows; i++){ 
  for(var j = 0; j<s1Columns; j++) {
       dest1.getRange(i+1,j+1).setValue(source1Data[i][j]);
    }
  }  

  for(var i = 0; i<s2Rows; i++){ 
    for(var j = 0; j<s2Columns; j++) {
        dest2.getRange(i+1,j+1).setValue(source2Data[i][j]);
    }
  }

  // Export to xlsx
  var ssID = destination.getId();
  var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+"?format=xlsx";

  var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};

  var response = UrlFetchApp.fetch(url, params).getBlob();
  // Save to drive
  var folder = DriveApp.getFolderById('1vDFMM2EfRe1unR6hJOsKapfJrcwxRkxy');
  folder.createFile(response).setName('출고.xlsx');

  // Delete Temp file
  DriveApp.getFilesByName('Temp').next().setTrashed(true);

}

I am trying to export two tabs from my spreadsheet and save it as a xlsx file. In doing so I have copied the two tabs to a new spreadsheet and then exporting to xlsx using the following code. I have read through almost all posts that deal with export to xlsx via Google Script and have concluded they are all variations of the code below. It seems to work but with two issues:

  1. It works but the excel file doesn't contain the data and just empty with two tabs. The URL works fine and the xlsx file contains data when I download it.

  2. The last line that deletes a temp file I created to export doesn't do its job. The file still exists in the drive and nothing has changed.

I have looked everywhere for answers but can't figure this out. Any help will be appreciated.

  • its a google apps script trying to export to excel? – Michael Hong Jun 05 '18 at 06:06
  • @MichaelHong Hovering over the tag (or clicking it) shows that [tag:gas] is for "Gnu Assembler".. Could you back up a bit and explain what you're starting with and what you're trying to end up with? There are countless different examples online - have you tried more than one? – ashleedawg Jun 05 '18 at 06:12
  • 1
    Possible duplicate of [How to convert a Google Docs-File to an Excel-File (XLSX)](https://stackoverflow.com/questions/27277058/how-to-convert-a-google-docs-file-to-an-excel-file-xlsx) – ashleedawg Jun 05 '18 at 06:17
  • @ashleedawg Oh thanks for the heads up. I have edited my comment. I have spent past two days looking around for different examples and they all seem to be variations of the code above(including the link you have posted). I am trying to save two of my tabs as a xlsx file. Other ppl seem to not have problems with the code above but for reason I am getting a blank file although the URL gives me a perfectly working xlsx file. – Michael Hong Jun 05 '18 at 06:33
  • Is the script in your question your latest version? – Tanaike Jun 05 '18 at 11:43
  • @Tanaike Yes haven't touched it since I can't seem to figure out whats wrong with it. – Michael Hong Jun 06 '18 at 01:01
  • Thank you for replying. You want to choose 2 sheets from a spreadsheet and export them as xlsx file. Is my understanding correct? – Tanaike Jun 06 '18 at 01:11
  • @Tanaike Yes that is correct. – Michael Hong Jun 06 '18 at 12:06
  • @Tanaike I have written the choosing two sheets an copying them over into a temp spreadsheet which is stored in destination and then I am using the above code to export it and save it in Drive. – Michael Hong Jun 06 '18 at 12:23
  • Thank you for replying. Can you provide the script? Because I couldn't find about the copy of 2 tags in your current script. When you provide it, it might be able to find the modification points. – Tanaike Jun 06 '18 at 23:01
  • @Tanaike Sure. I have edited the post to include the full script. – Michael Hong Jun 07 '18 at 00:47
  • Thank you for updating it. I posted an answer. Could you please confirm it? – Tanaike Jun 07 '18 at 03:13

2 Answers2

3

Try this code

function exportSpreadsheet() {

  var ssID = "yourID";

  var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
                                                        "?format=xlsx&"+
                                                        "size=0&"+
                                                        "fzr=true&"+
                                                        "portrait=false&"+
                                                        "fitw=true&"+
                                                        "gridlines=false&"+
                                                        "printtitle=true&"+
                                                        "sheetnames=true&"+
                                                        "pagenum=CENTER&"+
                                                        "attachment=true";

  var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};

  var response = UrlFetchApp.fetch(url, params).getBlob();

  var folder = DriveApp.getFolderById("YourFolder");

  folder.createFile(response);

}
1

A for Q 1 :

In your case, how about using SpreadsheetApp.flush()? For example, please put this as follows.

// Export to xlsx
SpreadsheetApp.flush();
var ssID = destination.getId();

A for Q 2 :

After you updated your script, I noticed that DriveApp.removeFile(DriveApp.getFilesByName('Temp').next()) is modified to DriveApp.getFilesByName('Temp').next().setTrashed(true). By this, the file of Temp is put in the trash. I think that you have already known about this.

If this was not what you want, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165