# \[p5\] I am curious about the Excel conversion source code / array declaration

**URL:** <https://discourse.processing.org/t/p5-i-am-curious-about-the-excel-conversion-source-code-array-declaration/38965>\
**Category:** Libraries\
**Created:** [September 25, 2022, 2:37am UTC](https://discourse.processing.org/t/p5-i-am-curious-about-the-excel-conversion-source-code-array-declaration/38965 "2022-09-25T02:37:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [September 25, 2022, 2:37am UTC](https://discourse.processing.org/t/p5-i-am-curious-about-the-excel-conversion-source-code-array-declaration/38965/1 "2022-09-25T02:37:08Z")

</div>

[p5] I am curious about the Excel conversion source code / array declaration.

**1. How to declare array like ‘java’ in ‘p5’?**  
example) 10x10 array make  
`String[][] randoms = new String[10][10];`  
How do I do ‘p5’ to do the above?

**2. Implemented ‘excel export’ using the source code below.**  
2-1) html

```auto
<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/addons/p5.sound.min.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2014-11-29/FileSaver.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.12.13/xlsx.full.min.js"></script>
    
    
  </head>
  <body>
    <main>
    </main>
    <script src="sketch.js"></script>
  </body>
</html>

```

2-2) sketch.js

```auto
var String_ARRAY = [["","","","",""],["","","","",""],["","","","",""],["","","","",""]];

function setup() {
  createCanvas(400, 400);  
          
  //----------------------------------------------------//     
  for (var i = 0; i < 4; i++) {
   String_ARRAY[0][i]="value-"+str(i+1);
   String_ARRAY[1][i]=random(0,100);
   String_ARRAY[2][i]=random(100,200);
   String_ARRAY[3][i]=random(200,300);
  }
  //----------------------------------------------------//
  
  //----------------------------------------------------//
  var worksheet_data = String_ARRAY;
  var workbook = XLSX.utils.book_new();
  var worksheet = XLSX.utils.aoa_to_sheet(worksheet_data);  
  workbook.SheetNames.push("Test");
  workbook.Sheets["Test"] = worksheet;   
  var filename=str(hour())+str(minute())+str(second());
  exportExcelFile(workbook,filename);
  //----------------------------------------------------//
 
}

function draw() {
  background(220);
}

function exportExcelFile(workbook, file_name) { 
    return XLSX.writeFile(workbook, file_name+".xlsx");
}

```

The above source code works fine.

**Questions) How to declare array like ‘java’ in ‘p5’?**

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 25, 2022, 5:18am UTC](https://discourse.processing.org/t/p5-i-am-curious-about-the-excel-conversion-source-code-array-declaration/38965/2 "2022-09-25T05:18:15Z")

</div>

> [@GWAK](#):
>
> **How to declare array like ‘Java’ in ‘p5’?**  
> Example make 10x10 array:  
> `String[][] randoms = new String[10][10];`

In general when we create vanilla arrays in JS we don’t need to pre-dimension them b/c we’re free to add & remove items from them at anytime.

But if you really want/need such pre-dimensioned 2D array we can use the following `{ length: n }` trick using Array.**from()** + Array::**fill()**:

```auto
const
  LEN = 10, VAL = '',
  strArr = Array.from({ length: LEN }, () => Array(LEN).fill(VAL));

console.table(strArr);

```

> **[Array.from() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#using_arrow_functions_and_array.from)**
>
> The Array.from() static method creates a new, shallow-copied Array instance from an iterable or array-like object.

> **[Array.prototype.fill() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill)**
>
> The fill() method of Array instances changes all elements within a range of indices in an array to a static value. It returns the modified array.

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [September 25, 2022, 7:04am UTC](https://discourse.processing.org/t/p5-i-am-curious-about-the-excel-conversion-source-code-array-declaration/38965/3 "2022-09-25T07:04:33Z")

</div>

@GoToLoop

```auto
const Row = 4;
const Column = 4;
var VAL = '';
var strArr = Array.from({ length: Row }, () => Array(Column).fill(VAL));
console.log(strArr);

```

Thanks to you, it was helpful enough. thank you

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 25, 2022, 7:30am UTC](https://discourse.processing.org/t/p5-i-am-curious-about-the-excel-conversion-source-code-array-declaration/38965/4 "2022-09-25T07:30:08Z")

</div>

You can also make it as a function:

```auto
function createArray2d(rows = 1, cols = rows, val = null) {
  return Array.from({ length: rows }, () => Array(cols).fill(val));
}

const strArr = createArray2d(3, 4, ''); // [3 rows] x [4 columns]
console.table(strArr);

```

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [September 25, 2022, 8:02am UTC](https://discourse.processing.org/t/p5-i-am-curious-about-the-excel-conversion-source-code-array-declaration/38965/5 "2022-09-25T08:02:53Z")

</div>

Dear GoToLoop.

Very good. Nice function!

thank you so much.
