如何在方程中正确插入变量

时间:2015-04-30 16:46:06

标签: google-apps-script google-sheets

我有以下脚本公式,网站上的其他人帮助我开发。我正在制作一个有30多个按钮的电子表格,但是使用这个相同的公式,只有几个值被换出。

//Set Formula in Column H for Mastic Calculation.

sheet.getRange(lastrow+1,8).setFormula('=IF(AND($F$21="Flow Line",F' + (lastrow+1) + '/8=TRUNC(F' + (lastrow+1) + '/8)),(((F' + (lastrow+1) + '/8)+(G' + (lastrow+1) + '/2))*3),IF(AND($F$21="Flow Line",(F' + (lastrow+1) + '-6)/8=TRUNC((F' + (lastrow+1) + '-6)/8)),((((F' + (lastrow+1) + '-6)/8)+1+(G' + (lastrow+1) + '/2))*3),IF(AND($F$21="Flow Line",(F' + (lastrow+1) + '-12)/8=TRUNC((F' + (lastrow+1) + '-12)/8)),((((F' + (lastrow+1) + '-12)/8)+2+(G' + (lastrow+1) + '/2))*3),if(AND($F$21="Flow Line",(F' + (lastrow+1) + '-18)/8=TRUNC((F' + (lastrow+1) + '-18)/8)),((((F' + (lastrow+1) + '-18)/8)+3+(G' + (lastrow+1) + '/2))*3),IF(AND($F$21="Full Mastic",F' + (lastrow+1) + '/8=TRUNC(F' + (lastrow+1) + '/8)),(((F' + (lastrow+1) + '/8)+(G' + (lastrow+1) + '/2))*5),if(AND($F$21="Full Mastic",(F' + (lastrow+1) + '-6)/8=TRUNC((F' + (lastrow+1) + '-6)/8)),((((F' + (lastrow+1) + '-6)/8)+3+(G' + (lastrow+1) + '/2))*5),if(AND($F$21="Full Mastic",(F' + (lastrow+1) + '-12)/8=TRUNC((F' + (lastrow+1) + '-12)/8)),((((F' + (lastrow+1) + '-12)/8)+3+(G' + (lastrow+1) + '/2))*5),if(AND($F$21="Full Mastic",(F' + (lastrow+1) + '-18)/8=TRUNC((F' + (lastrow+1) + '-18)/8)),((((F' + (lastrow+1) + '-18)/8)+3+(G' + (lastrow+1) + '/2))*5),0))))))))');

我希望能够换掉几个整数并使它们变量。我试过在脚本的开头插入这个:

//Set Variable for Flow Line Mastic

halfmastic = 3

以及其他变体,例如将其设置为var,但当我将其插入代替公式中的整数时,我似乎无法使其正常工作。

1 个答案:

答案 0 :(得分:0)

看起来如下。

var halfMastic = 3;

...' + (lastrow+1) + '-18)/8)+' + halfMastic + '+(G' + (lastrow+1) + '/2))*5),0...