anchor.js 2.5 KB

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