Guest User

Google Sheets TSV Export Code

a guest
Feb 23rd, 2016
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ConvertToTSV(sheet) {
  2. var ws = sheet.getDataRange();
  3. try {
  4. var data = ws.getValues();
  5. // Loop through the data in the range and build a string with the TSV data
  6. var csv = "";
  7. for (var row = 0; row < data.length; row++) {
  8. // Join each row's columns
  9. // Add a carriage return to end of each row
  10. csv += data[row].join("\t") + "\r\n";
  11. }
  12. return csv;
  13. }
  14. catch(err) {
  15. Logger.log(err);
  16. Browser.msgBox(err);
  17. }
  18. }
  19.  
  20. function BulkTSVExport() {
  21. var myFolder = DriveApp.createFolder(SpreadsheetApp.getActiveSpreadsheet().getName());
  22. var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  23. for (var i = 0; i < sheets.length; i++) {
  24. var csv = ConvertToTSV(sheets[i]);
  25. myFolder.createFile(sheets[i].getName() + ".tsv", csv);
  26. }
  27.  
  28. }
  29.  
  30. function TSVExport() {
  31. var sheet = SpreadsheetApp.getActiveSheet();
  32. var csv = ConvertToTSV(sheet);
  33. DriveApp.createFile(sheet.getName() + ".tsv", csv);
  34. }
  35.  
  36. function onOpen() {
  37. var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  38. var entries = [
  39. {name : "Export Current Sheet to TSV", functionName : "TSVExport"},
  40. {name : "Export All Sheets to TSV", functionName : "BulkTSVExport"}
  41. ];
  42. spreadsheet.addMenu("TSV", entries);
  43. }
RAW Paste Data