<commit_before>import {_} from 'lodash';
import XLSX from 'xlsx-style';
import Runner from '../../core/actions/runner.js';
import GetContributionVersion from './get_contribution_version';
//import json2csv from 'json2csv';

import {default as magicVersions} from '../configs/magic_versions';
import {default as magicDataModels} from '../configs/data_models/data_models';

export default class extends Runner {

  constructor({LocalState}) {
    super('EXPORT_CONTRIBUTION', {LocalState});
    this.VersionGetter = new GetContributionVersion({LocalState});
    this.version;
    this.model;
  }

  /*To extended text is a boolean indicating whether or not the text should be extended*/
  toText(jsonToExport, toExtendedText) {
    // Text should be a valid MagIC tab delimited text file with the tables and columns in the order defined in the data model.

    // Retrieve the data model version used in the jsonToExport
    this.version = this.VersionGetter.getVersion(jsonToExport)
    if (!this.version) return jsonToExport;

    // Retrieve the data model
    this.model = magicDataModels[this.version];
    this.orderedModel = this.createOrderedModel(jsonToExport);

    this.testValidityOfTablesAndColumns(jsonToExport);


    // TODO: use the model to build up text string here
    let text = this.createTSVfile(jsonToExport, toExtendedText);

    return text;
  }

  //Per requirements, this will only work for model 3.0
  toExcel(jsonToExport) {
    this.version = this.VersionGetter.getVersion(jsonToExport);//Todo: refactor the init of class variables into a separate function
    this.model = magicDataModels[this.version];
    this.orderedModel = this.createOrderedModel(jsonToExport);

    // Create an empty workbook
    let workbook = { SheetNames: [], Sheets: {} };

    let style = {fill:{patternType: 'solid'},  font:{}, numFmt:{}, alignment:{}, border:{}};

    for(let modelTable in this.model.tables)
    {
      workbook.SheetNames.push(modelTable);//Create a sheet for each table in the 3.0 model in the workbook.
      
      let tableHeaders = this.createExtendedHeadersData(modelTable, jsonToExport/*, orderedColumnArray*/);

      /*These the first four rows of the excel file*/
      let groupHeader = tableHeaders.groupHeader;
      groupHeader.unshift('Group:');//place 'Group' at the beginning of the array
      //console.log(`groupHD${groupHeader}`);
      let nameHeader = tableHeaders.labelHeader;
      nameHeader.unshift('Name:');
      let typeHeader = tableHeaders.columnTypeOrUnitHeader;
      typeHeader.unshift('Type:');
      let columnHeader = tableHeaders.columnNameHeader;
      columnHeader.unshift('Column:');

      //Now gather the data from the json object
      let tableData = jsonToExport[modelTable];
     /* let tableDataWithExtraLeadingColumn = [];

      for(let dataRowIdx in tableData)
        tableDataWithExtraLeadingColumn.push(tableData[dataRowIdx]);*/

      /*Create array of sheet headers/data. This will be an array of arrays.
      The first dimension of the array is the row in the excel sheet
      The second dimension is the row data*/

      let completeSpreadSheetData =
          [
            groupHeader ,
            nameHeader,
            typeHeader,
            columnHeader
            /*      [1,2,3],
             [true, false, null, "sheetjs"],
             ["foo","bar",new Date("2014-02-19T14:30Z"), "0.3"],
             ["baz", null, "qux"]*/
          ]

      //Add data to the spreadsheet row by row, adding a blank column at the beginning
      for(let dataRowIdx in tableData)
      {
        let newRowArray = _.values(tableData[dataRowIdx]);
        newRowArray.unshift('');//Add a blank column at the beginning of each row
        completeSpreadSheetData.push(newRowArray);
      }

      workbook.Sheets[modelTable] = this._toSheet(completeSpreadSheetData);

      let currentSheet = workbook.Sheets[modelTable];
      currentSheet['A1'].s =  {
        alignment: {horizontal: 'center', vertical: 'center'},
        border: {
          left: {style: 'thick', color: {auto: 1}},
          right: {style: 'thick', color: {auto: 1}},
          top: {style: 'thick', color: {auto: 1}},
          bottom: {style: 'thick', color: {auto: 1}}
        }};
      //style; //{fill.patternType: 'solid'};
      /*currentSheet['A2'].v = 'Name:';
      currentSheet['A3'].v = 'Type:';
      currentSheet['A4'].s = 'Column:';*/

    }

    return workbook;
  }


  _datenum(v, date1904) {
    if(date1904) v+=1462;
    var epoch = Date.parse(v);
    return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
  }

  _toSheet(data, opts) {
    var workSheet = {};
    var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
    for(var R = 0; R != data.length; ++R) {<commit_msg>Dynamic formatting for group<commit_after>import {_} from 'lodash';
import XLSX from 'xlsx-style';
import Runner from '../../core/actions/runner.js';
import GetContributionVersion from './get_contribution_version';
//import json2csv from 'json2csv';

import {default as magicVersions} from '../configs/magic_versions';
import {default as magicDataModels} from '../configs/data_models/data_models';

export default class extends Runner {

  constructor({LocalState}) {
    super('EXPORT_CONTRIBUTION', {LocalState});
    this.VersionGetter = new GetContributionVersion({LocalState});
    this.version;
    this.model;
  }

  /*To extended text is a boolean indicating whether or not the text should be extended*/
  toText(jsonToExport, toExtendedText) {
    // Text should be a valid MagIC tab delimited text file with the tables and columns in the order defined in the data model.

    // Retrieve the data model version used in the jsonToExport
    this.version = this.VersionGetter.getVersion(jsonToExport)
    if (!this.version) return jsonToExport;

    // Retrieve the data model
    this.model = magicDataModels[this.version];
    this.orderedModel = this.createOrderedModel(jsonToExport);

    this.testValidityOfTablesAndColumns(jsonToExport);


    // TODO: use the model to build up text string here
    let text = this.createTSVfile(jsonToExport, toExtendedText);

    return text;
  }

  //Per requirements, this will only work for model 3.0
  toExcel(jsonToExport) {
    this.version = this.VersionGetter.getVersion(jsonToExport);//Todo: refactor the init of class variables into a separate function
    this.model = magicDataModels[this.version];
    this.orderedModel = this.createOrderedModel(jsonToExport);

    // Create an empty workbook
    let workbook = { SheetNames: [], Sheets: {} };

    let style = {fill:{patternType: 'solid'},  font:{}, numFmt:{}, alignment:{}, border:{}};

    for(let modelTable in this.model.tables)
    {
      workbook.SheetNames.push(modelTable);//Create a sheet for each table in the 3.0 model in the workbook.
      
      let tableHeaders = this.createExtendedHeadersData(modelTable, jsonToExport/*, orderedColumnArray*/);

      /*These the first four rows of the excel file*/
      let groupHeader = tableHeaders.groupHeader;
      groupHeader.unshift('Group:');//place 'Group' at the beginning of the array
      //console.log(`groupHD${groupHeader}`);
      let nameHeader = tableHeaders.labelHeader;
      nameHeader.unshift('Name:');
      let typeHeader = tableHeaders.columnTypeOrUnitHeader;
      typeHeader.unshift('Type:');
      let columnHeader = tableHeaders.columnNameHeader;
      columnHeader.unshift('Column:');

      //Now gather the data from the json object
      let tableData = jsonToExport[modelTable];

      /*Create array of sheet headers/data. This will be an array of arrays.
      The first dimension of the array is the row in the excel sheet
      The second dimension is the row data*/
      let completeSpreadSheetData =
          [
            groupHeader ,
            nameHeader,
            typeHeader,
            columnHeader
            /*      [1,2,3],
             [true, false, null, "sheetjs"],
             ["foo","bar",new Date("2014-02-19T14:30Z"), "0.3"],
             ["baz", null, "qux"]*/
          ]

      //Add data to the spreadsheet row by row, adding a blank column at the beginning
      for(let dataRowIdx in tableData)
      {
        let newRowArray = _.values(tableData[dataRowIdx]);
        newRowArray.unshift('');//Add a blank column at the beginning of each row
        completeSpreadSheetData.push(newRowArray);
      }

      workbook.Sheets[modelTable] = this._toSheet(completeSpreadSheetData);

      //From the xlsx-style docs:
      /*Cell range objects are stored as {s:S, e:E} where S is the first cell and E is the last cell in the range.
      The ranges are inclusive. For example, the range A3:B7 is represented by the object {s:{c:0, r:2}, e:{c:1, r:6}}*/
      let currentSheet = workbook.Sheets[modelTable];

      //for each group found for this table
      /*for(let groupIdx in groupHeader)
      {
        let currentCellToFormat = '';
        //console.log(`group idxSr : ${groupIdx.toString()}`);
        let excelIdx = Number(groupIdx);
        excelIdx++;
        //console.log(`prior ${(excelIdx).toString()}`);
        currentCellToFormat = 'A'+ Number(excelIdx).toString();
        console.log(`TRy ${currentCellToFormat}`);
        currentSheet[currentCellToFormat/!*'A1'*!/].s =  {
          alignment: {horizontal: 'center', vertical: 'center'},
          border: {
            left: {style: 'thick', color: {auto: 1}},
            right: {style: 'thick', color: {auto: 1}},
            top: {style: 'thick', color: {auto: 1}},
            bottom: {style: 'thick', color: {auto: 1}}
          },
          font:{bold:'true'},
          fill:{fgColor:{rgb:'D3D3D3'}}
        };
      }*/

      for(let groupIdx in groupHeader)
      {
       // let currentCellToFormat = '';
        //console.log(`group idxSr : ${groupIdx.toString()}`);
       // let excelIdx = Number(groupIdx);
        //excelIdx++;
        //console.log(`prior ${(excelIdx).toString()}`);
        //currentCellToFormat = 'A'+ Number(excelIdx).toString();
        //console.log(`TRy ${currentCellToFormat}`);
        let cellAddress = XLSX.utils.encode_cell({c: groupIdx,r: 0});
        currentSheet[cellAddress].s =  {
          alignment: {horizontal: 'center', vertical: 'center'},
          border: {
            left: {style: 'thick', color: {auto: 1}},
            right: {style: 'thick', color: {auto: 1}},
            top: {style: 'thick', color: {auto: 1}},
            bottom: {style: 'thick', color: {auto: 1}}
          },
          font:{bold:'true'},
          fill:{fgColor:{rgb:'D3D3D3'}}
        };
      }


      currentSheet['A2'].s =  {
        alignment: {horizontal: 'center', vertical: 'center'},
        border: {
          left: {style: 'thick', color: {auto: 1}},
          right: {style: 'thick', color: {auto: 1}},
          top: {style: 'thick', color: {auto: 1}},
          bottom: {style: 'thick', color: {auto: 1}}
        }};
      currentSheet['A3'].s =  {
        alignment: {horizontal: 'center', vertical: 'center'},
        border: {
          left: {style: 'thick', color: {auto: 1}},
          right: {style: 'thick', color: {auto: 1}},
          top: {style: 'thick', color: {auto: 1}},
          bottom: {style: 'thick', color: {auto: 1}}
        }};
      currentSheet['A4'].s =  {
        alignment: {horizontal: 'center', vertical: 'center'},
        border: {
          left: {style: 'thick', color: {auto: 1}},
          right: {style: 'thick', color: {auto: 1}},
          top: {style: 'thick', color: {auto: 1}},
          bottom: {style: 'thick', color: {auto: 1}}
        }};


      //style; //{fill.patternType: 'solid'};
      /*currentSheet['A2'].v = 'Name:';
      currentSheet['A3'].v = 'Type:';
      currentSheet['A4'].s = 'Column:';*/

    }

    return workbook;
  }


  _datenum(v, date1904) {
    if(date1904) v+=1462;
    var epoch = Date.parse(v);
    return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
  }

  _toSheet(data, opts) {
    var workSheet = {};
    var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
    for(var R = 0; R != data.length; ++R) {
{'fraction_non_alphanumeric': 0.13749691434213773, 'fraction_numerical': 0.015798568254751913, 'mean_word_length': 2.785113671753348, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7385905', 'n_tokens_mistral': 3796, 'n_tokens_neox': 3581, 'n_words': 1027}