Goal Seek operation provides the input value for the formula according to the desired or known result. To perform Goal Seek operation, DsExcel provides goalSeek method in IRange interface that uses goal and changingCell parameters to perform the operation.
goal parameter specifies the desired output, whereas changingCell parameter specifies the cell whose value will change.
goalSeek method is a boolean-type method and returns true when the corresponding value is found. If the value is not found, the method returns false, and the value of changingCell will remain the same.
You must adhere to the following guidelines to ensure the correct result when using goalSeek method:
Refer to the following example code to find the missing expense for the month of March using goalSeek method by setting the goal to 150000 and changingCell to B3:
Java |
Copy Code |
---|---|
// Create a new workbook. var workbook = new Workbook(); // Set MaximumIterations and MaximumChange. workbook.getOptions().getFormulas().setMaximumIterations(1000); workbook.getOptions().getFormulas().setMaximumChange(0.000001); var activeSheet = workbook.getActiveSheet(); // Add row header titles. activeSheet.getRange("A1:A4").setValue(new String[] { "January Expense", "February Expense", "March Expense", "Average" }); // January Expense. activeSheet.getRange("B1").setValue(100000); activeSheet.getRange("B1").setNumberFormat("$#,##0"); // February Expense. activeSheet.getRange("B2").setValue(180000); activeSheet.getRange("B2").setNumberFormat("$#,##0"); // March Expense. activeSheet.getRange("B3").setNumberFormat("$#,##0"); // Average of all three monthly expenses. activeSheet.getRange("B4").setFormula("=AVERAGE(B3,B2,B1)"); activeSheet.getRange("B4").setNumberFormat("$#,##0"); // Calculate the value of cell B3 using the GoalSeek method. activeSheet.getRange("B4").goalSeek(150000, activeSheet.getRange("B3")); activeSheet.getRange("A1:B4").autoFit(); // Save the Excel file. workbook.save("GoalSeek.xlsx"); |
Before Goal Seek Operation | After Goal Seek Operation |
---|---|
Limitations
There may be minor inconsistencies with Excel calculations in some cases.