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); .addNumberTableCell(odCell);
break; break;
case "date": case "date":
odCell.styleName = styleManager.getAutomaticCellStyleName("date", odCell.styleName); let datePattern = "YYYY-MM-DD";
odCell.styleName = styleManager.getAutomaticCellStyleName("date", odCell.styleName, datePattern);
xw xw
.addDateTableCell(odCell); .addDateTableCell(odCell);
break; break;

View File

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

View File

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

View File

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