How do you get the formula from a cell instead of the value?

前端 未结 5 2218
执笔经年
执笔经年 2020-12-05 14:55

How do you get the literal value (the formula) from a cell instead of the result value?

EXAMPLE DESIRED:

  • A2: \"Foo\"
  • B3: \"=A2\" - so it
5条回答
  •  一向
    一向 (楼主)
    2020-12-05 15:28

    I had the same problem and tried to use the formula that Rubén created, but had a limitation. I couldn't use these formula inside of another one (I was trying to make a mid()). So made a different approach that worked:

    function CELLFORMULA(reference) {
      var ss = SpreadsheetApp;
      var sheet = ss.getActiveSheet();
      var formula = ss.getActiveRange().getFormula();
      re = /cellformula\((.*)\);/g;
      args = re.exec(formula);
      try {
        var range = sheet.getRange(args[1]);
      }
      catch(e) {
        throw new Error(args + ' is not a valid range');
      }
      return range.getFormula();
    }
    

提交回复
热议问题