User forms
Dialogs (dialog)
Build a dialog that just picks a button, a dialog with input fields plus OK / Cancel, and a modeless progress bar that shows how far a task has got, all in code without creating a form design (forms/). The bundled empty form RAS_Dialog is written into the workbook and lays out the controls at run time (only when you use this module).
import { Dialog, choose, Progress } from "dialog";
import * as z from "validation";
// A dialog with arbitrary buttons: the index (from 0) of the button pressed. × and Esc give -1
export function confirmClear(): boolean {
return choose("Clear the input?", ["Clear", "Cancel"], title: "Confirm", cancel: 1) == 0;
}
// A dialog with input fields (the default buttons are OK and Cancel)
export function askExport() {
const d = new Dialog("Export", "Choose the output settings");
const kind = d.select("Format", ["CSV", "TSV"]);
const file = d.file("Output file", dialogType: 2).required("Choose an output file");
const head = d.checkbox("Include headings", true);
const memo = d.text("Memo", "", multiline: true).rule(z.string().max(200));
if (d.show() != 0) return; // 0: OK, 1: Cancel, -1: ×
debug.print(kind.text, file.text, head.checked, memo.text);
}
// A modeless progress bar (shown as soon as it is created; cancelled is set by the stop button or ×)
export function rebuildAll() {
const last = Sheet1.lastRow();
const p = new Progress("Totaling", max: last);
for (let r = 1; r <= last; r++) {
if (p.cancelled) break;
p.value = r;
p.message = `Row ${r} / ${last}`;
}
p.close();
}
For user forms that have a form design, see User forms.
Reference
Class dialog.DialogField
One item of a dialog. Returned by text(), select(), checkbox() and so on of Dialog.
Values can be set before show() and read after show() (after the dialog has closed). Reading or writing while it is shown
(in an onChange handler, for example) reads or writes the control on the screen.
- #
kind: string = "" The kind of item ("text", "password", "select", "combo", "list", "radio", "checkbox", "file", "label").
- #
label: string = "" The label (for
checkbox, the text of the check box).- #
items: string[] = []
The choices (
select,combo,list,radio).- #
multi: boolean = false
Multiple lines (
text) or multiple selection (list).- #
rows: int = 1
Number of rows shown (
list, multi-linetext).- #
schema: ValidationSchema = null
The input rules (set with
rule()/required()).- #
dialogType: int = 3
The settings of the file selection field (
file).- #
title: string = "" - #
tip: string = "" A description shown as a tooltip on the input field.
- #
control: MSForms.Control = null
The control shown (null when not shown).
- #
touched: boolean = false
Whether the input has been committed once (from then on it is validated on every change).
- #
get text(): string set text(v: string)
The text entered. For
select/combo/list/radio, the text of the selected item; forfile, the path.- #
get index(): int set index(v: int)
The number of the selected item (from 0; -1 if none).
select,combo,list,radio.- #
get checked(): boolean set checked(v: boolean)
Whether it is checked (
checkbox).- #
get selected(): string[]
The texts of the selected items (multi-select
list; for other items, an array holdingtextor an empty array).- #
isPicked(i: int): boolean
Whether item
iof thelistis selected.- #
pick(i: int, on: boolean = true): DialogField
Selects item
iof thelist(falsedeselects it). Without multiple selection, other selections are cleared.- #
get enabled(): boolean set enabled(v: boolean)
Whether input is allowed.
- #
get error(): string
The error shown (
""if none).- #
rule(schema: ValidationSchema): DialogField
Sets the input rules (made with
z.string()and so on ofvalidation). Validated by OK and the other buttons.- #
required(message: string = ""): DialogField
Makes the input required (for
checkbox, the box must be checked).- #
addFilter(description: string, extensions: string): DialogField
Adds a file type (
file;addFilter("Excel workbooks", "*.xlsx; *.xlsm")).- #
hint(text: string): DialogField
A description of the input field (tooltip).
Class dialog.Dialog
A dialog built in code. Items are stacked from the top in the order the methods that add them (text, select, checkbox and so on) are called,
with the buttons right-aligned below. show() shows it modally and returns the number of the button pressed.
const d = new Dialog("Export", "Choose the output settings");
const kind = d.select("Format", ["CSV", "TSV"]);
const file = d.file("Destination", dialogType: 2).required("Choose a destination");
const head = d.checkbox("Write headings", true);
if (d.show() != 0) return; // not OK (Cancel, ×)
exportTo(file.text, kind.index, head.checked);
- #
title: string = "" The text of the title bar.
- #
message: string = "" A description shown above the items (not shown when
"").- #
width: double = 300.0
The inner width (points).
- #
defaultButton: int = 0
The number of the button pressed by Enter (-1 for none).
- #
cancelButton: int = 1
The number of the button pressed by Esc (-1 for none; like ×, it returns -1). This button does not validate the input.
- #
fields: DialogField[] = []
The items.
- #
captions: string[] = ["OK", __l("キャンセル", "Cancel")]
The texts of the buttons.
- #
result: int = -1
The button pressed last (the return value of
show()).- #
new Dialog(title: string = "", message: string = "")
- #
text(label: string, value: string = "", multiline: boolean = false, rows: int = 4): DialogField
A one-line input field (with
multiline, several lines,rowslines high).- #
password(label: string, value: string = ""): DialogField
A password field (shown as
*).- #
select(label: string, items: string[], index: int = 0): DialogField
A drop-down to choose one of the choices (no typing).
indexis the number selected at first.- #
combo(label: string, items: string[], value: string = ""): DialogField
A combo box to choose from the choices or type.
- #
list(label: string, items: string[], multi: boolean = false, rows: int = 5): DialogField
A list box (with
multi, multiple selection shown with a check box per item).rowsis the number of visible rows.- #
radio(label: string, items: string[], index: int = 0): DialogField
Choose one with option buttons (radio buttons).
- #
checkbox(caption: string, checked: boolean = false): DialogField
A check box.
- #
file(label: string, path: string = "", dialogType: int = 3, title: string = ""): DialogField
A file or folder selection field (a field and a "..." button).
dialogTypeis the kind ofApplication.FileDialog(1: open, 2: save as, 3: file picker (default), 4: folder picker).- #
folder(label: string, path: string = "", title: string = ""): DialogField
A folder selection field (same as
file(label, path, 4)).- #
label(text: string): DialogField
A text (a description and so on).
Replaces the buttons (default OK and Cancel).
cancelis the number of the button pressed by Esc (-1 for none),enterthe number of the button pressed by Enter.- #
onChange(handler: (f: DialogField) => void): Dialog
A function called every time the value of an item changes (to change
enabledof other items, for example).- #
onSubmit(check: (d: Dialog) => boolean): Dialog
A check called when a button other than cancel is pressed (after the rules are validated). Returning
falsekeeps the dialog open (report the problem withmsgBoxor the like rather than the items'RAS_ShowError).- #
show(): int
Shows the dialog modally, waits until it closes and returns the number of the button pressed (from 0). The × on the title bar and Esc (the cancel button) return -1. Buttons other than cancel validate the items with rules (
rule/required) and do not close the dialog if there are errors.- #
ok(): boolean
Whether it was closed with OK (the first button) (
show() == 0).- #
close(result: int = -1): void
Closes it if it is shown (
show()returnsresult).- #
rasCloseBox(): void
Function dialog.choose
function choose(message: string, buttons: string[], title: string = "", cancel: int = -1, enter: int = 0): int
Shows a dialog with buttons and returns the number of the button pressed (from 0). × and Esc give -1.
cancel is the number of the button pressed by Esc (-1: Esc does nothing), enter the number of the button pressed by Enter.
switch (choose("Save the changes?", ["Save", "Save as", "Cancel"], cancel: 2)) {
case 0: save(); break;
case 1: saveAs(); break;
default: return; // Cancel, × (-1)
}
Class dialog.Progress
A progress bar shown modelessly. It is shown as soon as it is created; advance value while the work goes on.
When the "Cancel" button (cancelable) or × is clicked, cancelled becomes true. Close it with close().
const p = new Progress("Totaling", max: last);
for (let r = 1; r <= last; r++) {
if (p.cancelled) break;
p.value = r;
p.message = `${r} / ${last} rows`;
}
p.close();
- #
new Progress(caption: string = "", max: double = 100.0, title: string = "", cancelable: boolean = true, width: double = 300.0)
- #
get value(): double set value(v: double)
The current value (from
mintomax).- #
get min(): double set min(v: double)
The minimum value (default 0).
- #
get max(): double set max(v: double)
The maximum value (default 100).
- #
get caption(): string set caption(v: string)
The text shown above the bar.
- #
get message(): string set message(v: string)
The text shown below the bar (the item being processed and so on).
- #
get cancelled(): boolean
Whether the cancel button or × was clicked.
- #
step(by: double = 1.0): void
Advances the value by
by.- #
get progressBar(): ProgressBar
The bar part (to change its colors and so on).
- #
close(): void
Closes it (calling this more than once is fine).
- #
rasCloseBox(): void