Categories
JavaScript Answers

How to read Excel file using Node.js and JavaScript?

Sometimes, we want to read Excel file using Node.js and JavaScript

In this article, we’ll look at how to read Excel file using Node.js and JavaScript.

How to read Excel file using Node.js and JavaScript?

To read Excel file using Node.js and JavaScript, we use the node-xlsx package.

To install it, we run

npm install node-xlsx --save

Then we use it by writing

const xlsx = require("node-xlsx");

const obj = xlsx.parse(__dirname + "/myFile.xlsx");

to parse a file at the path with xlsx.parse.

We can parse a buffer with

const obj = xlsx.parse(fs.readFileSync(__dirname + "/myFile.xlsx"));

We read the file with readFileSync and then parse the returned buffer.

Conclusion

To read Excel file using Node.js and JavaScript, we use the node-xlsx package.

Categories
JavaScript Answers

How to make synchronous requests in Node.js and JavaScript?

Sometimes, we want to make synchronous requests in Node.js and JavaScript.

In this article, we’ll look at how to make synchronous requests in Node.js and JavaScript.

How to make synchronous requests in Node.js and JavaScript?

To make synchronous requests in Node.js and JavaScript, we can use the retus library.

To install it, we run

npm i retus

Then we use it by writing

const retus = require("retus");

const { body } = retus("https://example.com");

to make a get request to https://example.com synchronously.

body has the response body.

Conclusion

To make synchronous requests in Node.js and JavaScript, we can use the retus library.

Categories
JavaScript Answers

How to prettify JSON data in text area input with JavaScript?

Sometimes, we want to prettify JSON data in text area input with JavaScript.

In this article, we’ll look at how to prettify JSON data in text area input with JavaScript.

How to prettify JSON data in text area input with JavaScript?

To prettify JSON data in text area input with JavaScript, we use the JSON.stringify method.

For instance, we write

<textarea id="myTextArea" cols="50" rows="10"></textarea>
<button>Pretty Print</button>

to add a text area and a button.

Then we write

const button = document.querySelector("button");

button.onclick = () => {
  const ugly = document.getElementById("myTextArea").value;
  const obj = JSON.parse(ugly);
  const pretty = JSON.stringify(obj, undefined, 4);
  document.getElementById("myTextArea").value = pretty;
};

to select the button with querySelector.

And then we set button.onclick to a function that formats the JSON by calling JSON.parse to parse the ugly JSON string.

Next we call JSON.stringify with obj and 4 to format the string with 4 spaces for indentation.

And then we assign the formatted pretty JSON string back to the text area.

Conclusion

To prettify JSON data in text area input with JavaScript, we use the JSON.stringify method.

Categories
CSS

How to change display of toggle button with Twitter Bootstrap collapse?

Sometimes, we want to change display of toggle button with Twitter Bootstrap collapse.

In this article, we’ll look at how to change display of toggle button with Twitter Bootstrap collapse.

How to change display of toggle button with Twitter Bootstrap collapse?

To change display of toggle button with Twitter Bootstrap collapse, we can add our own CSS styles.

For instance, we write

<a
  class="btn btn-primary collapsed"
  data-toggle="collapse"
  href="#collapseExample"
>
  <!--You can put any valid html inside these!-->
  <span class="if-collapsed">Open</span>
  <span class="if-not-collapsed">Close</span>
</a>
<div class="collapse" id="collapseExample">
  <div class="well">...</div>
</div>

to add the collapse elements.

Then we write

[data-toggle="collapse"].collapsed .if-not-collapsed {
  display: none;
}
[data-toggle="collapse"]:not(.collapsed) .if-collapsed {
  display: none;
}

to hide the button when the when the collapsed is open with

[data-toggle="collapse"].collapsed .if-not-collapsed {
  display: none;
}

And we hide the collapse container when it’s closed with

[data-toggle="collapse"]:not(.collapsed) .if-collapsed {
  display: none;
}

Conclusion

To change display of toggle button with Twitter Bootstrap collapse, we can add our own CSS styles.

Categories
JavaScript Answers

How to round up or down a moment.js to the nearest minute with JavaScript?

Sometimes, we want to round up or down a moment.js to the nearest minute with JavaScript.

In this article, we’ll look at how to round up or down a moment.js to the nearest minute with JavaScript.

How to round up or down a moment.js to the nearest minute with JavaScript?

To round up or down a moment.js to the nearest minute with JavaScript, we use the startOf method.

For instance, we write

const roundDown = moment("2022-02-17 12:59:59").startOf("hour");
roundDown.format("HH:mm:SS");

to round up to the nearest hour by calling startOf.

Then we write

const roundUp = (momentObj, roundBy) => {
  return momentObj.add(1, roundBy).startOf(roundBy);
};

const a = moment("2022-02-17 12:00:00");
const roundedUp = roundUp(a, "minute").format("HH:mm:SS");

to define the roundUp function.

In it, we call add with 1 and roundBy to add 1 roundBy unit to the moment object.

Then we call startOf with roundBy to round to the nearest roundBy unit.

Next we call roundUp and format to round up and format the time returned.

Conclusion

To round up or down a moment.js to the nearest minute with JavaScript, we use the roundTo method.