0

I want to download all the sheets of an excel document in csv format. presently, only the first sheet data is accessible. code is given below:

function downloadGDriveFile (file) {
var request1 = gapi.client.drive.files.export({
  'fileId': '1VLFgD8CNvXTVdHKyBdZPUmgFmhWMzg7qWLbxzLUTtSo',
  'mimeType': 'text/csv'
})
request1.then(function(response) {
  //console.log(response);
}, function(err) {
  console.log('Error');
  console.log(err.result.error);
});
}

presently I am using a static id. The excel gave two sheets. But the code shows only first sheet data. how to download all the sheets with in a workbook.

techie
  • 13
  • 6
  • duplicate of http://stackoverflow.com/questions/43526270/how-to-export-specific-sheet-using-new-v3-google-drive-api See my answer there for possible workarounds – pinoyyid Apr 21 '17 at 07:44
  • @pinoyyid your given link is very helpful. But I'm able to do the download multiple sheets. Can you help me with some code. – techie Apr 21 '17 at 11:25

1 Answers1

1

I've read from this tutorial blog that this is indeed the behavior when you try to export files.

However, there's a way to get all sheets from your spreadsheet using Apps Script. After executing custom menu "csv->export as csv file", the sheets will be placed inyour Google Drive inside a folder. Here's the code from the blog:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var csvMenuEntries = [{name: "export as csv files", functionName: "saveAsCSV"}];
  ss.addMenu("csv", csvMenuEntries);
};

function saveAsCSV() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  // create a folder from the name of the spreadsheet
  var folder = DriveApp.createFolder(ss.getName().toLowerCase().replace(/ /g,'_') + '_csv_' + new Date().getTime());
  for (var i = 0 ; i < sheets.length ; i++) {
    var sheet = sheets[i];
    // append ".csv" extension to the sheet name
    fileName = sheet.getName() + ".csv";
    // convert all available sheet data to csv format
    var csvFile = convertRangeToCsvFile_(fileName, sheet);
    // create a file in the Docs List with the given name and the csv data
    folder.createFile(fileName, csvFile);
  }
  Browser.msgBox('Files are waiting in a folder named ' + folder.getName());
}

function convertRangeToCsvFile_(csvFileName, sheet) {
  // get available data range in the spreadsheet
  var activeRange = sheet.getDataRange();
  try {
    var data = activeRange.getValues();
    var csvFile = undefined;

    // loop through the data in the range and build a string with the csv data
    if (data.length > 1) {
      var csv = "";
      for (var row = 0; row < data.length; row++) {
        for (var col = 0; col < data[row].length; col++) {
          if (data[row][col].toString().indexOf(",") != -1) {
            data[row][col] = "\"" + data[row][col] + "\"";
          }
        }

        // join each row's columns
        // add a carriage return to end of each row, except for the last one
        if (row < data.length-1) {
          csv += data[row].join(",") + "\r\n";
        }
        else {
          csv += data[row];
        }
      }
      csvFile = csv;
    }
    return csvFile;
  }
  catch(err) {
    Logger.log(err);
    Browser.msgBox(err);
  }
} 
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • how to use this code. I tired to merge this code with my code But I did'nt get the other sheets expect first one. Help with some code. – techie Apr 26 '17 at 12:18