Préparation pour avoir plus de styles de date

This commit is contained in:
Vincent Calame 2022-07-31 02:57:01 +02:00
parent c94285283c
commit b4e7a254bd
4 changed files with 57 additions and 30 deletions

View File

@ -103,7 +103,8 @@ OpenDocument.OdsConverter.prototype.convert = function (table, options) {
.addNumberTableCell(odCell);
break;
case "date":
odCell.styleName = styleManager.getAutomaticCellStyleName("date", odCell.styleName);
let datePattern = "YYYY-MM-DD";
odCell.styleName = styleManager.getAutomaticCellStyleName("date", odCell.styleName, datePattern);
xw
.addDateTableCell(odCell);
break;

View File

@ -16,8 +16,10 @@ OpenDocument.StyleManager = function () {
for(let object of OpenDocument.StyleManager.DEFAULT_STYLES ) {
this.putStyle(object.type + "-named", _buildDefault(object));
}
this.automaticStyleNumber = 1;
this.currencyMap = new Map();
this.automaticCellStyleNumber = 1;
this.dataStyleNumber = 1;
this.currencyDataStyleMap = new Map();
this.dateDataStyleMap = new Map();
OpenDocument.StyleManager.readDocumentStyleSheets(this);
function _buildDefault(object) {
@ -68,35 +70,22 @@ OpenDocument.StyleManager.prototype.getStyle = function (mapName, styleKey) {
return this.map.get(styleKey);
};
OpenDocument.StyleManager.prototype.getAutomaticCellStyleName = function (type, parentStyleName, currencyCode) {
var styleKey = type + ":";
if (type === "currency") {
styleKey += currencyCode + ":";
}
styleKey += parentStyleName;
OpenDocument.StyleManager.prototype.getAutomaticCellStyleName = function (type, parentStyleName, cellOption) {
var styleKey = _buildStyleKey();
if (this.hasStyle("cell-automatic", styleKey)) {
return this.getStyle("cell-automatic", styleKey).styleName;
}
var name = OpenDocument.CELLSTYLE_PREFIX + this.automaticStyleNumber;
this.automaticStyleNumber++;
var automaticCellStyleName = this.getNewAutomaticeCellStyleName();
var dataStyleName = null;
switch(type) {
case "date":
dataStyleName = OpenDocument.ISODATE_DATASTYLE_NAME;
dataStyleName = this.getMatchingDateDataStyleName(cellOption);
break;
case "currency":
let number;
if (this.currencyMap.has(currencyCode)) {
number = this.currencyMap.get(currencyCode);
} else {
number = this.automaticStyleNumber;
this.automaticStyleNumber++;
this.currencyMap.set(currencyCode, number);
}
dataStyleName = OpenDocument.DATASTYLE_PREFIX + number;
dataStyleName = this.getMatchingCurrencyDataStyleName(cellOption);
break;
}
var cellStyle = new OpenDocument.Style("cell", name);
var cellStyle = new OpenDocument.Style("cell", automaticCellStyleName);
if (parentStyleName) {
cellStyle.setParent(parentStyleName);
} else {
@ -106,9 +95,46 @@ OpenDocument.StyleManager.prototype.getAutomaticCellStyleName = function (type,
cellStyle.setDataStyle(dataStyleName);
}
this.putStyle("cell-automatic", cellStyle, styleKey);
return automaticCellStyleName;
function _buildStyleKey() {
let key = type + ":";
if (cellOption) {
key += cellOption + ":";
}
key += parentStyleName;
return key;
}
};
OpenDocument.StyleManager.prototype.getNewAutomaticeCellStyleName = function () {
var name = OpenDocument.CELLSTYLE_PREFIX + this.automaticCellStyleNumber;
this.automaticCellStyleNumber++;
return name;
};
OpenDocument.StyleManager.prototype.getMatchingCurrencyDataStyleName = function (currencyCode) {
if (this.currencyDataStyleMap.has(currencyCode)) {
return this.currencyDataStyleMap.get(currencyCode);
} else {
let dataStyleName = OpenDocument.DATASTYLE_PREFIX + this.dataStyleNumber;
this.dataStyleNumber++;
this.currencyDataStyleMap.set(currencyCode, dataStyleName);
return dataStyleName;
}
};
OpenDocument.StyleManager.prototype.getMatchingDateDataStyleName = function (datePattern) {
if (this.dateDataStyleMap.has(datePattern)) {
return this.dateDataStyleMap.get(datePattern);
} else {
let dataStyleName = OpenDocument.DATASTYLE_PREFIX + this.dataStyleNumber;
this.dataStyleNumber++;
this.dateDataStyleMap.set(datePattern, dataStyleName);
return dataStyleName;
}
};
OpenDocument.StyleManager.prototype.getMatchingStyleName = function (type, element) {
var map = this.matchingClassMaps.get(type);
if (map) {
@ -135,9 +161,11 @@ OpenDocument.StyleManager.prototype.writeStyles = function (mapName, xmlWriter)
};
OpenDocument.StyleManager.prototype.writeDataStyles = function (xmlWriter) {
xmlWriter
.addDateStyle();
for(let entry of this.currencyMap) {
for(let entry of this.dateDataStyleMap) {
xmlWriter
.addDateStyle(entry[0], entry[1]);
}
for(let entry of this.currencyDataStyleMap) {
xmlWriter
.addCurrencyStyle(entry[0], entry[1]);
}

View File

@ -193,10 +193,10 @@ OpenDocument.XmlWriter.prototype.closeStyle = function () {
return this;
};
OpenDocument.XmlWriter.prototype.addDateStyle = function () {
OpenDocument.XmlWriter.prototype.addDateStyle = function (datePattern, dataStyleName) {
this
.startOpenTag("number:date-style")
.addAttribute("style:name", OpenDocument.ISODATE_DATASTYLE_NAME)
.addAttribute("style:name", dataStyleName)
.endOpenTag()
.startOpenTag("number:year")
.addAttribute("number:style", "long")
@ -213,9 +213,8 @@ OpenDocument.XmlWriter.prototype.addDateStyle = function () {
return this;
};
OpenDocument.XmlWriter.prototype.addCurrencyStyle = function (currencyCode, number) {
OpenDocument.XmlWriter.prototype.addCurrencyStyle = function (currencyCode, dataStyleName) {
var currency = Currency.get(currencyCode);
var dataStyleName = OpenDocument.DATASTYLE_PREFIX + number;
this
.startOpenTag("number:currency-style")
.addAttribute("style:name", dataStyleName + "P0")

View File

@ -6,7 +6,6 @@
var OpenDocument = {};
OpenDocument.DEFAULT_CELLSTYLE_NAME = "Default";
OpenDocument.ISODATE_DATASTYLE_NAME = "N101";
OpenDocument.COLUMNSTYLE_PREFIX = "co";
OpenDocument.CELLSTYLE_PREFIX = "ce";
OpenDocument.DATASTYLE_PREFIX = "N";