Search code examples
exceltypescriptoffice-scripts

How to paste 1-D array into range in office script?


I'm very new to office scripts. I have 1-Dimensional array that I want to paste in a range, but I'm getting the error

Line 7: Range setValues: The argument is invalid or missing or has an incorrect format.

I've found that the expected array should be of 2 dimensions like in this thread, but I don't know how to fix it in my case. Thanks for any help.

This is my current script.

function main(workbook: ExcelScript.Workbook) {

    const arr: number[] = [201, 106, 95];

    let destRange = workbook.getActiveWorksheet().getRange("A1:A3");

    destRange.setValues(arr);

}

Solution

  • You need to convert it to a 2-D array:

    const twoDArray = arr.map(value => [value]);