<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace FastExcel
{
    partial class FastExcel
    {
        /// <summary>
        /// Dictionary of defined names
        /// </summary>
        internal IReadOnlyDictionary<string, DefinedName> DefinedNames { get; private set; }

        private void LoadDefinedNames()
        {
            XDocument document;
            try
            {
                document = XDocument.Load(Archive.GetEntry("xl/workbook.xml").Open());
            }
            catch (Exception exception)
            {
                throw new DefinedNameLoadException("Unable to open stream to read internal workbook.xml file", exception);
            }
            if (document == null)
            {
                throw new DefinedNameLoadException("Unable to load workbook.xml from open stream. Probable corrupt file.");
            }

            var definedNames = new Dictionary<string, DefinedName>();

            foreach (var e in (from d2 in document.Descendants().Where(dx => dx.Name.LocalName == "definedNames").Descendants()
                               select d2))
            {
                var currentDefinedName = new DefinedName(e);
                definedNames.Add(currentDefinedName.Key, currentDefinedName);
            }
            DefinedNames = definedNames;
        }

        /// <summary>
        /// Retrieves ranges of cells by their defined name
        /// </summary>
        /// <param name="definedName">Defined Name</param>
        /// <param name="worksheetIndex">If scoped to a sheet, the worksheetIndex</param>
        /// <returns>List of cells encapsulated in another list representing seperate ranges</returns>
        public IEnumerable<IEnumerable<Cell>> GetCellRangesByDefinedName(string definedName, int? worksheetIndex = null)
        {
            List<List<Cell>> result = new List<List<Cell>>();

            string key = (!worksheetIndex.HasValue ? definedName : definedName + ":" + worksheetIndex);

            if (!DefinedNames.ContainsKey(key))
                return result;

            string[] references = DefinedNames[key].Reference.Split(',');
<commit_msg>Added load on demand for DefinedNames<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace FastExcel
{
    partial class FastExcel
    {
        /// <summary>
        /// Dictionary of defined names
        /// </summary>
        internal IReadOnlyDictionary<string, DefinedName> DefinedNames
        {
            get
            {
                if (_definedNames == null)
                    LoadDefinedNames();
                return _definedNames;
            }
        }
        private Dictionary<string, DefinedName> _definedNames;

        private void LoadDefinedNames()
        {
            XDocument document;
            try
            {
                document = XDocument.Load(Archive.GetEntry("xl/workbook.xml").Open());
            }
            catch (Exception exception)
            {
                throw new DefinedNameLoadException("Unable to open stream to read internal workbook.xml file", exception);
            }
            if (document == null)
            {
                throw new DefinedNameLoadException("Unable to load workbook.xml from open stream. Probable corrupt file.");
            }

            _definedNames = new Dictionary<string, DefinedName>();

            foreach (var e in (from d2 in document.Descendants().Where(dx => dx.Name.LocalName == "definedNames").Descendants()
                               select d2))
            {
                var currentDefinedName = new DefinedName(e);
                _definedNames.Add(currentDefinedName.Key, currentDefinedName);
            }

        }

        /// <summary>
        /// Retrieves ranges of cells by their defined name
        /// </summary>
        /// <param name="definedName">Defined Name</param>
        /// <param name="worksheetIndex">If scoped to a sheet, the worksheetIndex</param>
        /// <returns>List of cells encapsulated in another list representing seperate ranges</returns>
        public IEnumerable<IEnumerable<Cell>> GetCellRangesByDefinedName(string definedName, int? worksheetIndex = null)
        {
            List<List<Cell>> result = new List<List<Cell>>();

            string key = (!worksheetIndex.HasValue ? definedName : definedName + ":" + worksheetIndex);

            if (!DefinedNames.ContainsKey(key))
                return result;

            string[] references = DefinedNames[key].Reference.Split(',');

{'fraction_non_alphanumeric': 0.09913793103448276, 'fraction_numerical': 0.0008620689655172414, 'mean_word_length': 1.94106463878327, 'pattern_counts': {'":': 2, '<': 40, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1863722', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1113, 'n_words': 298}