/*
* Copyright (C) Ascensio System SIA, 2009-2026
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation, together with the
* additional terms provided in the LICENSE file.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: https://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA by email at info@onlyoffice.com
* or by postal mail at 20A-6 Ernesta Birznieka-Upisha Street, Riga,
* LV-1050, Latvia, European Union.
*
* The interactive user interfaces in modified versions of the Program
* are required to display Appropriate Legal Notices in accordance with
* Section 5 of the GNU AGPL version 3.
*
* No trademark rights are granted under this License.
*
* All non-code elements of the Product, including illustrations,
* icon sets, and technical writing content, are licensed under the
* Creative Commons Attribution-ShareAlike 4.0 International License:
* https://creativecommons.org/licenses/by-sa/4.0/legalcode
*
* This license applies only to such non-code elements and does not
* modify or replace the licensing terms applicable to the Program's
* source code, which remains licensed under the GNU Affero General
* Public License v3.
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
#include "DefinedNames.h"
#include "../../XlsbFormat/Biff12_records/CommonRecords.h"
#include "../../../MsBinaryFile/XlsFile/Format/Logic/GlobalsSubstream.h"
#include "../../../MsBinaryFile/XlsFile/Format/Logic/Biff_records/Lbl.h"
#include "../../../MsBinaryFile/XlsFile/Format/Logic/Biff_structures/PtgList.h"
#include "../../Common/SimpleTypes_Shared.h"
namespace OOX
{
namespace Spreadsheet
{
CDefinedName::CDefinedName(OOX::Document *pMain) : WritingElement(pMain)
{
}
CDefinedName::~CDefinedName()
{
}
void CDefinedName::fromXML(XmlUtils::CXmlNode& node)
{
}
std::wstring CDefinedName::toXML() const
{
return L"";
}
void CDefinedName::toXML(NSStringUtils::CStringBuilder& writer) const
{
if (false == m_oRef.IsInit() && false == m_oName.IsInit()) return;
writer.WriteString(L"GetValue());
WritingStringNullableAttrBool(L"hidden", m_oHidden);
writer.WriteString(L">");
if(m_oRef.IsInit())
writer.WriteEncodeXmlString(*m_oRef);
writer.WriteString(L"");
}
void CDefinedName::fromXML(XmlUtils::CXmlLiteReader& oReader)
{
ReadAttributes( oReader );
if ( oReader.IsEmptyNode() )
return;
m_oRef = oReader.GetText3();
if(m_oName.IsInit())
XLS::GlobalWorkbookInfo::arDefineNames_static.push_back(m_oName.get());
}
XLS::BaseObjectPtr CDefinedName::toBin()
{
auto ptr(new XLSB::Name);
XLS::BaseObjectPtr objectPtr(ptr);
if(m_oComment.IsInit())
ptr->comment = m_oComment.get();
else
ptr->comment.setSize(0xFFFFFFFF);
if(m_oDescription.IsInit())
ptr->description = m_oDescription.get();
else
ptr->description.setSize(0xFFFFFFFF);
if(m_oFunction.IsInit())
ptr->fFunc = m_oFunction->GetValue();
else
ptr->fFunc = false;
if(m_oFunctionGroupId.IsInit())
ptr->fGrp = m_oFunctionGroupId->GetValue();
if(m_oHelp.IsInit())
ptr->helpTopic = m_oHelp.get();
else
ptr->helpTopic.setSize(0xFFFFFFFF);
if(m_oHidden.IsInit())
ptr->fHidden = m_oHidden->GetValue();
else
ptr->fHidden = false;
if (m_oLocalSheetId.IsInit())
ptr->itab = m_oLocalSheetId->GetValue();
else
ptr->itab = 0xFFFFFFFF;
if (m_oName.IsInit())
ptr->name = m_oName.get();
else
ptr->name.setSize(0xFFFFFFFF);
if (m_oPublishToServer.IsInit())
ptr->fPublished = m_oPublishToServer->GetValue();
else
ptr->fPublished = false;
if (m_oShortcutKey.IsInit())
ptr->chKey = std::stoi(m_oShortcutKey.get());
else
ptr->chKey = 0;
if (m_oVbProcedure.IsInit())
ptr->fOB = m_oVbProcedure->GetValue();
else
ptr->fOB = false;
if(!ptr->fOB)
ptr->fProc = false;
if(!ptr->fProc)
{
ptr->unusedstring1.setSize(0xFFFFFFFF);
ptr->unusedstring2.setSize(0xFFFFFFFF);
}
if (m_oWorkbookParameter.IsInit())
ptr->fWorkbookParam = m_oWorkbookParameter->GetValue();
else
ptr->fWorkbookParam = false;
if (m_oXlm.IsInit())
ptr->fFutureFunction = m_oXlm->GetValue();
else
ptr->fFutureFunction = false;
if (m_oRef.IsInit())
ptr->rgce = m_oRef.get();
ptr->fCalcExp = true;
ptr->fBuiltin = false;
return objectPtr;
}
XLS::BaseObjectPtr CDefinedName::toXLS()
{
auto name = new XLS::Lbl;
auto namePtr = XLS::BaseObjectPtr(name);
if(m_oHidden.IsInit())
name->fHidden = m_oHidden->GetValue();
if(m_oFunction.IsInit())
name->fFunc = m_oFunction->GetValue();
if(m_oVbProcedure.IsInit())
{
name->fOB = m_oVbProcedure->GetValue();
name->fProc = m_oVbProcedure->GetValue();
}
if (m_oName.IsInit())
name->Name_bin = m_oName.get();
else
name->Name_bin = L"";
if(m_oLocalSheetId.IsInit())
name->itab = m_oLocalSheetId->GetValue();
if(m_oPublishToServer.IsInit())
name->fPublished = m_oPublishToServer->GetValue();
if(m_oFunctionGroupId.IsInit())
name->fGrp = m_oFunctionGroupId->GetValue();
if(m_oWorkbookParameter.IsInit())
name->fWorkbookParam = m_oWorkbookParameter->GetValue();
auto parseFmlaValueType = [](XLS::ParsedFormula &fmla)
{
if(!fmla.rgce.sequence.empty())
{
for(auto i = 0; i < fmla.rgce.sequence.size(); i++)
{
if(fmla.rgce.sequence[i].get()->ptg_id.get() == 6424)
{
auto list = static_cast(fmla.rgce.sequence[i].get());
auto area = list->toArea();
fmla.rgce.sequence[i].reset(area);
}
}
}
return 0;
};
if(m_oRef.IsInit())
name->rgce.parseStringFormula(m_oRef.get(), L"");
parseFmlaValueType(name->rgce);
return namePtr;
}
void CDefinedName::fromBin(XLS::BaseObjectPtr& obj)
{
ReadAttributes(obj);
}
EElementType CDefinedName::getType () const
{
return et_x_DefinedName;
}
// void CDefinedName::ReadAttributes(XmlUtils::CXmlLiteReader& oReader)
// ->sheetData
void CDefinedName::ReadAttributes(XLS::BaseObjectPtr& obj)
{
auto ptr = static_cast(obj.get());
std::wstring ref = ptr->rgce.getAssembledFormula();
if (ref.empty()) return; // ???
m_oComment = ptr->comment.value();
// m_oCustomMenu = ptr->.value();
m_oDescription = ptr->description.value();
m_oFunction = ptr->fFunc;
m_oFunctionGroupId = ptr->fGrp;
m_oHelp = ptr->helpTopic.value();
if (ptr->fHidden)
m_oHidden = ptr->fHidden;
if(ptr->itab != 0xFFFFFFFF)
m_oLocalSheetId = ptr->itab;
m_oName = ptr->name.value();
m_oPublishToServer = ptr->fPublished;
m_oShortcutKey = std::to_wstring(ptr->chKey);
//m_oStatusBar = ;
m_oVbProcedure = ptr->fOB;
m_oWorkbookParameter = ptr->fWorkbookParam;
m_oXlm = ptr->fFutureFunction;
m_oRef = ref;
}
CDefinedNames::CDefinedNames(OOX::Document *pMain) : WritingElementWithChilds(pMain)
{
}
CDefinedNames::~CDefinedNames()
{
}
void CDefinedNames::fromXML(XmlUtils::CXmlNode& node)
{
}
std::wstring CDefinedNames::toXML() const
{
return L"";
}
void CDefinedNames::toXML(NSStringUtils::CStringBuilder& writer) const
{
if(m_arrItems.empty()) return;
writer.WriteString(L"");
for ( size_t i = 0; i < m_arrItems.size(); ++i)
{
if ( m_arrItems[i] )
{
m_arrItems[i]->toXML(writer);
}
}
writer.WriteString(L"");
}
void CDefinedNames::fromXML(XmlUtils::CXmlLiteReader& oReader)
{
ReadAttributes( oReader );
if ( oReader.IsEmptyNode() )
return;
int nCurDepth = oReader.GetDepth();
while( oReader.ReadNextSiblingNode( nCurDepth ) )
{
std::wstring sName = XmlUtils::GetNameNoNS(oReader.GetName());
if ( L"definedName" == sName || L"NamedRange" == sName)
{
CDefinedName *pDefinedName = new CDefinedName(m_pMainDocument);
m_arrItems.push_back( pDefinedName);
pDefinedName->fromXML(oReader);
}
}
}
std::vector CDefinedNames::AddFutureFunctions(const _UINT32 namesStart)
{
std::vector FunctionsVector;
if(XLS::GlobalWorkbookInfo::arDefineNames_static.empty())
return FunctionsVector;
for(auto i = namesStart; i < XLS::GlobalWorkbookInfo::arDefineNames_static.size(); i++)
{
if(XLS::GlobalWorkbookInfo::arDefineNames_static[i].substr(0, 6) == L"_xlfn.")
{
FunctionsVector.push_back(createFutureFunction(XLS::GlobalWorkbookInfo::arDefineNames_static[i]));
}
else
{
FunctionsVector.push_back(createCustomFunction(XLS::GlobalWorkbookInfo::arDefineNames_static[i]));
}
}
return FunctionsVector;
}
std::vector CDefinedNames::toBin()
{
std::vector objectVector;
for(auto i:m_arrItems)
if(i->m_oName.IsInit() || i->m_oRef.IsInit())
objectVector.push_back(i->toBin());
auto functionsVector = AddFutureFunctions(m_arrItems.size());
if(!functionsVector.empty())
{
std::copy(functionsVector.begin(), functionsVector.end(), std::back_inserter(objectVector));
}
return objectVector;
}
void CDefinedNames::toXLS(XLS::BaseObjectPtr substream)
{
auto globalsSubstream = static_cast(substream.get());
for(auto i:m_arrItems)
if(i->m_oName.IsInit() || i->m_oRef.IsInit())
globalsSubstream->m_arLBL.push_back(i->toXLS());
}
void CDefinedNames::fromBin(std::vector& obj)
{
//ReadAttributes(obj);
if (obj.empty())
return;
for(auto &definedName : obj)
{
CDefinedName *pDefinedName = new CDefinedName(m_pMainDocument);
pDefinedName->fromBin(definedName);
if(pDefinedName->m_oName.IsInit() || pDefinedName->m_oRef.IsInit())
m_arrItems.push_back( pDefinedName);
else
delete pDefinedName;
}
}
EElementType CDefinedNames::getType () const
{
return et_x_DefinedNames;
}
void CDefinedNames::ReadAttributes(XmlUtils::CXmlLiteReader& oReader)
{
}
XLS::BaseObjectPtr CDefinedNames::createFutureFunction(const std::wstring& funcName)
{
auto ptr(new XLSB::Name);
XLS::BaseObjectPtr objectPtr(ptr);
ptr->chKey = 0;
ptr->fHidden = true;
ptr->fFunc = true;
ptr->fOB = false;
ptr->fProc = true;
ptr->fCalcExp = false;
ptr->fGrp = true;
ptr->fPublished = false;
ptr->fBuiltin = false;
ptr->fWorkbookParam = false;
ptr->comment.setSize(0xFFFFFFFF);
ptr->itab = 0xFFFFFFFF;
ptr->fFutureFunction = true;
ptr->name = funcName;
ptr->rgce.parseStringFormula(L"#NAME?", L"");
ptr->description.setSize(0xFFFFFFFF);
ptr->helpTopic.setSize(0xFFFFFFFF);
ptr->unusedstring1.setSize(0xFFFFFFFF);
ptr->unusedstring2.setSize(0xFFFFFFFF);
return objectPtr;
}
XLS::BaseObjectPtr CDefinedNames::createCustomFunction(const std::wstring& funcName)
{
auto ptr(new XLSB::Name);
XLS::BaseObjectPtr objectPtr(ptr);
ptr->chKey = 0;
ptr->fHidden = false;
ptr->fFunc = true;
ptr->fOB = true;
ptr->fProc = true;
ptr->fCalcExp = false;
ptr->fGrp = false;
ptr->fPublished = false;
ptr->fBuiltin = false;
ptr->fWorkbookParam = false;
ptr->itab = 0xFFFFFFFF;
ptr->comment.setSize(0xFFFFFFFF);
ptr->description.setSize(0xFFFFFFFF);
ptr->helpTopic.setSize(0xFFFFFFFF);
ptr->unusedstring1.setSize(0xFFFFFFFF);
ptr->unusedstring2.setSize(0xFFFFFFFF);
ptr->fFutureFunction = false;
ptr->name = funcName;
return objectPtr;
}
} //Spreadsheet
} // namespace OOX