extract date fields macro

This commit is contained in:
Jan Prochazka 2020-11-01 12:46:34 +01:00
parent ab2fb3bf97
commit 8ff44e41b1
3 changed files with 126 additions and 10 deletions

View File

@ -44,6 +44,7 @@ const MacroDetailContainer = styled.div`
const MacroDetailTabWrapper = styled.div` const MacroDetailTabWrapper = styled.div`
display: flex; display: flex;
overflow-y: auto;
`; `;
const MacroSection = styled.div` const MacroSection = styled.div`

View File

@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import _ from 'lodash'; import _ from 'lodash';
import styled from 'styled-components';
import { import {
FormTextField, FormTextField,
FormSubmit, FormSubmit,
@ -11,6 +12,9 @@ import {
} from '../utility/forms'; } from '../utility/forms';
import { Formik, Form, useFormikContext } from 'formik'; import { Formik, Form, useFormikContext } from 'formik';
const MacroArgumentsWrapper = styled.div`
`;
function MacroArgument({ arg, namePrefix }) { function MacroArgument({ arg, namePrefix }) {
const name = `${namePrefix}${arg.name}`; const name = `${namePrefix}${arg.name}`;
if (arg.type == 'text') { if (arg.type == 'text') {
@ -37,12 +41,12 @@ function MacroArgumentList({ args, onChangeValues, namePrefix }) {
if (onChangeValues) onChangeValues(values); if (onChangeValues) onChangeValues(values);
}, [values]); }, [values]);
return ( return (
<> <MacroArgumentsWrapper>
{' '} {' '}
{args.map((arg) => ( {args.map((arg) => (
<MacroArgument arg={arg} key={arg.name} namePrefix={namePrefix} /> <MacroArgument arg={arg} key={arg.name} namePrefix={namePrefix} />
))} ))}
</> </MacroArgumentsWrapper>
); );
} }

View File

@ -176,18 +176,129 @@ return {
}, },
], ],
}, },
{
title: 'Extract date fields',
name: 'extractDateFields',
group: 'Tools',
description: 'Extract yaear, month, day and other date/time fields from selection and adds it as new columns',
type: 'transformData',
code: `
const selectedColumnNames = modules.lodash.uniq(selectedCells.map(x => x.column));
const selectedRowIndexes = modules.lodash.uniq(selectedCells.map(x => x.row));
const addedColumnNames = modules.lodash.compact([args.year, args.month, args.day, args.hour, args.minute, args.second]);
const selectedRows = modules.lodash.groupBy(selectedCells, 'row');
const resultRows = rows.map((row, rowIndex) => {
if (!selectedRowIndexes.includes(rowIndex)) return {
...row,
__insertedFields: addedColumnNames,
};
let mom = null;
for(const cell of selectedRows[rowIndex]) {
const m = modules.moment(row[cell.column]);
if (m.isValid()) {
mom = m;
break;
}
}
if (!mom) return {
...row,
__insertedFields: addedColumnNames,
};
const fields = {
year: mom.year(),
month: mom.month() + 1,
day: mom.day(),
hour: mom.hour(),
minute: mom.minute(),
second: mom.second(),
};
return {
...row,
...modules.lodash.pick(fields, addedColumnNames),
__insertedFields: addedColumnNames,
}
});
const resultCols = [
...cols,
...addedColumnNames,
];
return {
rows: resultRows,
cols: resultCols,
}
`,
args: [
{
type: 'text',
label: 'Year name',
name: 'year',
default: 'year',
},
{
type: 'text',
label: 'Month name',
name: 'month',
default: 'month',
},
{
type: 'text',
label: 'Day name',
name: 'day',
default: 'day',
},
{
type: 'text',
label: 'Hour name',
name: 'hour',
default: 'hour',
},
{
type: 'text',
label: 'Minute name',
name: 'minute',
default: 'minute',
},
{
type: 'text',
label: 'Second name',
name: 'second',
default: 'second',
},
],
},
]; ];
// function f() { // function f() {
// const selectedColumnNames = modules.lodash.uniq(selectedCells.map((x) => x.column)); // const selectedColumnNames = modules.lodash.uniq(selectedCells.map((x) => x.column));
// const addedColumnNames = selectedColumnNames.map((col) => (args.prefix || '') + col + (args.postfix || '')); // const selectedRowIndexes = modules.lodash.uniq(selectedCells.map((x) => x.row));
// const resultRows = rows.map((row) => ({ // const addedColumnNames = modules.lodash.compact(args.year, args.month, args.day, args.hour, args.minute, args.second);
// ...row, // const selectedRows = modules.lodash.groupBy(selectedCells, 'row');
// ...modules.lodash.fromPairs(
// selectedColumnNames.map((col) => [(args.prefix || '') + col + (args.postfix || ''), row[col]]) // const resultRows = rows.map((row, rowIndex) => {
// ), // if (!selectedRowIndexes.includes(rowIndex)) return row;
// __insertedFields: addedColumnNames, // const mom = selectedRows[index].find((x) => {
// })); // const m = modules.moment(row[x.column]);
// if (m.isValid()) return m;
// });
// if (!mom) return row;
// const fields = {
// year: mom.year(),
// month: mom.month(),
// day: mom.day(),
// hour: mom.hour(),
// minute: mom.minute(),
// second: mom.second(),
// };
// return {
// ...row,
// ...modules.lodash.pick(fields, addedColumnNames),
// __insertedFields: addedColumnNames,
// };
// });
// const resultCols = [...cols, ...addedColumnNames]; // const resultCols = [...cols, ...addedColumnNames];
// return { // return {
// rows: resultRows, // rows: resultRows,