1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include "workbookhelper.hxx"
23 #include <oox/helper/binarystreambase.hxx>
24 #include <oox/helper/refvector.hxx>
30 namespace oox
{ class AttributeList
; }
31 namespace oox
{ class SequenceInputStream
; }
35 // codes for built-in names
36 const sal_Unicode BIFF_DEFNAME_CONSOLIDATEAREA
= '\x00';
37 const sal_Unicode BIFF_DEFNAME_AUTOOPEN
= '\x01'; // Sheet macro executed when workbook is opened.
38 const sal_Unicode BIFF_DEFNAME_AUTOCLOSE
= '\x02'; // Sheet macro executed when workbook is closed.
39 const sal_Unicode BIFF_DEFNAME_EXTRACT
= '\x03'; // Filter output destination for advanced filter.
40 const sal_Unicode BIFF_DEFNAME_DATABASE
= '\x04';
41 const sal_Unicode BIFF_DEFNAME_CRITERIA
= '\x05'; // Filter criteria source range for advanced filter.
42 const sal_Unicode BIFF_DEFNAME_PRINTAREA
= '\x06'; // Print ranges.
43 const sal_Unicode BIFF_DEFNAME_PRINTTITLES
= '\x07'; // Rows/columns repeated on each page when printing.
44 const sal_Unicode BIFF_DEFNAME_RECORDER
= '\x08';
45 const sal_Unicode BIFF_DEFNAME_DATAFORM
= '\x09';
46 const sal_Unicode BIFF_DEFNAME_AUTOACTIVATE
= '\x0A'; // Sheet macro executed when workbook is activated.
47 const sal_Unicode BIFF_DEFNAME_AUTODEACTIVATE
= '\x0B'; // Sheet macro executed when workbook is deactivated.
48 const sal_Unicode BIFF_DEFNAME_SHEETTITLE
= '\x0C';
49 const sal_Unicode BIFF_DEFNAME_FILTERDATABASE
= '\x0D'; // Sheet range autofilter or advanced filter works on.
50 const sal_Unicode BIFF_DEFNAME_UNKNOWN
= '\x0E';
52 struct DefinedNameModel
54 OUString maName
; /// The original name.
55 OUString maFormula
; /// The formula string.
56 sal_Int32 mnSheet
; /// Sheet index for local names.
57 sal_Int32 mnFuncGroupId
; /// Function group identifier.
58 bool mbMacro
; /// True = Macro name (VBA or sheet macro).
59 bool mbFunction
; /// True = function, false = command.
60 bool mbVBName
; /// True = VBA macro, false = sheet macro.
61 bool mbHidden
; /// True = name hidden in UI.
63 explicit DefinedNameModel();
66 /** Base class for defined names and external names. */
67 class DefinedNameBase
: public WorkbookHelper
70 explicit DefinedNameBase( const WorkbookHelper
& rHelper
);
72 /** Returns the original name as imported from or exported to the file. */
73 const OUString
& getModelName() const { return maModel
.maName
; }
74 /** Returns the name as used in the Calc document. */
75 const OUString
& getCalcName() const { return maCalcName
; }
77 /** Returns the original name as imported from or exported to the file. */
78 const OUString
& getUpcaseModelName() const;
81 DefinedNameModel maModel
; /// Model data for this defined name.
82 mutable OUString maUpModelName
; /// Model name converted to uppercase ASCII.
83 OUString maCalcName
; /// Final name used in the Calc document.
86 class DefinedName final
: public DefinedNameBase
89 explicit DefinedName( const WorkbookHelper
& rHelper
);
90 virtual ~DefinedName() override
;
92 /** Sets the attributes for this defined name from the passed attribute set. */
93 void importDefinedName( const AttributeList
& rAttribs
);
94 /** Sets the formula string from the body of the definedName element. */
95 void setFormula( const OUString
& rFormula
);
96 /** Imports the defined name from a DEFINEDNAME record in the passed stream. */
97 void importDefinedName( SequenceInputStream
& rStrm
);
99 /** Creates a defined name in the Calc document. */
100 void createNameObject( sal_Int32 nIndex
);
101 /** Converts the formula string or BIFF token array for this defined name. */
102 void convertFormula( const css::uno::Sequence
<css::sheet::ExternalLinkInfo
>& rExternalLinks
);
103 std::unique_ptr
<ScTokenArray
> getScTokens( const css::uno::Sequence
<css::sheet::ExternalLinkInfo
>& rExternalLinks
);
104 /** Returns true, if this defined name is global in the document. */
105 bool isGlobalName() const { return mnCalcSheet
< 0; }
106 /** Returns true, if this defined name is a special builtin name. */
107 bool isBuiltinName() const { return mcBuiltinId
!= BIFF_DEFNAME_UNKNOWN
; }
108 /** Returns true, if this defined name is a macro function call. */
109 bool isMacroFunction() const { return maModel
.mbMacro
&& maModel
.mbFunction
; }
110 /** Returns true, if this defined name is a reference to a VBA macro. */
111 bool isVBName() const { return maModel
.mbMacro
&& maModel
.mbVBName
; }
113 /** Returns the 0-based sheet index for local names, or -1 for global names. */
114 sal_Int16
getLocalCalcSheet() const { return mnCalcSheet
; }
115 /** Returns the built-in identifier of the defined name. */
116 sal_Unicode
getBuiltinId() const { return mcBuiltinId
; }
117 /** Returns the token index used in API token arrays (com.sun.star.sheet.FormulaToken). */
118 sal_Int32
getTokenIndex() const { return mnTokenIndex
; }
119 /** Tries to resolve the defined name to an absolute cell range. */
120 bool getAbsoluteRange( ScRange
& orRange
) const;
121 bool isValid(const css::uno::Sequence
<css::sheet::ExternalLinkInfo
>& rExternalLinks
) const;
124 typedef ::std::unique_ptr
< StreamDataSequence
> StreamDataSeqPtr
;
126 RangeDataRet maScRangeData
; /// ScRangeData of the defined name.
127 sal_Int32 mnTokenIndex
; /// Name index used in API token array.
128 sal_Int16 mnCalcSheet
; /// Calc sheet index for sheet-local names.
129 sal_Unicode mcBuiltinId
; /// Identifier for built-in defined names.
130 StreamDataSeqPtr mxFormula
; /// Formula data for BIFF12 import.
133 typedef std::shared_ptr
< DefinedName
> DefinedNameRef
;
135 class DefinedNamesBuffer final
: public WorkbookHelper
138 explicit DefinedNamesBuffer( const WorkbookHelper
& rHelper
);
140 /** Imports a defined name from the passed attribute set. */
141 DefinedNameRef
importDefinedName( const AttributeList
& rAttribs
);
142 /** Imports a defined name from a DEFINEDNAME record in the passed stream. */
143 void importDefinedName( SequenceInputStream
& rStrm
);
145 /** Creates all defined names in the document. */
146 void finalizeImport();
148 /** Returns a defined name by zero-based index (order of appearance). */
149 DefinedNameRef
getByIndex( sal_Int32 nIndex
) const;
150 /** Returns a defined name by token index (index in XDefinedNames container). */
151 DefinedNameRef
getByTokenIndex( sal_Int32 nIndex
) const;
152 /** Returns a defined name by its model name.
153 @param nSheet The sheet index for local names or -1 for global names.
154 If no local name is found, tries to find a matching global name.
155 @return Reference to the defined name or empty reference. */
156 DefinedNameRef
getByModelName( const OUString
& rModelName
, sal_Int16 nCalcSheet
= -1 ) const;
157 /** Returns a built-in defined name by its built-in identifier.
158 @param nSheet The sheet index of the built-in name.
159 @return Reference to the defined name or empty reference. */
160 DefinedNameRef
getByBuiltinId( sal_Unicode cBuiltinId
, sal_Int16 nCalcSheet
) const;
163 DefinedNameRef
createDefinedName();
166 typedef ::std::pair
< sal_Int16
, OUString
> SheetNameKey
;
167 typedef ::std::pair
< sal_Int16
, sal_Unicode
> BuiltinKey
;
169 typedef RefVector
< DefinedName
> DefNameVector
;
170 typedef RefMap
< SheetNameKey
, DefinedName
> DefNameNameMap
;
171 typedef RefMap
< BuiltinKey
, DefinedName
> DefNameBuiltinMap
;
172 typedef RefMap
< sal_Int32
, DefinedName
> DefNameTokenIdMap
;
174 DefNameVector maDefNames
; /// List of all defined names in insertion order.
175 DefNameNameMap maModelNameMap
; /// Maps all defined names by sheet index and model name.
176 DefNameBuiltinMap maBuiltinMap
; /// Maps all defined names by sheet index and built-in identifier.
177 DefNameTokenIdMap maTokenIdMap
; /// Maps all defined names by API token index.
180 } // namespace oox::xls
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */