# excel tabellier programm V2
#
#	F3	startet Dialog und Tabellierung
#
#	Werfen Sie einen Blick in die get_range() Funktion, sie eignet
#	sich gut als Bibliotheksfunktion.
#
#	Das Programm sollte sich ohne groen Aufwand auf andere
#	Tabellenkalkulationen portieren lassen.
#	Setzen Sie dazu remoteDDE (BEGIN Section) auf den richtigen Wert
#	und ndern Sie die Belegung der shift+F2 Taste.
#

FUNCTION get_range(range) {
	local	file, top, bottom, left, right, x

	if (index(range, "!") == 0)
		return (1);
	else
		file = substr(range, 1, rstart-1);

	x = substr(range, rstart+1);			# ZaSb:ZcSd
	top = substr(x, 2) + 0;
	if (index(x, "S") == 0)
		return (1);
	else
		left = substr(x, rstart+1) + 0

	if (index(x, ":") == 0) {
		bottom = top;
		right = left;
		return (0);
		}

	x = substr(x, rstart+1) 			# ZcSd
	bottom = substr(x, 2) + 0
	if (index(x, "S") == 0)
		return (1);
	else
		right = substr(x, rstart+1) + 0

	return (0)
	}



BEGIN {
	ctrl(2, "Excel Tabellierer V2", 1, 4, 113)
	close("") |: status()
	DDEservice = "xltab"

	remoteDDE  = "excel|"
	remoteSYS  = "excel|system"
	SYS	   = remoteSYS "!"
	SEL	   = SYS "selection"
	MACROLIB   = "xltab.xlm"
	MACROFILE  = shellpath "\\" MACROLIB

# excel und makrodatei laden
#
	if (getmod("excel") == 0) {
		status("excel wird gestartet ...")
		exec("excel " MACROFILE, 1)
		}
	else {
		if (index((SYS "topics" | get) |: strlwr(), "\t" MACROFILE "\t") == 0) {
			status (file " wird geffnet ...")
			cmd = "[open(\"" & MACROFILE & "\")]"
			cmd | remoteSYS
			}
		}
	}


#
# excel dialog starten
#

/F3 "dialog"/ {
	ctrl(1)
	"[App.Activate(,0)]"		  | remoteSYS
	"[Run(\"" MACROLIB "!dialog1\")]" | remoteSYS
	}


#
# F3 dialog ruft zurck
#

/? xyTAB data/ {
	DDEoutput = "Hello World!"
	}

/| xyTAB data/ {
	beep();
	n = split(DDEinput, data, "\r\n");

	variabel = data[0]
	ergebnis = data[1]
	eingabe  = data[2]
	ausgabe  = data[3]

	enter("xyTAB")
	}

/user xyTAB/ {
	thistext = WINtext;
	ctrl("tabulating ...");

	get_range(ausgabe)
	outF = get_range["file"] "!"
	outZ = get_range["top"];
	outS = get_range["left"];

	get_range(eingabe)
	varF = get_range["file"] "!"	# Achtung !
	varZ = get_range["top"] + 0;	#    die Werte sind Strings!
	varS = get_range["left"];	#
	end  = get_range["bottom"] + 0;

	for (; varZ <= end; outZ++, varZ++) {
		(remoteDDE varF "Z" varZ "S" varS) | get x
		status(x);
		y = x | remoteDDE variabel : ergebnis | (remoteDDE outF "Z" outZ "S" outS);
		}

	ctrl(thistext);
	}
