Run, test and debug
Debugging
With the VS Code extension installed, place a breakpoint in a .ras file and run with F5 (or "RAS: Start debugging"). Stepping, inspecting variables and watch expressions, and stopping on exceptions are available.
This is not VBA debugging. The program is converted to JavaScript and run on the runtime bundled with VS Code, and only the Excel operations are performed against Excel (spec §14.10).
- Windows: it operates on real Excel through
ras_com.node, bundled with the extension. If the target workbook (theworkbookinras.toml) is open in Excel it connects to it; if not, it opens it. - Mac, Linux, and Windows without Excel: it loads the workbook with the Excel stand-in and reads and writes its cells. The workbook changed by the program is saved in
build/debug/workbooks/at the end (the original workbook is not modified). On Windows too, you can run this way by settingras.debug.exceltostandIn. Each time execution stops at a breakpoint, the sheet at that moment is shown in a panel, with the cells that changed since last time highlighted. When the run ends, the saved workbook is shown, with the cells changed from the original workbook highlighted (settingras.debug.sheetView). - If, while the program is waiting for Excel to respond, Excel shows a dialog (a run-time error or compile error in handwritten VBA, a
MsgBox, or a confirmation from Excel itself), the content is reported to VS Code and the dialog is brought to the front. With"dialogs": "dismiss"inlaunch.json, it is closed automatically with the button equivalent to Cancel (spec §14.10.6). - What can be started is a function without arguments, top-level statements, and user forms. Functions called by Excel, such as
@onevent handlers of workbooks and sheets, and UDFs, cannot be started. In runs without Excel, handlers for events triggered by the program's own operations (writing cells, switching sheets, saving and so on) are called and can be stepped through. - User forms are shown in a VS Code panel, clicks and input raise the form's events (
Click,Change,Exitand so on), and the handlers can be stepped through. The form's appearance is an approximation in the browser, and key and mouse events do not occur (see spec §14.10.7 for what is supported). - The optimizations of the VBA build (bulk transfer, inlining and so on) are not applied, so timing and speed differ from the real thing. Differences from real Excel, such as the precision of
long/currency/decimaland unsupported VBA standard functions, are summarized in spec §14.10.5.
You can also start a function directly from the CodeLens "▶ Run in Excel | Debug" above it. "Run in Excel" syncs to the workbook and then runs it as a macro in Excel itself (Windows only), and "Debug" starts a debug run with that function. You can also pin the function in launch.json.
{ "type": "ras", "request": "launch", "name": "RAS: rebuildReport", "entry": "Report.rebuildReport" }
The debugger statement
Writing debugger; in the source makes the debug run stop at that line. Nothing is output to VBA (spec §14.10.9).
export function inspect() {
const last = Sheet1.lastRow();
debugger; // stops here
debug.print(last);
}
In the playground, you can debug by placing breakpoints in the .ras files at ras://project/… under "Sources" in the browser's developer tools (F12) (spec §14.10.10).
Details of working in VS Code, such as the launch.json fields, debugging-related settings and test generation, are in the VS Code extension.