Using the google drive API to download a spreadsheet in csv format

前端 未结 7 762
执笔经年
执笔经年 2020-11-30 19:10

I\'m sorry if this is an obvious question, I\'m still pretty new to the API. I\'m using the python drive api library, and trying to download a google spreadsheet as a csv.

7条回答
  •  一整个雨季
    2020-11-30 20:00

    Not sure if it's what the OP needed, but in the new Google Sheets version it seems that it became a little hard to hot link a csv version of your spreadsheet.

    In case you are interested in a Google apps script that will export all sheets in a spreadsheet to individual csv files (instead of downloading each one individually), Here you go:

    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 = DocsList.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);
      }
    }
    

    Note: This script uses the DocsList.createFile() method, which is only available for Google Apps accounts.

    If you need further explanation, go here: http://drzon.net/export-all-google-sheets-to-csv/

提交回复
热议问题