To add a sheet to websheet, you need to obtain the workbook object. With the workbook object, you can call the WorkSheetAdd() method. If no parameters are provided, the new sheet will be named "sheet" followed by the sheet count. Alternatively, you can call WorkSheetAdd('sheetname') with a parameter to specify the name. Reference code:
let wsheet = new websheet('yourID', yourElement);
/**
* Step 1: Get workbook
*/
let workbook = wsheet.Workbook();
/**
* Step 2: Add sheet using workbook
*/
let newSheet = workbook.WorkSheetAdd();
let newTestSheet = workbook.WorkSheetAdd('newTestSheet');
/**
* Step 3: Rebuild and redraw sheet
*/
wsheet.BuildSheet();
wsheet.Draw();
If the new sheet name conflicts with an existing one, no action will be taken and null will be returned.
To activate or display a specific sheet, call the SetActiveSheet('sheetname') method on the workbook object. Reference code:
workbook.SetActiveSheet(newSheet.name);
To delete a sheet, call the DelWorkSheet('sheetname') method on the workbook object. Example code to delete 'sheet1':
workbook.DelWorkSheet('sheet1');
If attempting to delete the last remaining sheet being displayed, no action will be taken.
Use GetWorkSheet() method to get all sheets. The following code prints all sheet names:
worksheets = workbook.GetWorkSheet();
for (let n = 0; n < worksheets.length; n++) {
const worksheet = worksheets[n];
console.log(worksheet.name);
}
In the following example, we add two sheets (one with default name "sheet2" and another named "newTestSheet"), activate sheet2, print the sheet list (showing 3 sheets), then delete sheet1 and print the sheet list again (showing 2 sheets):
let yourElement = document.getElementById("yourElement");
let wsheet = new websheet('HTML', yourElement);
/**
* Step 1: Get workbook
*/
let workbook = wsheet.Workbook();
/**
* Step 2: Add sheets
*/
let newSheet = workbook.WorkSheetAdd();
let newTestSheet = workbook.WorkSheetAdd('newTestSheet');
if (newTestSheet != null && newSheet != null) {
newTestSheet.SetCellValue(1, 1, 'this is new TestSheet');
newTestSheet.setColWidth(1, 160);
newSheet.SetCellValue(1, 1, 'this is sheet2');;
newSheet.setColWidth(1, 160);
workbook.SetActiveSheet(newSheet.name);
let worksheets = workbook.GetWorkSheet();
for (let n = 0; n < worksheets.length; n++) {
const worksheet = worksheets[n];
console.log(worksheet.name);
}
workbook.DelWorkSheet('sheet1');
worksheets = workbook.GetWorkSheet();
for (let n = 0; n < worksheets.length; n++) {
const worksheet = worksheets[n];
console.log(worksheet.name);
}
}
/**
* Step 3: Rebuild and redraw sheet
*/
wsheet.BuildSheet();
wsheet.Draw();
Log output:
Result screenshot: