Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

SAP Screen Personas


 

Scripting: A Library of Useful Functions


SAP SCREEN PERSONAS KNOWLEDGE BASE - by Regina Sheynblat , Kranthi Kumar Muppala

Purpose

This article will catalog a set of functions that will aid in implementing scripts with complex requirements.

DISCLAIMER: THE FUNCTIONS LISTED BELOW ARE FOR REFERENCE ONLY AND NOT TO BE USED IN A PRODUCTION ENVIRONMENT WITHOUT PROPER TESTING AND APPROPRIATE MODIFICATIONS. KINDLY USE THE SCN FORUM IF YOU NEED SUPPORT OR TO REPORT BUGS.

Overview

The scripts generated using the script recorder work fine for scenarios like navigating between scripts, copying and pasting of values, pushing buttons, etc. However, requirements like copying content from a table control or finding a row of a table control that matches a criteria require advanced JavaScript knowledge and an understanding of the SAP Screen Personas API. The 'Related Content' section has some links to JavaScript tutorials and information on accessing the API documentation on the SAP Screen Personas server.

The functions can be stored in one or more file and used as global scripts to be reused across flavors. Refer to the KB article on global scripts in the 'Related Content' section.

Table Control and ALV Grid View Functions

Copy Contents

copyTableContents
/**
 * Returns an array of rows containing information from the table control.
 * @param {string} sTableControlId - The ID of the table control.
 * @returns {Array[Object]} The contents array.
 */
copyTableContents = function(sTableControlId) {
    // Fetch the table and its columns
    var oTableControl = session.findById(sTableControlId),
        aColumns = oTableControl.columns,
        aContents = [],
        nTopRow,
        oRow,
        sColumnName,
        nRowIndex,
        i;
    if (oTableControl.rowCount > 0) {
        //Set the visible row to 0
        oTableControl.firstVisibleRow = 0;
        //Get the Max visible row number
        nTopRow = oTableControl.visibleRowCount - 1;
        //Loop through all the rows
        for (nRowIndex = 0; nRowIndex < oTableControl.rowCount; nRowIndex++) {
            oRow = {};
            if (nRowIndex > nTopRow) {
                // Set the first visible row to the next set of rows. If the next set goes beyond the maximum rows,
                // adjust it so that the set's last row is the table's last row.
                if (nTopRow + oTableControl.visibleRowCount > oTableControl.rowCount) {
                    oTableControl.firstVisibleRow = oTableControl.rowCount - oTableControl.visibleRowCount;
                } else {
                    oTableControl.firstVisibleRow = nTopRow + 1;
                }
                nTopRow += oTableControl.visibleRowCount;
            }
            // Populate the row information.
            for (i = 0; i < aColumns.length; i++) {
                sColumnName = aColumns.elementAt(i).name;
                oRow[sColumnName] = oTableControl.getCellValue(nRowIndex, sColumnName);
            }
            // Break after the first blank row - the values usually contain all underscores like "____" for a 4 character column.
            if (oRow[aColumns.elementAt(0).name] && !oRow[aColumns.elementAt(0).name].replace(/_/g, "")) {
                break;
            }
            aContents.push(oRow);
        }
    }
    return aContents;
};

 

Print Column Names in Browser Console

printColumnNames
/**
 * Prints the columns names of the provided table control.
 * @param {string} sTableControlId - The ID of the table control.
 * @returns {string} The column names.
 */
printColumnNames = function(sTableControlId) {
    var oTableControl = session.findById(sTableControlId),
        aColumns = oTableControl.columns,
        i,
        sResult = "";
    for (i = 0; i < aColumns.length; i++) {
        sResult += "Column " + i + ": " + aColumns.elementAt(i).name + "\n";
        console.log("Column " + i + ": " + aColumns.elementAt(i).name);
    }
    return sResult;
};

 

Find Row Index Based on a Value in a Column

findTableRowIndex
/**
 * Returns a table control row index of a cell that matches the value and column name provided.
 * @param {string} sTableControlId - The ID of the table control.
 * @param {string} sColumnName - The column name.
 * @param {string} sValue - The value to be matched.
 * @returns {number} The row index of the cell.
 */
findTableRowIndex = function(sTableControlId, sColumnName, sValue) {
    // Fetch the table and its columns
    var oTableControl = session.findById(sTableControlId),
        nTopRow,
        nRowIndex,
        i,
        sCellValue;
    if (oTableControl.rowCount > 0) {
        //Set the visible row to 0
        oTableControl.firstVisibleRow = 0;
        //Get the Max visible row number
        nTopRow = oTableControl.visibleRowCount - 1;
        //Loop through all the rows
        for (nRowIndex = 0; nRowIndex < oTableControl.rowCount; nRowIndex++) {
            if (nRowIndex > nTopRow) {
                // Set the first visible row to the next set of rows. If the next set goes beyond the maximum rows,
                // adjust it so that the set's last row is the table's last row.
                if (nTopRow + oTableControl.visibleRowCount > oTableControl.rowCount) {
                    oTableControl.firstVisibleRow = oTableControl.rowCount - oTableControl.visibleRowCount;
                } else {
                    oTableControl.firstVisibleRow = nTopRow + 1;
                }
                nTopRow += oTableControl.visibleRowCount;
            }
            sCellValue = oTableControl.getCellValue(nRowIndex, sColumnName);
            // Trim any whitespaces to compare any right aligned columns
            sCellValue = sCellValue ? sCellValue.replace(/ /g, "") : "";
            //console.log("sCellValue:" + sCellValue);
            if (sCellValue === sValue) {
                return nRowIndex;
            }
            // Break after the first blank row - the values usually contain all underscores like "____" for a 4 character column.
            if (sCellValue && !sCellValue.replace(/_/g, "")) {
                break;
            }
        }
    }
    return -1;
};

 

Find Row Index of the First Empty Row

findFirstEmptyRowIndex
/**
 * Returns the first table control row index where the first cell is empty.
 * @param {string} sTableControlId - The ID of the table control.
 * @param {string} sColumnName - The column name.
 * @returns {number} The row index of the cell.
 */
findFirstEmptyRowIndex = function(sTableControlId, sColumnName) {
    // Fetch the table and its columns
    var oTableControl = session.findById(sTableControlId),
        nTopRow,
        nRowIndex,
        i,
        sCellValue;
    if (oTableControl.rowCount > 0) {
        //Set the visible row to 0
        oTableControl.firstVisibleRow = 0;
        //Get the Max visible row number
        nTopRow = oTableControl.visibleRowCount - 1;
        //Loop through all the rows
        for (nRowIndex = 0; nRowIndex < oTableControl.rowCount; nRowIndex++) {
            if (nRowIndex > nTopRow) {
                // Set the first visible row to the next set of rows. If the next set goes beyond the maximum rows,
                // adjust it so that the set's last row is the table's last row.
                if (nTopRow + oTableControl.visibleRowCount > oTableControl.rowCount) {
                    oTableControl.firstVisibleRow = oTableControl.rowCount - oTableControl.visibleRowCount;
                } else {
                    oTableControl.firstVisibleRow = nTopRow + 1;
                }
                nTopRow += oTableControl.visibleRowCount;
            }
            sCellValue = oTableControl.getCellValue(nRowIndex, sColumnName);
            // Trim any whitespaces to compare any right aligned columns
            sCellValue = sCellValue ? sCellValue.replace(/ /g, "") : "";
            //console.log("sCellValue:" + sCellValue);
            // Return after the first blank row - the values usually contain all underscores like "____" for a 4 character column.
            if (!sCellValue || (sCellValue && !sCellValue.replace(/_/g, ""))) {
                return nRowIndex;
            }
        }
    }
    return -1;
};

Related Content

Related Search Terms:

SAP Screen Personas, Scripting

Related SAP Notes/KBAs

Javascript Tutorial from CodeAcademy

Javascript Tutorial from w3Schools

Scripting: Including Global Javascript Libraries