Guest User

Google Sheets TSV Export Menu Code

a guest
Dec 3rd, 2015
174
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×