Excel Script: getRange not returning column values

William C 25 Reputation points
2025-07-30T14:38:03.2866667+00:00

Hi,

I was working on a script that would get the values in a Column based on the column letter . But anytime I run the code below it returns back null. Is this expected or a bug ?

I know there are getColumn which can get it back the indexed number but was trying to do it by letter.

function main(workbook: ExcelScript.Workbook) {

// Get the active cell and worksheet.
// Get the active worksheet
let sheet: ExcelScript.Worksheet \= workbook.getActiveWorksheet();

// Get the range for column A
let range \= sheet.getRange("A:A");


// Get the values for the range
let values \= range.getValues();

console.log(values);
}


Screenshot 2025-07-30 at 10.26.34 AM

P.S. Ignore the \= forum wouldnt post the code without it

Microsoft 365 and Office | Excel | Other | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Viorel 125.8K Reputation points
    2025-07-30T14:55:40.89+00:00

    Try to limit the area:

    let range = sheet.getRange( "A1:A20" );
    
    // or
    
    let range = sheet.getRange( "A:A" ).getUsedRange( true );
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Chiara Carbone 360 Reputation points
    2025-07-30T14:55:49.6266667+00:00

    Hi

    Using "A:A" references the entire column (1,048,576 rows), and getRange("A:A").getValues() in Excel Scripts sometimes behaves unreliably or inefficiently with such a large range.

    Please use this:

    function main(workbook: ExcelScript.Workbook) {
        // Get the active cell and worksheet.
        let selectedSheet = workbook.getActiveWorksheet();
        let usedRange = selectedSheet.getUsedRange();
        
        let rowCount = usedRange.getRowCount();
    
        let range = selectedSheet.getRange(`A1:A${rowCount}`);
    
        let values = range.getValues();
        console.log(values);
    }
    
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.

  2. Marie B 610 Reputation points
    2025-07-30T15:36:22.21+00:00

    Hi there,

    Thank you for reaching out!

    It seems like the issue may be due to an empty column or a small syntax error. Here's a quick fix:

    Try referencing a smaller range like "A1:A10" to ensure data exists.

    Make sure you're using = instead of = for assignment.

    If you want to get values by column letter, here’s a solution:

    typescript

    Copy

    Edit

    let columnLetter = "A";

    let columnIndex = columnLetter.charCodeAt(0) - 65;

    let range = sheet.getRangeByIndexes(0, columnIndex, sheet.getRowCount(), 1);

    Let me know if this resolves the issue or if you need further assistance.

    Best,Marie

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.