To create a 2D array in OfficeScripts, you can use nested arrays. Here’s an example of how to create a 2D array and assign values to it:
function create2DArray() { // Define the dimensions of the 2D array var numRows = 3; var numCols = 4; // Create an empty 2D array var array2D = new Array(numRows); // Loop through the rows and create arrays for each row for (var i = 0; i < numRows; i++) { array2D[i] = new Array(numCols); } // Assign values to the 2D array array2D[0][0] = "A"; array2D[0][1] = "B"; array2D[0][2] = "C"; array2D[0][3] = "D"; array2D[1][0] = 1; array2D[1][1] = 2; array2D[1][2] = 3; array2D[1][3] = 4; array2D[2][0] = true; array2D[2][1] = false; array2D[2][2] = "X"; array2D[2][3] = "Y"; // Logging the 2D array console.log(array2D); }
In this example, the create2DArray
function creates a 2D array with 3 rows and 4 columns. It starts by creating an empty array array2D
with the specified number of rows.
Next, it loops through each row and creates an array for each row using new Array(numCols)
. This initializes each row of the 2D array as a separate array.
After creating the 2D array, the function assigns values to the individual elements of the array using the row and column indices. In this example, different types of values are assigned to demonstrate the flexibility of a 2D array.
Finally, the 2D array is logged to the console.
You can call the create2DArray
function to see the resulting 2D array in the console output.
Please note that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.