NanoXLSX.Core 3.1.0
Loading...
Searching...
No Matches
StringKeyedCellView.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2026
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System;
9using System.Collections;
10using System.Collections.Generic;
11
12namespace NanoXLSX.Internal
13{
20 internal sealed class StringKeyedCellView : IReadOnlyDictionary<string, Cell>
21 {
22 private readonly Dictionary<CellKey, Cell> store;
23
24 internal StringKeyedCellView(Dictionary<CellKey, Cell> store)
25 {
26 this.store = store;
27 }
28
30 public Cell this[string key]
31 {
32 get
33 {
34 Cell.ResolveCellCoordinate(key, out int col, out int row);
35 return store[new CellKey(col, row)];
36 }
37 }
38
40 public int Count
41 {
42 get { return store.Count; }
43 }
44
46 public IEnumerable<string> Keys
47 {
48 get
49 {
50 foreach (CellKey k in store.Keys)
51 {
52 yield return Cell.ResolveCellAddress(k.Column, k.Row);
53 }
54 }
55 }
56
58 public IEnumerable<Cell> Values
59 {
60 get { return store.Values; }
61 }
62
64 public bool ContainsKey(string key)
65 {
66 if (string.IsNullOrEmpty(key))
67 {
68 return false;
69 }
70 try
71 {
72 Cell.ResolveCellCoordinate(key, out int col, out int row);
73 return store.ContainsKey(new CellKey(col, row));
74 }
75 catch (Exception)
76 {
77 return false;
78 }
79 }
80
82 public bool TryGetValue(string key, out Cell value)
83 {
84 if (string.IsNullOrEmpty(key))
85 {
86 value = null;
87 return false;
88 }
89 try
90 {
91 Cell.ResolveCellCoordinate(key, out int col, out int row);
92 return store.TryGetValue(new CellKey(col, row), out value);
93 }
94 catch (Exception)
95 {
96 value = null;
97 return false;
98 }
99 }
100
102 public IEnumerator<KeyValuePair<string, Cell>> GetEnumerator()
103 {
104 foreach (KeyValuePair<CellKey, Cell> kv in store)
105 {
106 yield return new KeyValuePair<string, Cell>(
107 Cell.ResolveCellAddress(kv.Key.Column, kv.Key.Row),
108 kv.Value);
109 }
110 }
111
113 IEnumerator IEnumerable.GetEnumerator()
114 {
115 return GetEnumerator();
116 }
117 }
118}