[]
        
(Showing Draft Content)

Extract Data

DsDataViewer enables you to extract data from a cell or range programmatically using getSheet, getSheetFromName, getActiveSheet, getSelections, getText, and getValue methods. The following table lists the methods with their descriptions:

Class

Methods

Description

DsDataViewer

getSheet(index)

Fetches the specified sheet based on the index.

getSheetFromName(name)

Fetches the sheet with the specified name.

getActiveSheet()

Fetches the active sheet.

WorkSheet

getSelections()

Retrieves the selections in the current sheet.

getText(row,column)

Retrieves formatted text in the cell based on the desired row and column index.

getValue(row,column)

Retrieves unformatted data from the specified cell based on the desired row and column index.

Refer to the following example codes that demonstrate how to extract data programmatically:

Example 1: Extract Data from Specified Cell

// Fetch and load the XLSX file.
fetch("/Files/12-month cash flow statement1.xlsx").then(response => {
    response.blob().then(res => {

        // Open the file in DsDataViewer.
        viewer.openFile(res, FileType.XLSX).then(
            // Wait for the viewer to load the file completely.
            res => {
                // Get the active sheet.
                var sheet = viewer.getActiveSheet();

                // Get value from the specified cell.
                let value = sheet.getValue(1, 0);

                // Get text from the specified cell.
                let text = sheet.getText(1, 0);

                // Display the value and text in the console.
                console.log("value:" + value + " text:" + text);
            }
        );
    });
})

Example 2: Extract Data from Selected Cell Range:

// Fetch and load the XLSX file.
fetch("/Files/12-month cash flow statement1.xlsx").then(response => {
    response.blob().then(res => {

        // Open the file in DsDataViewer.
        viewer.openFile(res, FileType.XLSX).then(

            // Wait for the viewer to load the file completely.
            res => {
                // Get the active sheet.
                var sheet = viewer.getActiveSheet();
                var values = [];
                var texts = [];

                // Get selected range.
                for (const selection of sheet.getSelections()) {

                    // When selecting an entire column, row will be -1. 
                    let row = selection.row < 0 ? 0 : selection.row;

                    // When selecting an entire row, col will be -1.
                    let col = selection.col < 0 ? 0 : selection.col;;
                    let rowCount = selection.rowCount;
                    let colCount = selection.colCount;
                    for (let i = row; i < row + rowCount; i++) {
                        for (let j = col; j < col + colCount; j++) {
                            values.push(sheet.getValue(i, j));
                            texts.push(sheet.getText(i, j));
                        }
                    }

                    // Display the value and text in the console.
                    let log = `Row: ${row}, Col: ${col}, RowCount: ${rowCount}, ColCount: ${colCount}, Values: ${values.join(", ")}, Texts: ${texts.join(", ")}`;
                    console.log(log);
                    values = [];
                    texts = [];
                };
            }
        );
    });
})