RAS Playground 日本語 GitHub

User forms

User forms

A form's design lives in forms/<name>.form.json (FormSpec), and the code for event handling and so on is written in RAS in forms/<name>.ras (spec §14.11). You can edit them in the designer of the VS Code extension and of the playground, and ras pack / ras sync write them into the workbook.

ras form new frmInput          # creates forms/frmInput.form.json and forms/frmInput.ras
ras form pull                  # extracts the user forms in the workbook into forms/ (code is converted to RAS)

Code-behind

// forms/frmInput.ras — this is the form; controls are this.<name>
@on("btnOK.Click")
function ok() {
    if (this.txtName.text == "") {
        msgBox("Please enter a name");
        return;
    }
    this.close(this.txtName.text);
}

@on("txtName.Exit")
function leave(cancel: MSForms.ReturnBoolean) {
    cancel.value = this.txtName.text.length > 20;
}

Arguments and return value

If you write function constructor(...), you can create an instance of the form with arguments, as in new frmInput("Yamada"). showModal() shows the form modally and returns the value passed to this.close(value) in the code-behind (for the × button or a close() without a value, it returns the default value of the type. Spec §14.11.9).

// src/ask.ras
export function askName(): string {
    return new frmInput().showModal();
}

Design

For dialogs built only in code, without a form design, see Dialogs.

Importing existing forms

Existing forms made in the VBE can be extracted into forms/ with ras form pull (design, code and images). The code is converted from VBA to RAS and written to forms/<name>.ras. Lines that could not be converted (GoTo, ReDim and so on) remain as // TODO(VBA): comments and are shown as warnings. Properties that FormSpec cannot express are also shown as warnings, and revert to their default values when written back from forms/. After extracting, replace the workbook's forms with the contents of forms/ using ras pack --overwrite.

A VBA code-behind forms/<name>.bas from earlier versions can still be used as it is if there is no .ras. Re-importing with ras form pull --force converts it to a .ras.

Applying to the workbook

ras pack / ras sync write the forms into the workbook (Syncing with and importing from Excel). Even while the workbook is open in Excel, ras sync (and ras watch, and the sync on save in VS Code) apply the forms. A form whose design changed is imported again; a form whose code alone changed has only its code rewritten.

Running

You can run a form directly from the CodeLens "▶ Run in Excel | Debug" at the top of forms/<name>.ras (in the designer, the buttons in the title bar). "Run in Excel" syncs to the workbook and then shows the form in Excel itself (Windows only); "Debug" shows the form in a VS Code panel and debugs the code-behind with breakpoints (Debugging). In the playground, a form shown during a run appears on top of the page. In a debug run, 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 designer's operations (toolbox, properties, shortcuts) are described in the VS Code extension.