问题
I have not found division of real numbers (ie, /
) in worksheet functions. As a consequence, to evaluate =SUM(2,SUM(30,40)/3)
, we cannot use one expression ctx.workbook.functions.sum(1,ctx.workbook.functions.sum(30,40)/3)
;
we have to do ctx.sync
two times:
function test () {
Excel.run(function (ctx) {
var result = ctx.workbook.functions.sum(30,40);
var result2;
result.load();
return ctx.sync()
.then(function () {
result2 = ctx.workbook.functions.sum(1,result.value/3);
result2.load(); })
.then(ctx.sync)
.then(function () {
console.log(result2.value); });
});
}
That means, if there are several /
in one expression, we have to use even more ctx.sync
to evaluate it, which is very tedious (and especially hard to be automatically constructed).
So it would be really great to either find the worksheet function or a workaround for /
, so that we could still evaluate an expression containing /
in one step.
PS: it seems that there is no worksheet function for +
, *
, -
either, but we could use workarounds: sum
for +
, product
for *
, and sum(..., product(-1, ...)
for -
.
回答1:
One solution is to use a combination of the product
function and the power
function with the exponent -1. This solution will work even if the denominator (divisor) is a variable and not a constant.
var myNumerator = 888, myDenominator = 4;
var funcs = ctx.workbook.functions;
var result = funcs.product(myNumerator,funcs.power(myDenominator,-1));
In your specific example to evaluate =SUM(2,SUM(30,40)/3)
, the code would be:
result = funcs.sum(2,funcs.product(funcs.sum(30,40),funcs.power(3,-1)));
回答2:
At least for dividing by a constant (e.g., 3), isn't that the same as product(something, .33333333)
来源:https://stackoverflow.com/questions/38337870/worksheet-function-for-division-of-real-numbers-ie