将Google工作表转换为csv的问题

时间:2016-08-17 22:24:10

标签: csv google-apps-script export-to-csv

我遇到谷歌脚本问题并将我的Google工作表保存为csv。我试图使用我在互联网上找到的代码将工作表转换为csv,但是当我运行它时,文件类型显示未知而不是csv。没有它将它保存到csv google工作表是没用的。这是我找到的在线代码。不是我的代码,但我一直试图使用的代码。

   /*
     * script to export data in all sheets in the current spreadsheet as      individual csv files
     * files will be named according to the name of the sheet
    * author: Michael Derazon
    */

    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_');
      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);
    var files = folder.getFiles();
    while (files.hasNext()){
      var file = files.next();
      var destination = DriveApp.getFolderById("0B21i2lCR63QPTGdrcE9rMFgtLVk");
      destination.addFile(file);
      /*
      var pull = folder;
      pull.removeFile(file);
      */
    }


  }

  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);
  }
}

2 个答案:

答案 0 :(得分:0)

我相信此链接可能会对您有所帮助:http://www.instructables.com/id/Export-data-from-google-spreadsheet-in-required-CS/ (如果您不能使用PHP,那么这里是下载链接:http://php.net/downloads.php)。我假设你不需要脚本在javascript中。

答案 1 :(得分:0)

我明白了。看起来我要做的就是将MimeType.CSV添加到createFile方法的末尾。现在它肯定将它保存为csv。

folder.createFile(fileName, csvFile,MimeType.CSV);