Created
April 18, 2021 17:47
-
-
Save schamane/22fdb25ab4ca61f58a141fb87452ca71 to your computer and use it in GitHub Desktop.
Create excel with Koha action logs excel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { get, map, replace } = require('lodash'); | |
const { readFile, writeFile, utils } = require('xlsx'); | |
const { format } = require('date-fns'); | |
const workBook = readFile('./nadiajka_koha_excel.xlsx'); | |
const sheetName = workBook.SheetNames[1]; | |
const sheetData = utils.sheet_to_json(workBook.Sheets[sheetName], { | |
blankrows: true, | |
defval: undefined | |
}); | |
const mapDate = (date) => new Date(Math.round((date - 25569) * 86400 * 1000)); | |
const cleanContent = (content) => replace(content, /(?:\\[rn]|[\r\n]|[\s]+)+/g, ''); | |
const findBiblio = (data) => get(data.match(/'biblionumber'\s?=>\s?'([^\\']+)'/im), 1); | |
const findBarcode = (data) => get(data.match(/'barcode'\s?=>\s?'([^\\']+)'/im), 1); | |
const findSignature = (data) => get(data.match(/'itemcallnumber'\s?=>\s?'?"?([^\\'\\",]+)'?"?/im), 1); | |
const embolden = (str) => str.replace(/\\x{(\d+)}/g, (a, b) => String.fromCharCode(parseInt(b, 16))); | |
const newSheetData = map(sheetData, ({ date, content }) => { | |
const data = embolden(cleanContent(content)); | |
const entryDate = mapDate(date); | |
const sign = findSignature(data); | |
return { | |
date: format(entryDate, 'dd.MM.yyyy'), | |
time: format(entryDate, 'hh:mm'), | |
biblio: findBiblio(data), | |
barcode: findBarcode(data), | |
signature: sign === 'undef' ? undefined : sign, | |
url: `` | |
// constent: data | |
}; | |
}); | |
// console.log(newSheetData); | |
const writeSheetData = utils.json_to_sheet(newSheetData); | |
const newBook = utils.book_new(); | |
utils.book_append_sheet(newBook, writeSheetData, 'Changes on KOHA'); | |
writeFile(newBook, './output.xlsx'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment