office-scriptsms-office-script

Office Script: different array length results between getValues() and looping


I'm stuck with the logic in Office Scripts. Following function is running on a worksheet

function getKontrolDistinctShipKeys(kontrolSheet: ExcelScript.Worksheet): string[] {

    const kontrolSheetUsedRange = kontrolSheet.getUsedRange();
    const kontrolSheetRowCount = kontrolSheetUsedRange.getRowCount();
    const kontrolSheetColumnCount = kontrolSheetUsedRange.getColumnCount();

    const kontrolSheetDataRange = kontrolSheet.getRangeByIndexes(1, 0, kontrolSheetRowCount - 1, kontrolSheetColumnCount)
    if (kontrolSheetRowCount <= 1) {
        return []; // Return empty array if no data rows are present
    }
    
    // getValues()  
    
    const kontrolShipKeys: (string )[] = kontrolSheetDataRange.getValues();

    console.log(`fn getKontrolDistinctShipKeys kontrolShipKeys Length: ${kontrolShipKeys.length}`)
    // output: fn getKontrolDistinctShipKeys kontrolShipKeys Length: 100
    

    // for loop
    let kontrolShipKeysArr: (string)[] = []; 
    for (let colCount=1; colCount <= kontrolSheetColumnCount; colCount++){
            for (let rowCount = 1; rowCount < kontrolSheetRowCount; rowCount++){
                let kontrolShipKey = kontrolSheetDataRange.getCell(rowCount, colCount).getValue();
                kontrolShipKeysArr.push(kontrolShipKey)
    }
    }
        console.log(`fn getKontrolDistinctShipKeys kontrolShipKeysArr length: ${kontrolShipKeysArr.length}`)
    //output: fn getKontrolDistinctShipKeys kontrolShipKeysArr length: 5800
}

With the for loop, returning 5800 values is fine because it includes the nulls in the range. However, it is very compute intensive, and according to Microsoft's documentation getValues() should get the job done and return the raw values in the range.

I'm not able to wrap my head around this different behavior. Even if I manipulate the Worksheet and add/remove values to/from the cells, the getValues() statement always return 100 items.

Any ideas why getValues() gives such a result?

Thank you!

Cheers


Solution

  • 
    function main(workbook: ExcelScript.Workbook) {
        let selectedCell = workbook.getActiveCell();
        let kontrolSheet = workbook.getActiveWorksheet();
        const kontrolSheetUsedRange = kontrolSheet.getUsedRange();
        const kontrolSheetRowCount = kontrolSheetUsedRange.getRowCount();
        const kontrolSheetColumnCount = kontrolSheetUsedRange.getColumnCount();
    
        const kontrolSheetDataRange = kontrolSheet.getRangeByIndexes(1, 0, kontrolSheetRowCount - 1, kontrolSheetColumnCount)
    
        const kontrolShipKeys: (string)[] = kontrolSheetDataRange.getValues();
        // getValues returns a two dimension array
        console.log(kontrolShipKeys)
        
        // the output is same as the quantity of rows in kontrolSheetDataRange
        console.log(`fn getKontrolDistinctShipKeys kontrolShipKeys Length: ${kontrolShipKeys.length}`);
    
        // the quantity of cells in kontrolSheetDataRange
        console.log(`The quantity of cells: ${kontrolShipKeys.length * kontrolShipKeys[0].length}`);
    
    }
    
    

    enter image description here


    
    function main(workbook: ExcelScript.Workbook) {
        let testArray = [[1,2],[3,4],[5,6]]
        
        console.log(testArray)
    
        console.log(`${testArray}`)
    }
    

    The output in console

    enter image description here

    Key Difference: