sheetclip.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * SheetClip - Spreadsheet Clipboard Parser
  3. * version 0.3
  4. *
  5. * This tiny library transforms JavaScript arrays to strings that are pasteable by LibreOffice, OpenOffice,
  6. * Google Docs and Microsoft Excel.
  7. *
  8. * Copyright 2012, Marcin Warpechowski
  9. * Licensed under the MIT license.
  10. * http://github.com/warpech/sheetclip/
  11. */
  12. /*jslint white: true*/
  13. (function (scope)
  14. {
  15. "use strict";
  16. // Class Definition
  17. function SheetClip ()
  18. {
  19. }
  20. SheetClip.prototype = Object.create(Object.prototype,
  21. {
  22. parse:
  23. {
  24. value: function (str)
  25. {
  26. var r, rlen, rows, arr = [], a = 0, c, clen, multiline, last;
  27. rows = str.split('\n');
  28. if (rows.length > 1 && rows[rows.length - 1] === '')
  29. rows.pop();
  30. for (r = 0, rlen = rows.length; r < rlen; r += 1)
  31. {
  32. rows[r] = rows[r].split('\t');
  33. for (c = 0, clen = rows[r].length; c < clen; c += 1)
  34. {
  35. if (!arr[a])
  36. arr[a] = [];
  37. if (multiline && c === 0)
  38. {
  39. last = arr[a].length - 1;
  40. arr[a][last] = arr[a][last] + '\n' + rows[r][0];
  41. if (multiline && (countQuotes(rows[r][0]) & 1))
  42. { //& 1 is a bitwise way of performing mod 2
  43. multiline = false;
  44. arr[a][last] = arr[a][last].substring(0, arr[a][last].length - 1).replace(/""/g, '"');
  45. }
  46. }
  47. else
  48. {
  49. if (c === clen - 1 && rows[r][c].indexOf('"') === 0 && (countQuotes(rows[r][c]) & 1))
  50. {
  51. arr[a].push(rows[r][c].substring(1).replace(/""/g, '"'));
  52. multiline = true;
  53. }
  54. else
  55. {
  56. arr[a].push(rows[r][c].replace(/""/g, '"'));
  57. multiline = false;
  58. }
  59. }
  60. }
  61. if (!multiline)
  62. a += 1;
  63. }
  64. return arr;
  65. },
  66. enumerable: true,
  67. configurable: false,
  68. writable: false
  69. },
  70. stringify:
  71. {
  72. value: function (arr)
  73. {
  74. var r, rlen, c, clen, str = '', val;
  75. for (r = 0, rlen = arr.length; r < rlen; r += 1)
  76. {
  77. for (c = 0, clen = arr[r].length; c < clen; c += 1)
  78. {
  79. if (c > 0)
  80. str += '\t';
  81. val = arr[r][c];
  82. if (typeof val === 'string')
  83. {
  84. if (val.indexOf('\n') > -1)
  85. {
  86. str += '"' + val.replace(/"/g, '""') + '"';
  87. }
  88. else
  89. {
  90. str += val;
  91. }
  92. }
  93. else
  94. if (val === null || val === void 0)
  95. { //void 0 resolves to undefined
  96. str += '';
  97. }
  98. else
  99. {
  100. str += val;
  101. }
  102. }
  103. str += '\n';
  104. }
  105. return str;
  106. },
  107. enumerable: true,
  108. configurable: false,
  109. writable: false
  110. }
  111. });
  112. // Private Static Functions
  113. function countQuotes(str)
  114. {
  115. return str.split('"').length - 1;
  116. }
  117. if (typeof module !== "undefined" && module.exports)
  118. module.exports = SheetClip;
  119. else
  120. scope.SheetClip = SheetClip;
  121. }(this));