Excel goal seek over an API
To say it at the start: SpreadAPI has no goal-seek endpoint and no solver endpoint. A service takes input values, calculates the workbook and returns the outputs. It does not search for an input.
You can still answer the goal-seek question, "which input gives me this result?", from the outside. This post shows three ways. Two of them were run on 13 November 2026 against the public demo quote calculator of the SpreadAPI homepage; the code is the code we ran, and the iterations are the real ones. {serviceId} stands for the ID of the service.
Why there is no goal seek in a service
Goal Seek and Solver are tools of the Excel application. They are not formulas. They change a cell again and again and watch another cell. A SpreadAPI service runs the formulas of your workbook: each call sets the inputs for this one call, calculates and reads the outputs. The published workbook itself is not changed by a call.
So the search has to happen in one of two places: in the code that calls the service, or in the workbook as a formula.
The question
The demo service prices a software quote from licenses, edition and discount. For 50 Business licences with 5% discount it returns a total of 25,716.06.
The question for this post: which discount brings the total for 50 Business licences to 24,000?
The discount may be between 0 and 0.5 (50%). That is the search range.
Way 1: Bisection over single calls
Bisection halves the range in every step. Calculate the middle; if the total is still above the target, the discount has to be larger, so continue in the upper half, otherwise in the lower half.
// Goal seek from the outside: bisection over single calls
const URL =
'https://spreadapi.io/api/v1/services/{serviceId}/execute?flatten=true';
async function total(discount) {
const response = await fetch(URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
inputs: { licenses: 50, edition: 'Business', discount }
})
});
const data = await response.json();
if (!response.ok) throw new Error(data.message || data.error);
return data.result.total;
}
async function goalSeek(target, low, high, tolerance, maxSteps) {
// the total falls when the discount rises
const atLow = await total(low);
const atHigh = await total(high);
console.log(`discount ${low} -> ${atLow}, discount ${high} -> ${atHigh}`);
if (target > atLow || target < atHigh) {
throw new Error('Target is outside the range the inputs allow');
}
for (let step = 1; step <= maxSteps; step++) {
const middle = Math.round(((low + high) / 2) * 10000) / 10000;
const value = await total(middle);
console.log(`step ${step}: discount ${middle} -> total ${value}`);
if (Math.abs(value - target) <= tolerance) return { discount: middle, total: value, step };
if (value > target) low = middle; else high = middle;
}
return { discount: (low + high) / 2, total: null, step: maxSteps };
}
console.log(await goalSeek(24000, 0, 0.5, 10, 10));The output of the run:
discount 0 -> 27069.47, discount 0.5 -> 13534.74
step 1: discount 0.25 -> total 20302.17
step 2: discount 0.125 -> total 23685.82
step 3: discount 0.0625 -> total 25377.58
step 4: discount 0.0938 -> total 24530.35
step 5: discount 0.1094 -> total 24108.08
step 6: discount 0.1172 -> total 23896.89
step 7: discount 0.1133 -> total 24002.55
{ discount: 0.1133, total: 24002.55, step: 7 }A discount of 11.33% gives a total of 24,002.55. That is within the tolerance of 10 we asked for, after nine calls: two for the ends of the range and seven steps.
What to know about this way:
- It needs no knowledge about the model. It only assumes that the output moves in one direction when the input rises. Here the total falls when the discount rises.
- Every step is one call. A tighter tolerance costs more calls: each step halves the remaining range.
- The first two calls check that the target can be reached at all. Without that check the loop runs to its end and returns nonsense.
- The steps run one after the other, because each one depends on the result of the one before.
Way 2: A grid in one batch, then interpolate
The second way asks for several points at once. The discovery address of a service has a batch action that takes 2 to 20 scenarios in one request. Six points across the range show where the target lies; a straight line between the two neighbours gives the guess; a second request checks it.
// Goal seek from the outside: a grid in one batch, then interpolate
const DISCOVERY = 'https://spreadapi.io/d/{serviceId}';
async function batch(discounts) {
const response = await fetch(DISCOVERY, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
action: 'batch',
scenarios: discounts.map((discount) => ({
label: String(discount),
inputs: { licenses: 50, edition: 'Business', discount }
}))
})
});
const data = await response.json();
if (!data.success) throw new Error(data.error);
return data.scenarios.map((s) => ({
discount: Number(s.label),
total: s.outputs.find((o) => o.name === 'total').value
}));
}
const target = 24000;
// 1. Six points across the allowed range, one request
const grid = await batch([0, 0.1, 0.2, 0.3, 0.4, 0.5]);
console.table(grid);
// 2. The two neighbours that enclose the target
const index = grid.findIndex((point) => point.total < target);
const [a, b] = [grid[index - 1], grid[index]];
// 3. A straight line between them
const share = (a.total - target) / (a.total - b.total);
const guess = Math.round((a.discount + share * (b.discount - a.discount)) * 10000) / 10000;
// 4. Check the guess, together with a neighbour, in a second request
console.table(await batch([guess, Math.round((guess + 0.0001) * 10000) / 10000]));The output of the run:
┌─────────┬──────────┬──────────┐
│ (index) │ discount │ total │
├─────────┼──────────┼──────────┤
│ 0 │ 0 │ 27069.47 │
│ 1 │ 0.1 │ 24362.52 │
│ 2 │ 0.2 │ 21655.58 │
│ 3 │ 0.3 │ 18948.63 │
│ 4 │ 0.4 │ 16241.68 │
│ 5 │ 0.5 │ 13534.74 │
└─────────┴──────────┴──────────┘
┌─────────┬──────────┬──────────┐
│ (index) │ discount │ total │
├─────────┼──────────┼──────────┤
│ 0 │ 0.1134 │ 23999.85 │
│ 1 │ 0.1135 │ 23997.14 │
└─────────┴──────────┴──────────┘A discount of 11.34% gives 23,999.85. That took two requests with eight calculations in total, and it landed closer than the bisection.
What to know about this way:
- The straight line worked so well because in this workbook the total falls evenly with the discount: every 10 points of discount lower it by about 2,707. Your model may not be that kind. With price steps, caps or minimum fees between two grid points, the line misses. Then send a second, finer grid between the two neighbours instead of interpolating.
- The grid also shows you the shape of the model. If the totals do not move in one direction, you see it here, and bisection would have been the wrong tool.
- Every scenario counts as one call of the service. A batch saves requests and waiting time, not calls.
- A batch holds 20 scenarios at most.
Way 3: Put the inverse formula into the workbook
If the question comes up often, the cleanest answer is a formula. The person who owns the workbook adds an input cell for the target and a result cell that calculates the input needed. Then one call answers the question, and the rule sits where the rest of the logic is.
For the discount question the formula is short, because the total falls evenly with the discount:
=1 - TargetTotal / TotalWithoutDiscountWith the numbers from the runs above that is 1 − 24,000 / 27,069.47 = 0.1134, the value the grid found. We calculated this by hand from the measured totals; the demo workbook does not contain such a cell.
What to know about this way:
- It only works where the model can be turned around with formulas. For a loan that is often possible: Excel has
PVfor "which loan fits this payment" andRATEandNPERfor the rate and the term. For a model with lookups and steps it may not be possible in a closed form. - It is a change to the workbook. It has to be imported and published like any other change.
- It is the only one of the three ways that an AI assistant can use with a single tool call.
Which way when
- A one-off question, an unknown model: bisection. Little code, no assumptions beyond one direction.
- You want to see the curve, or the model has steps: the batch grid, with a finer second grid.
- The question is part of the product: the inverse formula in the workbook.
What about several variables and constraints?
That is what Solver does in Excel, and there is no counterpart in SpreadAPI. If you need it, the search runs in your code, with an optimisation library of your choice, and the service is the function it evaluates. Count the calls before you start: a search over several variables can need many evaluations, each one is a call toward the month's calls of the plan, and a service answers at most 1,000 calls per minute. See Call limits.
AI assistants and goal seek
An assistant that is connected to a service over MCP has a tool for one calculation and a tool that compares 2 to 20 scenarios in one call. With those it can try several values for a "what would I need" question. Whether it searches as carefully as the code above is up to the model. If your users ask such questions regularly, give the assistant a direct answer with way 3.
An assistant cannot change cells of your published workbook through a service, and the AI Areas tab of the editor does not offer that either: adding an area is marked "coming soon" in the app. What works today is described in AI areas.
Things to keep in mind
- Stored results. Identical inputs are answered from a stored result for up to 15 minutes. For a search that is welcome. It still counts as a call.
- Rounding. The code rounds the discount to four decimals, so that it does not send values like 0.11328125. Decide which precision makes sense for your input before you search.
- Limits of the inputs. A value outside the allowed range of an input is refused with HTTP 400. Keep the search range inside the limits of the service; the discovery address lists them.
- A target that cannot be reached. The bisection script checks that the target lies between the results at the ends of the range. The grid script does not: if no grid point lies below the target, it fails. Add that check before you use it for real.