feat: override constant defaults;

This commit is contained in:
Asad Karimov 2026-01-12 18:19:57 -05:00
parent 19abeee875
commit 4f440c9ef5
7 changed files with 79 additions and 25 deletions

@ -2,6 +2,10 @@
All notable changes to the "sheetjs-demo" extension will be documented in this file.
## [0.1.1]
- Added control over #rows x #columns to show
- Added control over #worbooks and #worksheets to cache
## [0.1.0]
- Added commands to disable/enable viewer for specific file extensions

@ -77,6 +77,14 @@ You can easily disable the SheetJS viewer for specific file extensions:
**Built-in VSCode**: Right-click any file and select "Open With..." to choose between SheetJS Viewer and other editors.
### Override Constants (Available Settings)
- `sheetjs.maxRows` - Rows displayed per page (default: **1000**)
- `sheetjs.maxColumns` - Columns displayed (default: **100**)
- `sheetjs.maxCachedWorkbooks` - Workbooks kept in memory (default: **10**)
- `sheetjs.maxCachedSheets` - Sheet pages cached (default: **255**)
Changes take effect when opening a new spreadsheet file (existing open files need to be reopened)
## Getting Started
Want to integrate SheetJS in your own VSCode extension? Check out our [detailed tutorial](https://docs.sheetjs.com/docs/) to learn how to implement these capabilities in your projects.

@ -5,7 +5,7 @@
"author": "Asadbek Karimov <contact@asadk.dev>",
"publisher": "asadbek",
"icon": "img/logo.png",
"version": "0.1.0",
"version": "0.1.1",
"license": "Apache-2.0",
"bugs": {
"url": "https://git.sheetjs.com/asadbek064/sheetjs-vscode-extension/issues"
@ -30,6 +30,35 @@
"activationEvents": [],
"main": "./dist/extension.js",
"contributes": {
"configuration": {
"title": "SheetJS Viewer",
"properties": {
"sheetjs.maxRows": {
"type": "number",
"default": 1000,
"minimum": 1,
"description": "Maximum number of rows to display per page"
},
"sheetjs.maxColumns": {
"type": "number",
"default": 100,
"minimum": 1,
"description": "Maximum number of columns to display"
},
"sheetjs.maxCachedWorkbooks": {
"type": "number",
"default": 10,
"minimum": 1,
"description": "Maximum number of workbooks to keep in cache"
},
"sheetjs.maxCachedSheets": {
"type": "number",
"default": 255,
"minimum": 1,
"description": "Maximum number of rendered sheets to keep in cache"
}
}
},
"commands": [
{
"command": "sheetjs.disableForExtension",

@ -23,7 +23,7 @@ export class LRUCache<K, V> {
if (!this.cache.has(key)) {
return undefined;
}
// update access time
this.accessTimes.set(key, Date.now());
return this.cache.get(key);
@ -39,7 +39,7 @@ export class LRUCache<K, V> {
if (!this.cache.has(key) && this.cache.size >= this.maxSize) {
this.evictLeastRecentlyUsed();
}
// store the value and update access time
this.cache.set(key, value);
this.accessTimes.set(key, Date.now());
@ -105,18 +105,18 @@ export class LRUCache<K, V> {
*/
private evictLeastRecentlyUsed(): void {
if (this.cache.size === 0) { return; }
// find the oldest entry
let oldestKey: K | undefined;
let oldestTime = Infinity;
for (const [key, time] of this.accessTimes.entries()) {
if (time < oldestTime) {
oldestTime = time;
oldestKey = key;
}
}
// remove the oldest entry
if (oldestKey !== undefined) {
console.log(`Evicting least recently used cache entry: ${String(oldestKey)}`);

@ -8,7 +8,7 @@ import { LRUCache } from './lruCache';
export class WorkbookCache {
private workbookCache: LRUCache<string, XLSX.WorkBook>;
private sheetCache: LRUCache<string, string>;
/**
* create a new workbook cache
* @param maxWorkbooks Maximum number of workbooks to cache
@ -18,7 +18,7 @@ export class WorkbookCache {
this.workbookCache = new LRUCache<string, XLSX.WorkBook>(maxWorkbooks);
this.sheetCache = new LRUCache<string, string>(maxSheets);
}
/**
* generate a cache key for a document
* @param uri Document URI
@ -27,7 +27,7 @@ export class WorkbookCache {
generateKey(uri: vscode.Uri, mtime: number): string {
return `${uri.toString()}-${mtime}`;
}
/**
* get a workbook from the cache
* @param key Cache key
@ -35,7 +35,7 @@ export class WorkbookCache {
getWorkbook(key: string): XLSX.WorkBook | undefined {
return this.workbookCache.get(key);
}
/**
* store a workbook in the cache
* @param key Cache key
@ -44,7 +44,7 @@ export class WorkbookCache {
setWorkbook(key: string, workbook: XLSX.WorkBook): void {
this.workbookCache.set(key, workbook);
}
/**
* check if a workbook exists in the cache
* @param key Cache key
@ -52,7 +52,7 @@ export class WorkbookCache {
hasWorkbook(key: string): boolean {
return this.workbookCache.has(key);
}
/**
* get sheet HTML from the cache
* @param key Sheet cache key
@ -60,7 +60,7 @@ export class WorkbookCache {
getSheet(key: string): string | undefined {
return this.sheetCache.get(key);
}
/**
* store sheet HTML in the cache
* @param key Sheet cache key
@ -69,7 +69,7 @@ export class WorkbookCache {
setSheet(key: string, html: string): void {
this.sheetCache.set(key, html);
}
/**
* Check if sheet HTML exists in the cache
* @param key Sheet cache key
@ -77,7 +77,7 @@ export class WorkbookCache {
hasSheet(key: string): boolean {
return this.sheetCache.has(key);
}
/**
* generate a sheet cache key
* @param baseKey Base workbook key
@ -87,17 +87,17 @@ export class WorkbookCache {
generateSheetKey(baseKey: string, sheetName: string, page: number): string {
return `${baseKey}-${sheetName}-page-${page}`;
}
/**
* clear all caches for a specific URI
* @param uriString URI string prefix to clear
*/
clearCachesForUri(uriString: string): void {
console.log(`Clearing caches for ${uriString}`);
// clear workbook cache entries for this URI
this.workbookCache.deleteByPredicate(key => key.startsWith(uriString));
// clear sheet cache entries for this URI
this.sheetCache.deleteByPredicate(key => key.startsWith(uriString));
}

@ -19,7 +19,10 @@ export class ExcelEditorProvider implements vscode.CustomReadonlyEditorProvider<
}
constructor(private readonly context: vscode.ExtensionContext) {
this.cache = new WorkbookCache(20, 255);
const config = vscode.workspace.getConfiguration('sheetjs');
const maxWorkbooks = config.get<number>('maxCachedWorkbooks', 10);
const maxSheets = config.get<number>('maxCachedSheets', 255);
this.cache = new WorkbookCache(maxWorkbooks, maxSheets);
}
async openCustomDocument(uri: vscode.Uri): Promise<ExcelDocument> {
@ -213,8 +216,13 @@ export class ExcelEditorProvider implements vscode.CustomReadonlyEditorProvider<
</div>
`;
// get config values
const config = vscode.workspace.getConfiguration('sheetjs');
const maxRows = config.get<number>('maxRows', 1000);
const maxColumns = config.get<number>('maxColumns', 100);
// setup up the HTML with JS to handle sheet switching
webviewPanel.webview.html = getExcelViewerHtml(sheetNames, sheetSelector);
webviewPanel.webview.html = getExcelViewerHtml(sheetNames, sheetSelector, maxRows, maxColumns);
// handle messages from the webview
this.setupMessageHandlers(document, webviewPanel, workbook, cacheKey);

@ -181,12 +181,12 @@ const loadingScript = `
});
`;
function getExcelViewerScript(sheetNames: string[]) {
function getExcelViewerScript(sheetNames: string[], maxRows: number, maxColumns: number) {
return `
// config
const config = {
rowsPerPage: 500,
maxColumns: 100
rowsPerPage: ${maxRows},
maxColumns: ${maxColumns}
};
// Store sheet names and state
@ -405,7 +405,12 @@ export function getErrorViewHtml(error: any): string {
}
// HTML for Excel viewer
export function getExcelViewerHtml(sheetNames: string[], sheetSelector: string): string {
export function getExcelViewerHtml(
sheetNames: string[],
sheetSelector: string,
maxRows: number = 1000,
maxColumns: number = 100
): string {
const body = `
<div class="container">
<div class="sheet-selector">
@ -437,5 +442,5 @@ export function getExcelViewerHtml(sheetNames: string[], sheetSelector: string):
</div>
`;
return createHtmlDocument(excelViewerStyles, body, getExcelViewerScript(sheetNames));
return createHtmlDocument(excelViewerStyles, body, getExcelViewerScript(sheetNames, maxRows, maxColumns));
}