A workbook is a container for multiple worksheets. It can be saved to and opened from a particular directory. The extension of a workbook must be swb.
The following command creates a workbook and shows it to the user as shown in the following image.
It should be noted that the newly created workbook, namely "Workbook 2", is owned by Lua since it was created using a command.
Ownership: There are two types of ownership for a workbook and the ownership is rather important when closing a workbook:
In some cases, the workbook can be shown after processing data (reading/writing). In order to create a workbook without showing it to the user:
When closing a workbook, the ownership matters. Consider the two workbooks shown in Fig 1, where "Workbook 1" is owned by System and "Workbook 2" is owned by Lua. If we were to execute the following code, where wb1 is a system owned workbook and the code executes its close member function, an error will be thrown:
ERROR: The workbook is not owned by a script. You can only close it via close button.
It should be noted that System owned workbooks can only be closed via the close button, as long as there is more than 1 System owned workbook. Furthermore, regardless of the number, Lua owned workbooks can always be closed through close button.
add(name="", row=1000, col=100) → Worksheet
Adds a worksheet to the workbook.
If argument name is not provided, then the name of the worksheet is assigned automatically. Note that worksheets in a workbook must have a unique name.
Closes the workbook. If the workbook has any changes on it, then a Save prompt will be shown to be able to save the changes.
It should be noted that once a workbook is closed, there will be no access to the variable as type Workbook. The memory associated with it will be destroyed and its metatable will be stripped off. Please also read the section on closing a workbook.
count() → integer
Returns the number of worksheets in the workbook.
cur() → Worksheet
Returns the active worksheet. It is the worksheet currently opened by either the user or the script.
where type arg can be string, integer or Worksheet.
After manually adding worksheets and running the following commands the workbook looks like the one shown in figure:
id() → integer
Every workbook, regardless created by a Lua command or by using the new button, has a unique ID associated with the workbook. The function returns the ID of the workbook, which is an integer number.
Opens the file with .swb extension from the specified path. The path variable, if provided, is of type string and it might be either a fullpath or a path relative to the executable file, such as path="myfile.sswb". It might be a better approach to create a Workbook without initially showing it to the user and then opening the file at the specified path. After all these processed, then it might be shown to the user.
>>wb2:open("C:/Users/gbingol/Desktop/demo_data.sswb") -- Please note the directory separator is / and not \.
Sometimes, typing the full path can be cumbersome or you might want user to choose the path. The following is doable too:
It is seen that the user is prompted to select a Workbook file.
Saves the workbook. If the argument path, which must be of type string, is not provided, then a Save dialog is shown to the user. If the file was previously saved to or opened from a sswb file, the argument path must not be provided as this will throw an error.
After the command wb:save(), the Save dialog will be shown to the user. Once the user saves the file, the next line, wb:close(), will be executed.
Shows the workbook, changes its state from hidden to visible.
title() → string
Returns the title of the Workbook. If the document is not opened from a directory then the title is the one that was automatically assigned, otherwise returns the full path.