How can I reorganize the order of columns in js-xlsx?

Utilizing the js-xlsx library, I am in the process of generating an excel file. However, I have encountered an issue where the order of the columns in my sheet is not as desired after using .json_to_sheet. For example, the current order looks like this:

100 | 200 | 300| 400| a | b | c

My expected order should be:

a | b | c | 100 | 200 | 300| 400

The headers a | b | c remain constant, while the numeric column headers may vary.

Is there a way to rearrange the columns in the desired order?

NOTE: If numerical values are used as headers, xlsx organizes the columns with numeric headers first before any other type of header. If non-numeric headers are used, the columns can be ordered directly in the JSON data saved. One workaround is to preface the numbers with a space ' ', which resolves the ordering issue. However, I would prefer not to add a space before the numbers.

Answer №1

// The default generation is 'a,b,c\n1,2,3\n'​
​XLSX​.​utils​.​sheet_to_csv​(​XLSX​.​utils​.​json_to_sheet​(​[​{​a​:​1​,​b​:​2​,​c​:​3​}​]​)​)​

// By passing header:['c','a','b'], you can reorder to: 'c,a,b\n3,1,2\n'​
​XLSX​.​utils​.​sheet_to_csv​(​XLSX​.​utils​.​json_to_sheet​(​[​{​a​:​1​,​b​:​2​,​c​:​3​}​]​,​ ​{​header​:​[​'c'​,​'a'​,​'b'​]​}​)​)​

// If you pass header:['c','b','a'], it will reorder to: 'c,b,a\n3,2,1\n'​
​XLSX​.​utils​.​sheet_to_csv​(​XLSX​.​utils​.​json_to_sheet​(​[​{​a​:​1​,​b​:​2​,​c​:​3​}​]​, ​{​header​:​[​'c'​,​'b'​,​'a'​]​}​)​)

This snippet was discovered on GitHub issue #738. Feel free to experiment with the 'header' parameter for different results.

Answer №2

Let's define a header array with values such as 'a', 'b', 'c', 100, and more. We then create a worksheet named 'ws' using XLSX utilities to convert JSON data into a sheet. It is important to note that the column headers in the header array should match the keys in the data.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Highcharts: Show tooltip above all elements

Is there a way to configure tooltip display above all elements? I want to define specific coordinates so that the tooltip remains open even if it is covering the chart, and only closes when interacting with the top block. Screenshot 1 Screenshot 2 For e ...

Having trouble retrieving accurate data in jQuery ajax post success when running on localhost with XAMPP

I am currently working on an ajax post using jQuery to send data to a php file and facing an issue with getting the correct data in the success function. Below are my code snippets: JAVASCRIPT $.ajax({ url: "ajax/chart-kpi-trend.php", data: { ...

Could it be possible that my consecutive POST and GET axios requests are gradually slowing down?

After chaining the POST and GET calls in my code, I noticed a slight slowdown and was curious if this is normal or if there's a more efficient approach. The delay in displaying the google map marker made me think that pushing the newly created marker ...

How can I display an iframe element only if it exists within an object in Angular?

I'm currently exploring options to specifically display the iframe element within a block of HTML content being sent to my Angular application. While I can use a *ngIf directive in my template to check for the presence of the tag, I am unsure of how ...

Identify picture quality?

I've been looking into this but haven't found a solution yet. Is there a way to determine an image's resolution using an upload file field? Currently, I can only find the pixel dimensions of an image...but I need to know its size in "inches ...

Tips on skipping the need to repeatedly use `@ApiProperty()` for every dto in NestJs-swagger

I'm currently exploring ways to streamline the process of specifying @ApiProperty() for each DTO. I've heard about a method involving the creation of a nest-cli.json file, where if you define Promise<DTO> in your controller within nest-swa ...

What steps can be taken to display database results solely for the user currently logged in and created by them?

Currently, I'm in the midst of a project that involves extracting an HTML list from JSON data using JavaScript. The list is being displayed on the logged-in user's profile, showcasing job listings from the JSON data. While I've successfully ...

event videojs termination malfunction

I am trying to set up an event that triggers when my video finishes playing. I have placed the script right below my video tag. <script type="text/javascript> var myPlayer = videojs("session_video"); videojs("session_video").r ...

What is the best way to implement 2 conditions in the componentDidUpdate method?

Is there a way to include two conditions in an "if" statement within the componentDidUpdate method? Below is the code I am working with: componentDidUpdate(prevProps, prevState) { if ( prevState.address_id !== this.state.address_id && ...

How to smoothly glide to the bottom of a chat box by scrolling synchronously

I am currently in the process of developing a chat application. Each time a user sends a new message, it is added to a list of messages displayed in an unordered list (ul). I have successfully made the ul scrollable, but unfortunately, when a new message i ...

angular failure to assign a variable to $scope

When communicating with the Node server, I am able to receive a response message in the controller. However, there seems to be an issue with assigning it properly. .controller('someCtrl', function(exportExcel){ $scope.clickEvent = function() ...

JavaScript file encountering a Typescript issue with a property defined in a subclass

Currently, I am utilizing Typescript to validate my Javascript files. One issue I have encountered is that when I inherit from a class, Typescript does not recognize the types of the properties in the parent class. I am unsure if I am overlooking something ...

Guiding a WordPress Form Submission to a Different Page

I'm currently using WordPress to create a form with the following code: [contact-form][contact-field label='What message would you like to send?' type='textarea' required='1'/]Word Limit: 50 words[contact-field label=&ap ...

What is the process for uploading a JSON file to Google Drive using Google Colab?

Currently, I am training a model in Google Colab and saving it in json format. My goal is to upload this trained model directly to my drive within the colab environment. This is the current process I am following: model_json = model.to_json() with open( ...

Experiencing an issue with Angular 14 when trying to install apollo-angular as

For the past two days, I've been attempting to set up apollo on an Angular application. Unfortunately, I keep encountering the following error: The package <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea8b9a85868685c78b8 ...

Implementing Placeholder Text Across Multiple Lines with Material UI

Currently, for the React App I am developing, I am utilizing Material UI. In order to achieve a multi-line placeholder for a textarea using the TextField component, here is what I have so far: <TextField id="details" ful ...

Autofill Text Input with React Material-UI

For my current project, I am utilizing Material UI and React. Each TextField in the project has a button next to it, and when the button is clicked, I want it to retrieve the value of its corresponding TextField and set that value as the input for another ...

A guide on searching for information in a JSON document using PHP

In PHP, I am trying to retrieve an ID from the URL and then search for corresponding data in a JSON file, which will be displayed on the page. The URL structure will be and the code I will use is; $id = $_GET['id']; to assign the ID to a vari ...

Adding items from the JSON data to a nested array at index i

In my project, I am working with two files: index.js and list.json. My goal is to extract an element from list.json and insert it into a nested array following the structure [hour][visits per hour]. The hour refers to the index of the hour; for example, ...

How to play audio with a source path that includes the special character "%" in JavaScript

Having an issue with playing an audio file that has '%' in its name. I've tried using the tag <audio src="test%320.mp3" controls></audio>, but it seems like the file is not being found when I try to play it. Can anyone ...