Categories
JavaScript Answers

How to set cell width when export .xlsx files with Node js-xlsx?

To set cell width when export .xlsx files with Node js-xlsx, we set the wch property.

For instance, we write

const fitToColumn = (arrayOfArray) => {
  return arrayOfArray[0].map((a, i) => ({
    wch: Math.max(
      ...arrayOfArray.map((a2) => (a2[i] ? a2[i].toString().length : 0))
    ),
  }));
};

const worksheet = XLSX.utils.aoa_to_sheet(arrayOfArray);
worksheet["!cols"] = fitToColumn(arrayOfArray);

to define the fitToColumn function that returns an array with objects with the wch property that has the width value.

We set the width to the max number of characters in the cell of each column.

We get the worksheet from the aoa_to_sheet called with our arrayOfArray nested array of data.

Categories
JavaScript Answers

How to create folder or key on s3 using AWS SDK for Node.js?

To create folder or key on s3 using AWS SDK for Node.js, we call the upload method.

For instance, we write

const AWS = require("aws-sdk");
AWS.config.region = "us-east-1";
const s3Client = new AWS.S3();

const params = {
  Bucket: "your_bucket_goes_here",
  Key: "folderInBucket/",
  ACL: "public-read",
  Body: "body does not matter",
};

s3Client.upload(params, (err, data) => {
  if (err) {
    console.log("Error creating the folder: ", err);
  } else {
    console.log("Successfully created a folder on S3");
  }
});

to call upload with the params object that has the Bucket and Key of the file.

And Body has the body of the file we’re uploading.

We get the uploaded data from data and errors from err in the callback.

Categories
JavaScript Answers

How to kill the pm2 –no-daemon process with Node?

To kill the pm2 –no-daemon process with Node, we run pm2 killorkill`.

For instance, we run

pm2 kill

to kill all pm2 processes.

Or we run

ps aux | grep PM2

to get the process ID of the pm2 process.

And we run

kill -9 [pid]

to send the KILL signal to the process with ID [pid] that we got from ps aux | grep PM2.

Categories
JavaScript Answers

How to add sub collection to a document in Firestore with JavaScript?

To add sub collection to a document in Firestore with JavaScript, we call add or set.

For instance, we write

db.collection("users").doc(username).collection("booksList").doc(myBookId).set({
  password,
  name,
  rollno,
});

to call set to update the entry in the 'booksList' collection with ID myBookId with the object we used as the argument for set.

And we write

db.collection("users").doc(username).collection("booksList").add({
  password,
  name,
  rollno,
});

to call add to add the entry we call add with to the 'booksList' collection.

Categories
JavaScript Answers

How to execute an exe file using Node?

To execute an exe file using Node, we use the execfile method.

For instance, we write

const exec = require("child_process").execFile;

exec("hello.exe", (err, data) => {
  console.log(err);
  console.log(data.toString());
});

to call exec run to hello.exe.

We get the output from data and errors from err in the callback.