0

I am currently using json2xls to export my queried data in json format into a xlsx file like so:

var xls = tool.json2xls(data, {order:["NAME", "OWNER_EMAIL", "GROUP", "BRAND", "DOC_CENTER_URL", "LIBRARYURL", "TOTAL_VIEWS"], 
                              fieldMap: {NAME: "Name", OWNER_EMAIL: "Owner Email", GROUP: "Group", BRAND: "Brand", DOC_CENTER_URL: "Doc Center URL",
                              LIBRARYURL: "Library URL", TOTAL_VIEWS: "Total Views"}}); 

I am already using the option field fieldMap to edit column names which works well but I want to be able to edit the widths of each column so that when the xlsx is opened it will be formatted well. In the json2xls docs it says that the avaiable options are:

  • style: a styles xml file, see https://github.com/functionscope/Node-Excel-Export
  • fields: either an array or map containing field configuration:
    • array: a list of names of fields to be exported, in that order
    • object: a map of names of fields to be exported and the types of those fields. Supported types are 'number','string','bool'
Riley Eaton
  • 137
  • 12
  • The ability to bold text would be great, I just can't find any docs on excel text formatting that would indicate how to do so. – Riley Eaton May 31 '21 at 16:05

1 Answers1

0

The docs in the link you've provided already have the answer:

var conf ={};
conf.stylesXmlFile = "styles.xml";
conf.name = "mysheet";
//here you define columns data and properties:
conf.cols = [
 {caption:'string',
  type:'string',
  beforeCellWrite:function(row, cellData){
    return cellData.toUpperCase();},
  //and specifically for column width:
  width:28.7109375}... 

Note that as per the docs, column width unit is character, which is Excel's way for column width. (e.g. 20 is the width of 20 characters, based on the font size and style)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ISAE
  • 519
  • 4
  • 12
  • I see that but I don't understand how that fits in to the json2xls method. Do I pass conf into json2xls as an option? – Riley Eaton May 31 '21 at 17:37
  • Actually after digging into the source code of json2xls I see that he is unnecessarily overriding the node-excel-support conf object. Do you have a specific reason to use this package? because it seems using the original will be simpler. If you go with json2xls your only option will be to provide a [style xml](https://learn.microsoft.com/en-us/office/open-xml/structure-of-a-spreadsheetml-document). – ISAE May 31 '21 at 18:06