Get Warnings While Loading an Excel File with Node.js via C++

Possible Usage Scenarios

Sometimes the user tries to load a workbook that is somewhat corrupt but still loadable. In such cases, Aspose.Cells throws warnings while loading the workbook. You can catch these warnings by implementing the IWarningCallback interface and setting the LoadOptions.getWarningCallback() property.

Get Warnings While Loading an Excel File

The following sample code explains how to get warnings while loading an Excel file. The code loads the sample Excel file, which throws a DuplicateDefinedName warning on loading. This warning is then caught by the IWarningCallback.warning(WarningInfo) method, which prints the warning messages on the console. The code then saves the workbook as the output Excel file. If you open the sample Excel file in Microsoft Excel, it will also display this warning, as shown in the screenshot below. Please also check the console output of the code given below for better understanding.

todo:image_alt_text

Sample Code

const AsposeCells = require("aspose.cells.node");
const path = require("path");

// Implement IWarningCallback interface to catch warnings while loading workbook
class WarningCallback extends AsposeCells.IWarningCallback {
    warning(warningInfo) {
        if (warningInfo.getType() === AsposeCells.WarningType.DuplicateDefinedName) {
            console.log("Duplicate Defined Name Warning: " + warningInfo.getDescription());
        }
    }
}

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Create load options and set the WarningCallback property 
// to catch warnings while loading workbook
const options = new AsposeCells.LoadOptions();
options.setWarningCallback(new WarningCallback());

// Load the source Excel file
const book = new AsposeCells.Workbook(path.join(dataDir, "sampleDuplicateDefinedName.xlsx"), options);

// Save the workbook 
book.save(path.join(dataDir, "outputDuplicateDefinedName.xlsx"));

Console Output

Here is the console output of the above code when executed with the provided sample Excel file.

Duplicate Defined Name Warning: Name:PRINT_AREA;ReferTo:Introduction!$D$16:$D$17
Duplicate Defined Name Warning: Name:PRINT_AREA;ReferTo:Panel!$B$228
Duplicate Defined Name Warning: Name:PRINT_AREA;ReferTo:'Queries'!$D$14:$D$16