Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T shanim/add datetime data type to initial data #45

Open
wants to merge 7 commits into
base: t-shanim/initial-data-population
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/connected-workbooks",
"version": "1.0.0",
"version": "2.1.10-beta",
"description": "Use this library to generate Excel workbooks with a custom Mashup Query loaded to the grid, to be used by applications for 'Export to Excel' features to allow refreshable workbooks",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -40,6 +40,7 @@
"@types/base64-js": "^1.3.0",
"@types/jest": "^26.0.24",
"@types/jszip": "^3.4.1",
"@types/uuid": "^9.0.2",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"babel-jest": "^27.0.6",
Expand Down
31 changes: 29 additions & 2 deletions src/GridParser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { GridNotFoundErr, headerNotFoundErr, invalidDataTypeErr, invalidValueInColumnErr } from "./constants";
import { dateFormats, GridNotFoundErr, headerNotFoundErr, invalidDataTypeErr, invalidFormatTypeErr, invalidMissingFormatFromDateTimeErr, invalidValueInColumnErr, dateFormatsRegex, milliSecPerDay, numberOfDaysTillExcelBeginYear, monthsbeforeLeap } from "./constants";
import { ColumnMetadata, dataTypes, Grid, TableData } from "./types";

export default class GridParser {
export class GridParser {
public parseToTableData(initialDataGrid: Grid): TableData | undefined {
if (!initialDataGrid) {

return undefined;
}

Expand Down Expand Up @@ -38,6 +39,13 @@ export default class GridParser {
}
}

if (dataType == dataTypes.dateTime) {
if (dateFormatsRegex[columnMetadata[colIndex].format!].test(cellValue.toString())) {
throw new Error(invalidValueInColumnErr);
}
rowData[prop] = this.convertToExcelDate(cellValue.toString());
}

row.push(rowData[prop].toString());
colIndex++;
}
Expand All @@ -47,6 +55,16 @@ export default class GridParser {
return tableData;
}

private convertToExcelDate(dateStr: string) {
const [month, day, year, hour, minute] = dateStr.split(/[\/: ]/);
const date = new Date(Date.UTC(parseInt(year), parseInt(month) - 1, parseInt(day), parseInt(hour), parseInt(minute), 0,0)).getTime();
// Excel incorrectly assumes that the year 1900 is a leap year. This is a workaround for that
if (parseInt(year) == 1900 && parseInt(month) <= monthsbeforeLeap) {
return ((date + numberOfDaysTillExcelBeginYear*milliSecPerDay) / (milliSecPerDay)) - 1;
}
return (date + numberOfDaysTillExcelBeginYear*milliSecPerDay) / (milliSecPerDay);
}

private validateGridHeader(data: Grid) {
const headerData: ColumnMetadata[] = data.Header;
if (!headerData) {
Expand All @@ -57,6 +75,15 @@ export default class GridParser {
if (!(headerData[prop].type in dataTypes)) {
throw new Error(invalidDataTypeErr);
}

if (headerData[prop].type == dataTypes.dateTime && headerData[prop].format == undefined) {
throw new Error(invalidMissingFormatFromDateTimeErr);
}

if (headerData[prop].format != undefined && !(headerData[prop].format! in dateFormats)) {
throw new Error(invalidFormatTypeErr);
}

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/TableDataParserFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import GridParser from "./GridParser";
import {GridParser} from "./GridParser";
import { TableDataParser } from "./types";

export default class TableDataParserFactory {
Expand Down
36 changes: 33 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const pivotCachesPath = "xl/pivotCache/";
export const section1mPath = "Formulas/Section1.m";
export const docPropsCoreXmlPath = "docProps/core.xml";
export const docPropsRootElement = "cp:coreProperties";
export const stylesXmlPath = "xl/styles.xml";

export const sharedStringsNotFoundErr = "SharedStrings were not found in template";
export const connectionsNotFoundErr = "Connections were not found in template";
Expand All @@ -28,6 +29,9 @@ export const GridNotFoundErr = "Invalid JSON file, grid data is missing";
export const invalidValueInColumnErr = "Invalid cell value in column";
export const headerNotFoundErr = "Invalid JSON file, header is missing";
export const invalidDataTypeErr = "Invalid JSON file, invalid data type";
export const invalidFormatTypeErr = "Invalid JSON file, invalid format type";
export const stylesNotFoundErr = "Styles were not found in template";
export const invalidMissingFormatFromDateTimeErr = "Invalid JSON file, missing format from dateTime";

export const blobFileType = "blob";
export const uint8ArrayType = "uint8array";
Expand Down Expand Up @@ -66,7 +70,12 @@ export const element = {
queryTableRefresh: "queryTableRefresh",
sheetData: "sheetData",
row: "row",
dimension: "dimension"
dimension: "dimension",
differentialFormats: "dxfs",
differentialFormat: "dxf",
numberFormat: "numFmt",
cellFormats: "cellXfs",
cellFormat: "xf"
}

export const elementAttributes = {
Expand Down Expand Up @@ -96,22 +105,43 @@ export const elementAttributes = {
nextId: "nextId",
row: "r",
spans: "spans",
x14acDyDescent: "x14ac:dyDescent"
x14acDyDescent: "x14ac:dyDescent",
numberFormatId: "numFmtId",
formatCode: "formatCode",
dataDiffFormatId: "dataDxfId",
fontId: "fontId",
fillId: "fillId",
borderId: "borderId",
formatId: "xfId",
applyNumberFormat: "applyNumberFormat"
};


export const elementAttributesValues = {
connectionName: (queryName: string) => `Query - ${queryName}`,
connectionDescription: (queryName: string) => `Connection to the '${queryName}' query in the workbook.`,
connection: (queryName: string) => `Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=${queryName};`,
connectionCommand: (queryName: string) => `SELECT * FROM [${queryName}]`,
tableResultType: () => "sTable"

}

export const milliSecPerDay = 86400000;
//This contains the number of days between 01/01/1970 and 01/01/1900
export const numberOfDaysTillExcelBeginYear = 25569;
export const monthsbeforeLeap = 2;
export const beginYear = 1900;
export const defaults = {
queryName: "Query1",
};

export const dateFormatsRegex: { [key: string]: RegExp } = {
"m/d/yyyy h:mm": /^([1-9]|0[1-9]|1[0-2])\/([1-9]|[012][0-9]|3[01])\/\d{4} ([01]\d|2[0-3]):([0-5]\d)$/
};

export const dateFormats: { [key: string]: number } = {
"m/d/yyyy h:mm": 22
};

export const URLS = {
PQ: [
"http://schemas.microsoft.com/DataMashup",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
export { WorkbookManager } from "./workbookManager";
export type { QueryInfo } from "./types";
export type {ArrayReader} from "././utils/arrayUtils";
export { GridParser } from "./GridParser";
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,25 @@ export interface TableData {
export interface ColumnMetadata {
name: string;
type: number;
format?: string;
}

export interface Grid {
Header: ColumnMetadata[];
GridData: (string|number|boolean)[][];
GridData: (string | number | boolean)[][];
}

export interface TableDataParser {
parseToTableData: (grid: any) => TableData | undefined;
}

export enum dataTypes {
autodetect = -1,
null = 0,
string = 1,
number = 2,
boolean = 3,
dateTime = 4,
}

export enum docPropsModifiableElements {
Expand Down
43 changes: 31 additions & 12 deletions src/utils/documentUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,42 @@ const createCellElement = (doc: Document, colIndex: number, rowIndex: number, da
const cellData: Element = doc.createElementNS(doc.documentElement.namespaceURI, "v");
updateCellData(dataType, data, cell, cellData);
cell.appendChild(cellData);

return cell;
};

const updateCellData = (dataType: number, data: string, cell: Element, cellData: Element) => {
switch(dataType) {
case dataTypes.string:
cell.setAttribute("t", "str");
break;
case dataTypes.number:
cell.setAttribute("t", "1");
break;
case dataTypes.boolean:
cell.setAttribute("t", "b");
break;
if (dataType == dataTypes.autodetect) {
dataType = isNaN(Number(data)) ? dataTypes.string : dataTypes.number;
if (dataType == dataTypes.string) {
if (data.toLowerCase() == "true" || data.toLowerCase() == "false") {
dataType = dataTypes.boolean;
} /* else if (!isNaN(Date.parse(data))) {
dataType = dataTypes.dateTime;
}*/
}
}
switch (dataType) {
case dataTypes.string:
cell.setAttribute("t", "str");
break;
case dataTypes.number:
cell.setAttribute("t", "1");
break;
case dataTypes.dateTime:
cell.setAttribute("s", "1");
break;
case dataTypes.boolean:
cell.setAttribute("t", "b");
break;
}
cellData.textContent = data;
};

export default { createOrUpdateProperty, getDocPropsProperties, getCellReference, createCell: createCellElement, getTableReference };
export default {
createOrUpdateProperty,
getDocPropsProperties,
getCellReference,
createCell: createCellElement,
getTableReference,
};
Loading