SHARE
TWEET

ExportCSV

anayexm Dec 4th, 2016 (edited) 30 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Source: https://gist.github.com/mderazon/9655893
  2. // Modified for Changelog only
  3.  
  4.  
  5. function onOpen() {
  6.   var ss = SpreadsheetApp.getActiveSpreadsheet();
  7.   var csvMenuEntries = [{name: "Export to CSV", functionName: "saveAsCSV"}];
  8.   ss.addMenu("Changelog", csvMenuEntries);
  9. };
  10.  
  11. function logAnything(){  
  12.   var currentDate = Utilities.formatDate(new Date(), "GMT+8", "MM-dd-yy hh:mm aaa");
  13.   Logger.log(currentDate);  
  14. }
  15.  
  16. function saveAsCSV() {
  17.   var changelogSheetName = "Changelog";
  18.   var ss = SpreadsheetApp.getActiveSpreadsheet();
  19.   var sheets = ss.getSheets();
  20.  
  21.   var folder = DriveApp.getFolderById("0BxJv41IalMRUOHVrak1wSzJab0k");
  22.   var changelogSheet = ss.getSheetByName(changelogSheetName);
  23.   var currentDate = Utilities.formatDate(new Date(), "GMT+8", "MM-dd-yy hh:mm aaa");
  24.  
  25.   fileName = ss.getName() + "_Changelog_" + currentDate + ".csv";
  26.    
  27.   var csvFile = convertRangeToCsvFile_(fileName, changelogSheet);
  28.    
  29.   folder.createFile(fileName, csvFile);
  30.  
  31.   Browser.msgBox('Uploading file in ' + folder.getName());
  32. }
  33.  
  34. function convertRangeToCsvFile_(csvFileName, sheet) {
  35.   // get available data range in the spreadsheet
  36.   var activeRange = sheet.getDataRange();
  37.   try {
  38.     var data = activeRange.getValues();
  39.     var csvFile = undefined;
  40.  
  41.     // loop through the data in the range and build a string with the csv data
  42.     if (data.length > 1) {
  43.       var csv = "";
  44.       for (var row = 0; row < data.length; row++) {
  45.         for (var col = 0; col < data[row].length; col++) {
  46.           if (data[row][col].toString().indexOf(",") != -1) {
  47.             data[row][col] = "\"" + data[row][col] + "\"";
  48.           }
  49.         }
  50.  
  51.         // join each row's columns
  52.         // add a carriage return to end of each row, except for the last one
  53.         if (row < data.length-1) {
  54.           csv += data[row].join(",") + "\r\n";
  55.         }
  56.         else {
  57.           csv += data[row];
  58.         }
  59.       }
  60.       csvFile = csv;
  61.     }
  62.     return csvFile;
  63.   }
  64.   catch(err) {
  65.     Logger.log(err);
  66.     Browser.msgBox(err);
  67.   }
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top