Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const XLSX = require('xlsx-style')
  2.  
  3. let workbook = XLSX.readFile('data.xlsx', {
  4.     cellStyles: true
  5. })
  6.  
  7. let families = workbook.SheetNames
  8.  
  9. let fields = {
  10.     A: 'Date',
  11.     B: 'Lieu',
  12.     C: 'Photo',
  13.     D: 'Ordre',
  14.     E: 'Genre',
  15.     F: 'Espèce',
  16.     G: 'Rav/Aux/S-E',
  17.     H: 'Stade',
  18.     I: 'Indice d\'abondance',
  19.     J: 'Nourriture'
  20. }
  21.  
  22. let impactsColor = {
  23.     'FFFFFF00': 'Ravageur toléré (Léger)',
  24.     'FFFD8207': 'Ravageur',
  25.     'FFFF0000': 'Ravageur lourd',
  26.     'FF00B050': 'Auxiliaire faible',
  27.     'FF7030A0': 'Auxiliaire',
  28.     'FF00B0F0': 'Auxiliaire important'
  29. }
  30.  
  31. let entities = []
  32. families.forEach(familyName => {
  33.     let document = workbook.Sheets[familyName]
  34.     let rows = Object.keys(document).reduce((acc, curr) => {
  35.         let num = parseInt(curr.slice(1))
  36.         if (!Number.isInteger(num)) {
  37.             return acc
  38.         }
  39.         return num > acc ? num : acc
  40.     }, 0)
  41.  
  42.     let firstIndex = 5
  43.     for (row = firstIndex; row < rows; row++) {
  44.         let obj = {}
  45.         for (const fieldIndex in fields) {
  46.             let fieldName = fields[fieldIndex]
  47.             let value = document[`${fieldIndex}${row}`].v
  48.             if (fieldName == 'Date' && value != '') {
  49.                 let date = XLSX.SSF.parse_date_code(value)
  50.                 obj[fieldName] = date
  51.             } else if (fieldName == 'Rav/Aux/S-E') {
  52.                 let style = document[`${fieldIndex}${row}`].s
  53.                 let fillStyle = style.fill
  54.                 let bgColor = fillStyle ? fillStyle.fgColor : undefined
  55.                 if (bgColor) {
  56.                     obj[fieldName] = impactsColor[bgColor.rgb]
  57.                 } else {
  58.                     obj[fieldName] = 'Sans effet'
  59.                 }
  60.             } else {
  61.                 obj[fieldName] = value
  62.             }
  63.             obj.familyName = familyName
  64.         }
  65.         entities.push(obj)
  66.     }
  67. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement