如何将csv从spreadsheett导出到驱动器文件夹

时间:2018-03-07 18:13:10

标签: google-apps-script google-sheets

我有一张电子表格,里面有4张我需要的.CSV格式。到现在为止,我有一个脚本将.csv文件发送给我的电子邮件,但我需要将其自动保存到google驱动器文件夹中,您有什么想法吗?

这是我的实际脚本:

function Csv(){

  var ss = SpreadsheetApp.openById('spreadsheetID');

  var url1 = "https://docs.google.com/spreadsheets/d/spreadsheetID/gviz/tq?tqx=out:csv&sheet=Sheet1";
  var url2 = "https://docs.google.com/spreadsheets/d/spreadsheetID/gviz/tq?tqx=out:csv&sheet=Sheet2";
  var url3 = "https://docs.google.com/spreadsheets/d/spreadsheetID/gviz/tq?tqx=out:csv&sheet=Sheet3";
  var url4 = "https://docs.google.com/spreadsheets/d/spreadsheetID/gviz/tq?tqx=out:csv&sheet=Sheet4";
  var params = {
    method      : "get",
    headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions: true
  };

  var blob1 = UrlFetchApp.fetch(url1, params).getBlob();
  blob1.setName("File1" +".csv");
  var blob2 = UrlFetchApp.fetch(url2, params).getBlob(); 
  blob2.setName("File2" +".csv");
  var blob3 = UrlFetchApp.fetch(url3, params).getBlob(); 
  blob3.setName("File3" +".csv");
  var blob4 = UrlFetchApp.fetch(url4, params).getBlob(); 
  blob4.setName("File4" +".csv");


  MailApp.sendEmail("myemail@gmail.com", "Subject", "Body", {attachments: [blob1,blob2,blob3,blob4]});

}

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以查看以下脚本。希望它能帮助您按预期管理工作表。



/*
 * 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: Shotez
*/

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);
  }
}
&#13;
&#13;
&#13;

运行脚本后,只需在google驱动器中查找包含.csv文件的文件夹。