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

[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

<!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

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’?

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():

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

console.table(strArr);
2 Likes

@GoToLoop

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

You can also make it as a function:

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);
1 Like

Dear GoToLoop.

Very good. Nice function!

thank you so much.

1 Like