Scenarios are sets of values that can substitute automatically on your worksheet and determine possible results of the formulas. You can switch between these scenarios to view the different results. DsExcel enables you to create and save different scenarios using getScenarios method of IWorksheet interface.
DsExcel provides the following methods to add and work with the scenarios:
Note: DsExcel also provides the following properties:
These methods work in Microsoft Excel when the worksheet is protected, but they will work in DsExcel without any restrictions. You can customize the operation depending on setProtection method of IWorksheet interface and values of these two methods.
Refer to the following example code to create and add scenarios to the worksheet:
Java |
Copy Code |
---|---|
// Create a new workbook. var workbook = new Workbook(); // Open an Excel file. workbook.open("What-If-Analysis-Scenarios.xlsx"); // Get the active sheet. var worksheet = workbook.getActiveSheet(); // Create and add different scenarios which represent the different discount rates. // Create a scenario with less discount rates. // The changing cells are D2:D6. List<Object> lessDiscountRatesValues = Arrays.asList(0.05, 0.02, 0.03, 0.02, 0.05); var lessDiscountRates = worksheet.getScenarios().add("Less Discount Rates", worksheet.getRange("D2:D6"), lessDiscountRatesValues); // Create a scenario with normal discount rates. // The changing cells are D2:D6. List<Object> normalDiscountRatesValues = Arrays.asList(0.1, 0.05, 0.05, 0.05, 0.1); var normalDiscountRates = worksheet.getScenarios().add("Normal Discount Rates", worksheet.getRange("D2:D6"), normalDiscountRatesValues); // Create a scenario with selling without discount. // The changing cells are D2:D6. List<Object> sellingWithoutDiscountValues = Arrays.asList(0, 0, 0, 0, 0); var sellingWithoutDiscount = worksheet.getScenarios().add("Selling Without Discount", worksheet.getRange("D2:D6"), sellingWithoutDiscountValues); // Create a scenario with bulk quantity sold. // The changing cells are E2:E6. List<Object> bulkQuantitySoldValues = Arrays.asList(1000, 1000, 1000, 1000, 1000); var bulkQuantitySold = worksheet.getScenarios().add("Bulk Quantity Sold", worksheet.getRange("E2:E6"), bulkQuantitySoldValues); // Save the workbook. workbook.save("CreateScenarios.xlsx"); |
Refer to the following example code to modify the value of a changing cell in the "Less Discount Rates" scenario:
Java |
Copy Code |
---|---|
// Modify the value of changing cell D3 of the "Less Discount Rates" scenario. lessDiscountRatesValues.set(1, 0.04); lessDiscountRates.changeScenario(worksheet.getRange("D2:D6"), lessDiscountRatesValues); |
The value of changing cell "D3" is updated from "0.02" to "0.04".
Before Modification | After Modification |
---|---|
Refer to the following example code to show "Less Discount Rates" scenario:
Java |
Copy Code |
---|---|
// Show "Less Discount Rates" scenario. worksheet.getScenarios().get("Less Discount Rates").show(); |
Refer to the following example code to show "Normal Discount Rates" scenario:
Java |
Copy Code |
---|---|
// Show "Normal Discount Rates" scenario. worksheet.getScenarios().get("Normal Discount Rates").show(); |
Refer to the following example code to delete "Less Discount Rates" scenario:
Java |
Copy Code |
---|---|
// Delete "Less Discount Rates" scenario. worksheet.getScenarios().get("Less Discount Rates").delete(); |
Refer to the following example code to hide "Less Discount Rates" scenario:
Java |
Copy Code |
---|---|
// Create a scenario with less discount rates and hide the scenario when the sheet is protected. List<Object> lessDiscountRatesValues = Arrays.asList(0.05, 0.02, 0.03, 0.02, 0.05); var lessDiscountRates = worksheet.getScenarios().add("Less Discount Rates", worksheet.getRange("D2:D6"), lessDiscountRatesValues, null, false, true); // Or // Hide "Less Discount Rates" scenario when the sheet is protected. worksheet.getScenarios().get("Less Discount Rates").setHidden(true); |
Refer to the following example code to protect "Less Discount Rates" scenario from any changes:
Java |
Copy Code |
---|---|
// Create a scenario with less discount rates and protect the scenario from any changes when the sheet is protected. List<Object> lessDiscountRatesValues = Arrays.asList(0.05, 0.02, 0.03, 0.02, 0.05); var lessDiscountRates = worksheet.getScenarios().add("Less Discount Rates", worksheet.getRange("D2:D6"), lessDiscountRatesValues, null, true); // Or // Protect "Less Discount Rates" scenario from any changes when the sheet is protected. worksheet.getScenarios().get("Less Discount Rates").setLocked(true); |
Refer to the following example code to add a comment to the "Less Discount Rates" scenario:
Java |
Copy Code |
---|---|
// Add comment to the scenario: "Created by Document Solutions for Excel". List<Object> lessDiscountRatesValues = Arrays.asList(0.05, 0.02, 0.03, 0.02, 0.05); var lessDiscountRates = worksheet.getScenarios().add("Less Discount Rates", worksheet.getRange("D2:D6"), lessDiscountRatesValues, "Created by Document Solutions for Excel"); |
Limitations
DsExcel does not support the following: