[Vuejs]-Can I make my Vue component accept dynamic formula like excel?

0👍

You will have to write some formula parsing logic.
Let suppose you want to implement a formula to sum values of X,Y columns and write that in Z column. Formula you would write in Z column would be something like X[1]+Y[1]. Now in JavaScript, its upto you how you parse it.

One way would be

getInput(formula) {
 const arr = formula.split('+')
 const isPositiveOperation = arr.length > 1
 if(isPositiveOperation) {
  const xIndex = arr[0].split('X[')[0].split(']')
  const yIndex = arr[1].split('X[')[0].split(']')

  const xValue = this.xItems[xIndex]
  const yValue = this.yItems[yIndex]

  return xValue + yValue
 }
}

This is very basic of course. You can look into regular expressions for more robust solutions.

Leave a comment