@@ -11,7 +11,7 @@ import {Maybe} from '../Maybe'
1111 * Options for querying the sheet mapping.
1212 */
1313export interface SheetMappingQueryOptions {
14- includeNotAdded ?: boolean ,
14+ includePlaceholders ?: boolean ,
1515}
1616
1717/**
@@ -21,12 +21,8 @@ class Sheet {
2121 constructor (
2222 public readonly id : number ,
2323 public displayName : string ,
24- /**
25- * Whether the sheet has been explicitly added to the instance either on initialization or via addSheet method.
26- */
27- public isAdded : boolean = true ,
28- ) {
29- }
24+ public isPlaceholder : boolean = false ,
25+ ) { }
3026
3127 /**
3228 * Returns the canonical (normalized) name of the sheet.
@@ -39,9 +35,9 @@ class Sheet {
3935/**
4036 * Manages the sheets in the instance.
4137 * Can convert between sheet names and ids and vice versa.
42- * Also stores placeholders for sheets that are used in formulas but not yet added. They are marked as isAdded=false .
38+ * Also stores placeholders for sheets that are used in formulas but not yet added. They are marked as isPlaceholder=true .
4339 * Sheetnames thet differ only in case are considered the same. (See: canonicalizeSheetName)
44- */
40+ */
4541export class SheetMapping {
4642 /**
4743 * Prefix for new sheet names if no name is provided by the user
@@ -73,16 +69,14 @@ export class SheetMapping {
7369 }
7470
7571 /**
76- * Returns sheet ID for the given name (case-insensitive). By default excludes not added sheets.
77- *
78- * @returns {Maybe<number> } the sheet ID, or undefined if not found.
72+ * Returns sheet ID for the given name. By default excludes placeholders.
7973 */
8074 public getSheetId ( sheetName : string , options : SheetMappingQueryOptions = { } ) : Maybe < number > {
8175 return this . _getSheetByName ( sheetName , options ) ?. id
8276 }
8377
8478 /**
85- * Returns sheet ID for the given name. Excludes not added sheets .
79+ * Returns sheet ID for the given name. Excludes placeholders .
8680 *
8781 * @throws {NoSheetWithNameError } if the sheet with the given name does not exist.
8882 */
@@ -96,7 +90,7 @@ export class SheetMapping {
9690 }
9791
9892 /**
99- * Returns display name for the given sheet ID. Excludes not added sheets .
93+ * Returns display name for the given sheet ID. Excludes placeholders .
10094 *
10195 * @returns {Maybe<string> } the display name, or undefined if the sheet with the given ID does not exist.
10296 */
@@ -105,7 +99,7 @@ export class SheetMapping {
10599 }
106100
107101 /**
108- * Returns display name for the given sheet ID. Excludes not added sheets .
102+ * Returns display name for the given sheet ID. Excludes placeholders .
109103 *
110104 * @throws {NoSheetWithIdError } if the sheet with the given ID does not exist.
111105 */
@@ -114,60 +108,59 @@ export class SheetMapping {
114108 }
115109
116110 /**
117- * Iterates over all sheet display names. By default excludes not added sheets .
111+ * Iterates over all sheet display names. By default excludes placeholders .
118112 */
119113 public * iterateSheetNames ( options : SheetMappingQueryOptions = { } ) : IterableIterator < string > {
120114 for ( const sheet of this . allSheets . values ( ) ) {
121- if ( options . includeNotAdded || sheet . isAdded ) {
115+ if ( options . includePlaceholders || ! sheet . isPlaceholder ) {
122116 yield sheet . displayName
123117 }
124118 }
125119 }
126120
127121 /**
128- * Returns array of all sheet display names. By default excludes not added sheets .
122+ * Returns array of all sheet display names. By default excludes placeholders .
129123 */
130124 public getSheetNames ( options : SheetMappingQueryOptions = { } ) : string [ ] {
131125 return Array . from ( this . iterateSheetNames ( options ) )
132126 }
133127
134128 /**
135- * Returns total count of sheets. By default excludes not added sheets .
129+ * Returns total count of sheets. By default excludes placeholders .
136130 */
137131 public numberOfSheets ( options : SheetMappingQueryOptions = { } ) : number {
138132 return this . getSheetNames ( options ) . length
139133 }
140134
141135 /**
142- * Checks if sheet with given ID exists. By default excludes not added sheets .
136+ * Checks if sheet with given ID exists. By default excludes placeholders .
143137 */
144138 public hasSheetWithId ( sheetId : number , options : SheetMappingQueryOptions = { } ) : boolean {
145139 return this . _getSheet ( sheetId , options ) !== undefined
146140 }
147141
148142 /**
149- * Checks if sheet with given name exists (case-insensitive). Excludes not added sheets .
143+ * Checks if sheet with given name exists (case-insensitive). Excludes placeholders .
150144 */
151145 public hasSheetWithName ( sheetName : string ) : boolean {
152146 return this . _getSheetByName ( sheetName , { } ) !== undefined
153147 }
154148
155149 /**
156150 * Adds new sheet with optional name and returns its ID.
157- * If called with a reserved sheet name (sheet name of some sheet present in the mapping but not added yet) , adds the reserved sheet.
151+ * If called with a name of placeholder sheet, adds the real sheet.
158152 *
159153 * @throws {SheetNameAlreadyTakenError } if the sheet with the given name already exists.
160- * @returns {number } the ID of the new sheet.
161154 */
162155 public addSheet ( newSheetDisplayName : string = `${ this . sheetNamePrefix } ${ this . lastSheetId + 2 } ` ) : number {
163- const sheetWithConflictingName = this . _getSheetByName ( newSheetDisplayName , { includeNotAdded : true } )
156+ const sheetWithConflictingName = this . _getSheetByName ( newSheetDisplayName , { includePlaceholders : true } )
164157
165158 if ( sheetWithConflictingName ) {
166- if ( sheetWithConflictingName . isAdded ) {
159+ if ( ! sheetWithConflictingName . isPlaceholder ) {
167160 throw new SheetNameAlreadyTakenError ( newSheetDisplayName )
168161 }
169162
170- sheetWithConflictingName . isAdded = true
163+ sheetWithConflictingName . isPlaceholder = false
171164 return sheetWithConflictingName . id
172165 }
173166
@@ -178,49 +171,48 @@ export class SheetMapping {
178171 }
179172
180173 /**
181- * Stores the sheet in the mapping with flag isAdded=false.
182- * If such sheet name is already present in the mapping, does nothing.
183- *
184- * @returns {number } the ID of the reserved sheet.
174+ * Adds a placeholder sheet with the given name if it does not exist yet
185175 */
186- public reserveSheetName ( sheetName : string ) : number {
187- const sheetWithConflictingName = this . _getSheetByName ( sheetName , { includeNotAdded : true } )
176+ public addPlaceholderIfNotExists ( sheetName : string ) : number {
177+ const sheetWithConflictingName = this . _getSheetByName ( sheetName , { includePlaceholders : true } )
188178
189179 if ( sheetWithConflictingName ) {
190180 return sheetWithConflictingName . id
191181 }
192182
193183 this . lastSheetId ++
194- const sheet = new Sheet ( this . lastSheetId , sheetName , false )
184+ const sheet = new Sheet ( this . lastSheetId , sheetName , true )
195185 this . storeSheetInMappings ( sheet )
196186 return sheet . id
197187 }
198188
199189 /**
200- * Removes sheet with given ID. Ignores not added sheets.
201- *
202- * @throws {NoSheetWithIdError } if the sheet with the given ID does not exist or is not added yet.
190+ * Removes sheet with given ID. Ignores placeholders
191+ * @throws {NoSheetWithIdError } if the sheet with the given ID does not exist or is a placeholder
203192 */
204193 public removeSheet ( sheetId : number ) : void {
205- const sheet = this . _getSheetOrThrowError ( sheetId , { } )
194+ const sheet = this . _getSheetOrThrowError ( sheetId , { includePlaceholders : false } )
206195 this . allSheets . delete ( sheetId )
207196 this . mappingFromCanonicalNameToId . delete ( sheet . canonicalName )
208197 }
209198
210- public softRemoveSheet ( sheetId : number ) : void {
199+ /**
200+ * Marks sheet with given ID as a placeholder.
201+ * @throws {NoSheetWithIdError } if the sheet with the given ID does not exist
202+ */
203+ public markSheetAsPlaceholder ( sheetId : number ) : void {
211204 const sheet = this . _getSheetOrThrowError ( sheetId , { } )
212- sheet . isAdded = false
205+ sheet . isPlaceholder = true
213206 }
214207
215208 /**
216209 * Renames sheet.
217- * If called with sheetId of a not added sheet, throws {NoSheetWithIdError}.
210+ * If called with sheetId of a placeholder sheet, throws {NoSheetWithIdError}.
218211 * If newDisplayName is conflicting with an existing sheet, throws {SheetNameAlreadyTakenError}.
219- * If newDisplayName is conflicting with a reserved sheet name (name of a non-added sheet) , throws {SheetNameAlreadyTakenError}.
212+ * If newDisplayName is conflicting with a placeholder sheet name, throws {SheetNameAlreadyTakenError}.
220213 *
221214 * @throws {SheetNameAlreadyTakenError } if the sheet with the given name already exists.
222215 * @throws {NoSheetWithIdError } if the sheet with the given ID does not exist.
223- * @returns {Maybe<string> } the old name, or undefined if the name was not changed.
224216 */
225217 public renameSheet ( sheetId : number , newDisplayName : string ) : { previousDisplayName : Maybe < string > , mergedSheetWith ?: number } {
226218 const sheet = this . _getSheetOrThrowError ( sheetId , { } )
@@ -230,11 +222,11 @@ export class SheetMapping {
230222 return { previousDisplayName : undefined }
231223 }
232224
225+ const sheetWithConflictingName = this . _getSheetByName ( newDisplayName , { includePlaceholders : true } )
233226 let mergedSheetWith : number | undefined = undefined
234- const sheetWithConflictingName = this . _getSheetByName ( newDisplayName , { includeNotAdded : true } )
235227
236228 if ( sheetWithConflictingName !== undefined && sheetWithConflictingName . id !== sheet . id ) {
237- if ( sheetWithConflictingName . isAdded ) {
229+ if ( ! sheetWithConflictingName . isPlaceholder ) {
238230 throw new SheetNameAlreadyTakenError ( newDisplayName )
239231 } else {
240232 this . mappingFromCanonicalNameToId . delete ( sheetWithConflictingName . canonicalName )
@@ -277,7 +269,7 @@ export class SheetMapping {
277269 return undefined
278270 }
279271
280- return ( options . includeNotAdded || retrievedSheet . isAdded ) ? retrievedSheet : undefined
272+ return ( options . includePlaceholders || ! retrievedSheet . isPlaceholder ) ? retrievedSheet : undefined
281273 }
282274
283275 /**
0 commit comments