Other API Samples

Live SpreadsheetGear API Samples

Range Samples Values and Formulas Goal Seek

Achieve a specific goal from a calculated result using a simple iterative linear search. In this sample we find the loan amount based on a desired payment by entering the desired payment, interest rate and term.

// Create a new workbook and get a reference to the active sheet and its cells.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(@"GoalSeek.xlsx");
SpreadsheetGear.IWorksheet worksheet = workbook.ActiveWorksheet;
SpreadsheetGear.IRange cells = worksheet.Cells;

// Get a reference to the target cell.
SpreadsheetGear.IRange targetCell = worksheet.Cells["TargetCell"];

// Get a reference to the cell to change.
SpreadsheetGear.IRange changingCell = worksheet.Cells["ChangingCell"];

// Set up the desired monthly payment.
double goal = 1000.0;

// Call GoalSeek which returns true if the desired goal is achieved.
bool achieved = targetCell.GoalSeek(goal, changingCell);

// Display the achieved status in the worksheet.
worksheet.Cells["GoalSeekStatusCell"].Value = achieved ? "Achieved" : "Failed";
Run This Sample
Render Image

Generate an image representation of the results of this sample, which uses the SpreadsheetGear.Drawing.Image class to convert ranges, charts and shapes to images.


Download File

Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.


Supporting Files

The following files are utilized by this sample:

Related Samples