anchor.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. const colCache = require('../utils/col-cache');
  3. class Anchor {
  4. constructor(worksheet, address) {
  5. let offset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  6. this.worksheet = worksheet;
  7. if (!address) {
  8. this.nativeCol = 0;
  9. this.nativeColOff = 0;
  10. this.nativeRow = 0;
  11. this.nativeRowOff = 0;
  12. } else if (typeof address === 'string') {
  13. const decoded = colCache.decodeAddress(address);
  14. this.nativeCol = decoded.col + offset;
  15. this.nativeColOff = 0;
  16. this.nativeRow = decoded.row + offset;
  17. this.nativeRowOff = 0;
  18. } else if (address.nativeCol !== undefined) {
  19. this.nativeCol = address.nativeCol || 0;
  20. this.nativeColOff = address.nativeColOff || 0;
  21. this.nativeRow = address.nativeRow || 0;
  22. this.nativeRowOff = address.nativeRowOff || 0;
  23. } else if (address.col !== undefined) {
  24. this.col = address.col + offset;
  25. this.row = address.row + offset;
  26. } else {
  27. this.nativeCol = 0;
  28. this.nativeColOff = 0;
  29. this.nativeRow = 0;
  30. this.nativeRowOff = 0;
  31. }
  32. }
  33. static asInstance(model) {
  34. return model instanceof Anchor || model == null ? model : new Anchor(model);
  35. }
  36. get col() {
  37. return this.nativeCol + Math.min(this.colWidth - 1, this.nativeColOff) / this.colWidth;
  38. }
  39. set col(v) {
  40. this.nativeCol = Math.floor(v);
  41. this.nativeColOff = Math.floor((v - this.nativeCol) * this.colWidth);
  42. }
  43. get row() {
  44. return this.nativeRow + Math.min(this.rowHeight - 1, this.nativeRowOff) / this.rowHeight;
  45. }
  46. set row(v) {
  47. this.nativeRow = Math.floor(v);
  48. this.nativeRowOff = Math.floor((v - this.nativeRow) * this.rowHeight);
  49. }
  50. get colWidth() {
  51. return this.worksheet && this.worksheet.getColumn(this.nativeCol + 1) && this.worksheet.getColumn(this.nativeCol + 1).isCustomWidth ? Math.floor(this.worksheet.getColumn(this.nativeCol + 1).width * 10000) : 640000;
  52. }
  53. get rowHeight() {
  54. return this.worksheet && this.worksheet.getRow(this.nativeRow + 1) && this.worksheet.getRow(this.nativeRow + 1).height ? Math.floor(this.worksheet.getRow(this.nativeRow + 1).height * 10000) : 180000;
  55. }
  56. get model() {
  57. return {
  58. nativeCol: this.nativeCol,
  59. nativeColOff: this.nativeColOff,
  60. nativeRow: this.nativeRow,
  61. nativeRowOff: this.nativeRowOff
  62. };
  63. }
  64. set model(value) {
  65. this.nativeCol = value.nativeCol;
  66. this.nativeColOff = value.nativeColOff;
  67. this.nativeRow = value.nativeRow;
  68. this.nativeRowOff = value.nativeRowOff;
  69. }
  70. }
  71. module.exports = Anchor;
  72. //# sourceMappingURL=anchor.js.map