shared-formula.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. const colCache = require('./col-cache');
  3. // const cellRefRegex = /(([a-z_\-0-9]*)!)?[$]?([a-z]+)[$]?([1-9][0-9]*)/i;
  4. const replacementCandidateRx = /(([a-z_\-0-9]*)!)?([a-z0-9_$]{2,})([(])?/gi;
  5. const CRrx = /^([$])?([a-z]+)([$])?([1-9][0-9]*)$/i;
  6. function slideFormula(formula, fromCell, toCell) {
  7. const offset = colCache.decode(fromCell);
  8. const to = colCache.decode(toCell);
  9. return formula.replace(replacementCandidateRx, (refMatch, sheet, sheetMaybe, addrPart, trailingParen) => {
  10. if (trailingParen) {
  11. return refMatch;
  12. }
  13. const match = CRrx.exec(addrPart);
  14. if (match) {
  15. const colDollar = match[1];
  16. const colStr = match[2].toUpperCase();
  17. const rowDollar = match[3];
  18. const rowStr = match[4];
  19. if (colStr.length > 3 || colStr.length === 3 && colStr > 'XFD') {
  20. // > XFD is the highest col number in excel 2007 and beyond, so this is a named range
  21. return refMatch;
  22. }
  23. let col = colCache.l2n(colStr);
  24. let row = parseInt(rowStr, 10);
  25. if (!colDollar) {
  26. col += to.col - offset.col;
  27. }
  28. if (!rowDollar) {
  29. row += to.row - offset.row;
  30. }
  31. const res = (sheet || '') + (colDollar || '') + colCache.n2l(col) + (rowDollar || '') + row;
  32. return res;
  33. }
  34. return refMatch;
  35. });
  36. }
  37. module.exports = {
  38. slideFormula
  39. };
  40. //# sourceMappingURL=shared-formula.js.map