shared-formula.js 1.4 KB

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