This closes #1447, add support for strict theme namespace

- Support specify if applying number format style for the cell calculation result
- Reduce cyclomatic complexities for the OpenReader function
pull/2/head
xuri 2 years ago
parent 00c58a73f3
commit 4f0025aab0
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7

@ -767,20 +767,29 @@ type formulaFuncs struct {
// YIELDMAT // YIELDMAT
// Z.TEST // Z.TEST
// ZTEST // ZTEST
func (f *File) CalcCellValue(sheet, cell string) (result string, err error) { func (f *File) CalcCellValue(sheet, cell string, opts ...Options) (result string, err error) {
var token formulaArg var (
token, err = f.calcCellValue(&calcContext{ rawCellValue = parseOptions(opts...).RawCellValue
styleIdx int
token formulaArg
)
if token, err = f.calcCellValue(&calcContext{
entry: fmt.Sprintf("%s!%s", sheet, cell), entry: fmt.Sprintf("%s!%s", sheet, cell),
iterations: make(map[string]uint), iterations: make(map[string]uint),
}, sheet, cell) }, sheet, cell); err != nil {
return
}
if !rawCellValue {
styleIdx, _ = f.GetCellStyle(sheet, cell)
}
result = token.Value() result = token.Value()
if isNum, precision, decimal := isNumeric(result); isNum { if isNum, precision, decimal := isNumeric(result); isNum {
if precision > 15 { if precision > 15 {
result = strings.ToUpper(strconv.FormatFloat(decimal, 'G', 15, 64)) result, err = f.formattedValue(styleIdx, strings.ToUpper(strconv.FormatFloat(decimal, 'G', 15, 64)), rawCellValue)
return return
} }
if !strings.HasPrefix(result, "0") { if !strings.HasPrefix(result, "0") {
result = strings.ToUpper(strconv.FormatFloat(decimal, 'f', -1, 64)) result, err = f.formattedValue(styleIdx, strings.ToUpper(strconv.FormatFloat(decimal, 'f', -1, 64)), rawCellValue)
} }
} }
return return

@ -132,15 +132,9 @@ func newFile() *File {
} }
} }
// OpenReader read data stream from io.Reader and return a populated // checkOpenReaderOptions check and validate options field value for open
// spreadsheet file. // reader.
func OpenReader(r io.Reader, opts ...Options) (*File, error) { func (f *File) checkOpenReaderOptions() error {
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}
f := newFile()
f.options = parseOptions(opts...)
if f.options.UnzipSizeLimit == 0 { if f.options.UnzipSizeLimit == 0 {
f.options.UnzipSizeLimit = UnzipSizeLimit f.options.UnzipSizeLimit = UnzipSizeLimit
if f.options.UnzipXMLSizeLimit > f.options.UnzipSizeLimit { if f.options.UnzipXMLSizeLimit > f.options.UnzipSizeLimit {
@ -154,7 +148,22 @@ func OpenReader(r io.Reader, opts ...Options) (*File, error) {
} }
} }
if f.options.UnzipXMLSizeLimit > f.options.UnzipSizeLimit { if f.options.UnzipXMLSizeLimit > f.options.UnzipSizeLimit {
return nil, ErrOptionsUnzipSizeLimit return ErrOptionsUnzipSizeLimit
}
return nil
}
// OpenReader read data stream from io.Reader and return a populated
// spreadsheet file.
func OpenReader(r io.Reader, opts ...Options) (*File, error) {
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}
f := newFile()
f.options = parseOptions(opts...)
if err = f.checkOpenReaderOptions(); err != nil {
return nil, err
} }
if bytes.Contains(b, oleIdentifier) { if bytes.Contains(b, oleIdentifier) {
if b, err = Decrypt(b, f.options); err != nil { if b, err = Decrypt(b, f.options); err != nil {

@ -497,12 +497,16 @@ func (avb *attrValBool) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
// Transitional namespaces. // Transitional namespaces.
func namespaceStrictToTransitional(content []byte) []byte { func namespaceStrictToTransitional(content []byte) []byte {
namespaceTranslationDic := map[string]string{ namespaceTranslationDic := map[string]string{
StrictSourceRelationship: SourceRelationship.Value, StrictNameSpaceDocumentPropertiesVariantTypes: NameSpaceDocumentPropertiesVariantTypes.Value,
StrictSourceRelationshipOfficeDocument: SourceRelationshipOfficeDocument, StrictNameSpaceDrawingMLMain: NameSpaceDrawingMLMain,
StrictSourceRelationshipChart: SourceRelationshipChart, StrictNameSpaceExtendedProperties: NameSpaceExtendedProperties,
StrictSourceRelationshipComments: SourceRelationshipComments, StrictNameSpaceSpreadSheet: NameSpaceSpreadSheet.Value,
StrictSourceRelationshipImage: SourceRelationshipImage, StrictSourceRelationship: SourceRelationship.Value,
StrictNameSpaceSpreadSheet: NameSpaceSpreadSheet.Value, StrictSourceRelationshipChart: SourceRelationshipChart,
StrictSourceRelationshipComments: SourceRelationshipComments,
StrictSourceRelationshipExtendProperties: SourceRelationshipExtendProperties,
StrictSourceRelationshipImage: SourceRelationshipImage,
StrictSourceRelationshipOfficeDocument: SourceRelationshipOfficeDocument,
} }
for s, n := range namespaceTranslationDic { for s, n := range namespaceTranslationDic {
content = bytesReplace(content, []byte(s), []byte(n), -1) content = bytesReplace(content, []byte(s), []byte(n), -1)

@ -41,7 +41,7 @@ func (mc *xlsxMergeCell) Rect() ([]int, error) {
// B1(x1,y1) D1(x2,y1) // B1(x1,y1) D1(x2,y1)
// +------------------------+ // +------------------------+
// | | // | |
// A4(x3,y3) | C4(x4,y3) | // A4(x3,y3) | C4(x4,y3) |
// +------------------------+ | // +------------------------+ |
// | | | | // | | | |
// | |B5(x1,y2) | D5(x2,y2)| // | |B5(x1,y2) | D5(x2,y2)|

@ -38,48 +38,55 @@ var (
// Source relationship and namespace. // Source relationship and namespace.
const ( const (
ContentTypeAddinMacro = "application/vnd.ms-excel.addin.macroEnabled.main+xml" ContentTypeAddinMacro = "application/vnd.ms-excel.addin.macroEnabled.main+xml"
ContentTypeDrawing = "application/vnd.openxmlformats-officedocument.drawing+xml" ContentTypeDrawing = "application/vnd.openxmlformats-officedocument.drawing+xml"
ContentTypeDrawingML = "application/vnd.openxmlformats-officedocument.drawingml.chart+xml" ContentTypeDrawingML = "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"
ContentTypeMacro = "application/vnd.ms-excel.sheet.macroEnabled.main+xml" ContentTypeMacro = "application/vnd.ms-excel.sheet.macroEnabled.main+xml"
ContentTypeSheetML = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" ContentTypeSheetML = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"
ContentTypeSpreadSheetMLChartsheet = "application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml" ContentTypeSpreadSheetMLChartsheet = "application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml"
ContentTypeSpreadSheetMLComments = "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml" ContentTypeSpreadSheetMLComments = "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml"
ContentTypeSpreadSheetMLPivotCacheDefinition = "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml" ContentTypeSpreadSheetMLPivotCacheDefinition = "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml"
ContentTypeSpreadSheetMLPivotTable = "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml" ContentTypeSpreadSheetMLPivotTable = "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml"
ContentTypeSpreadSheetMLSharedStrings = "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml" ContentTypeSpreadSheetMLSharedStrings = "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"
ContentTypeSpreadSheetMLTable = "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml" ContentTypeSpreadSheetMLTable = "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml"
ContentTypeSpreadSheetMLWorksheet = "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" ContentTypeSpreadSheetMLWorksheet = "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"
ContentTypeTemplate = "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml" ContentTypeTemplate = "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml"
ContentTypeTemplateMacro = "application/vnd.ms-excel.template.macroEnabled.main+xml" ContentTypeTemplateMacro = "application/vnd.ms-excel.template.macroEnabled.main+xml"
ContentTypeVBA = "application/vnd.ms-office.vbaProject" ContentTypeVBA = "application/vnd.ms-office.vbaProject"
ContentTypeVML = "application/vnd.openxmlformats-officedocument.vmlDrawing" ContentTypeVML = "application/vnd.openxmlformats-officedocument.vmlDrawing"
NameSpaceDublinCore = "http://purl.org/dc/elements/1.1/" NameSpaceDrawingMLMain = "http://schemas.openxmlformats.org/drawingml/2006/main"
NameSpaceDublinCoreMetadataInitiative = "http://purl.org/dc/dcmitype/" NameSpaceDublinCore = "http://purl.org/dc/elements/1.1/"
NameSpaceDublinCoreTerms = "http://purl.org/dc/terms/" NameSpaceDublinCoreMetadataInitiative = "http://purl.org/dc/dcmitype/"
NameSpaceXML = "http://www.w3.org/XML/1998/namespace" NameSpaceDublinCoreTerms = "http://purl.org/dc/terms/"
NameSpaceXMLSchemaInstance = "http://www.w3.org/2001/XMLSchema-instance" NameSpaceExtendedProperties = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
SourceRelationshipChart = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" NameSpaceXML = "http://www.w3.org/XML/1998/namespace"
SourceRelationshipChartsheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" NameSpaceXMLSchemaInstance = "http://www.w3.org/2001/XMLSchema-instance"
SourceRelationshipComments = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" SourceRelationshipChart = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"
SourceRelationshipDialogsheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/dialogsheet" SourceRelationshipChartsheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet"
SourceRelationshipDrawingML = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" SourceRelationshipComments = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"
SourceRelationshipDrawingVML = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing" SourceRelationshipDialogsheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/dialogsheet"
SourceRelationshipHyperLink = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" SourceRelationshipDrawingML = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"
SourceRelationshipImage = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" SourceRelationshipDrawingVML = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing"
SourceRelationshipOfficeDocument = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" SourceRelationshipExtendProperties = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"
SourceRelationshipPivotCache = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition" SourceRelationshipHyperLink = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
SourceRelationshipPivotTable = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotTable" SourceRelationshipImage = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
SourceRelationshipSharedStrings = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" SourceRelationshipOfficeDocument = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
SourceRelationshipTable = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table" SourceRelationshipPivotCache = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition"
SourceRelationshipVBAProject = "http://schemas.microsoft.com/office/2006/relationships/vbaProject" SourceRelationshipPivotTable = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotTable"
SourceRelationshipWorkSheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" SourceRelationshipSharedStrings = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"
StrictNameSpaceSpreadSheet = "http://purl.oclc.org/ooxml/spreadsheetml/main" SourceRelationshipTable = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table"
StrictSourceRelationship = "http://purl.oclc.org/ooxml/officeDocument/relationships" SourceRelationshipVBAProject = "http://schemas.microsoft.com/office/2006/relationships/vbaProject"
StrictSourceRelationshipChart = "http://purl.oclc.org/ooxml/officeDocument/relationships/chart" SourceRelationshipWorkSheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
StrictSourceRelationshipComments = "http://purl.oclc.org/ooxml/officeDocument/relationships/comments" StrictNameSpaceDocumentPropertiesVariantTypes = "http://purl.oclc.org/ooxml/officeDocument/docPropsVTypes"
StrictSourceRelationshipImage = "http://purl.oclc.org/ooxml/officeDocument/relationships/image" StrictNameSpaceDrawingMLMain = "http://purl.oclc.org/ooxml/drawingml/main"
StrictSourceRelationshipOfficeDocument = "http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument" StrictNameSpaceExtendedProperties = "http://purl.oclc.org/ooxml/officeDocument/extendedProperties"
StrictNameSpaceSpreadSheet = "http://purl.oclc.org/ooxml/spreadsheetml/main"
StrictSourceRelationship = "http://purl.oclc.org/ooxml/officeDocument/relationships"
StrictSourceRelationshipChart = "http://purl.oclc.org/ooxml/officeDocument/relationships/chart"
StrictSourceRelationshipComments = "http://purl.oclc.org/ooxml/officeDocument/relationships/comments"
StrictSourceRelationshipExtendProperties = "http://purl.oclc.org/ooxml/officeDocument/relationships/extendedProperties"
StrictSourceRelationshipImage = "http://purl.oclc.org/ooxml/officeDocument/relationships/image"
StrictSourceRelationshipOfficeDocument = "http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument"
// ExtURIConditionalFormattings is the extLst child element // ExtURIConditionalFormattings is the extLst child element
// ([ISO/IEC29500-1:2016] section 18.2.10) of the worksheet element // ([ISO/IEC29500-1:2016] section 18.2.10) of the worksheet element
// ([ISO/IEC29500-1:2016] section 18.3.1.99) is extended by the addition of // ([ISO/IEC29500-1:2016] section 18.3.1.99) is extended by the addition of

Loading…
Cancel
Save