
! script name: datatable.d/exporthtml
! topic: export a table of data in html format

!A
!A NAME:
!A
!A 	Export HTML
!A
!A DESCRIPTION:
!A
!A 	Export a table of data, in html format. The .html file resides in the
!A	current user directory.
!A
!B INSTRUCTIONS:
!B
!B 	The root directory is the current user directory
!B
!# see also - htdocs/help.d/datatables.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: export html table")

DIM Answer$[256]
DIM A$[256]
DIM B$[256]
DIM Filename$[256],File2$[256]
DIM Destination$[256]
DIM Data$[4096]
DIM Filepath$[256]
DIM Table_name$[256]
DIM Table_title$[256]
DIM Table_file$[256]
DIM Title$[256]
DIM Sdevice$[256]
DIM Drive_letter$[32]

DEG

System_id= OSystemId

IF ARGC>1 THEN
	Answer$=ARGV(1)
END IF
IF ARGC>2 THEN
	Table_name$= TRIM$(ARGV(2))
ELSE
	Table_name$= ""
END IF
Delete_table=0
IF ARGC>3 THEN
	Answer$=ARGV(3)
	Delete_table= VAL(Answer$)
END IF
IF ARGC>4 THEN
	Table_title$= TRIM$(ARGV(4))
ELSE
	Table_title$= ""
END IF
IF ARGC>5 THEN
	Answer$= ARGV(5)
	Table_file$= TRIM$(Answer$)
ELSE
	Table_file$= ""
END IF

Sdevice$= System$("MASS STORAGE IS")
N1=POS(Sdevice$,":")
IF N1>1 AND N1<LEN(Sdevice$) THEN
	Drive_letter$=Sdevice$[1,N1-1]
	Sdevice$=Sdevice$[N1+1]
END IF

Backround_color= 6
Table_color= 11
Caption_color= 1
! forces the default values for cell spacing
Cell_spacing= -1
Cell_padding= -1

Filename$=""

Ask_file:!

IF NOT LEN(Table_file$) THEN
	Stat=SetInputFormat("a99","")
	Stat=GetValue("EXPORT FILE NAME",File2$)
	IF ExitScript(Destination$) THEN Leave
ELSE
	File2$= Table_file$
END IF
IF (NOT POS(File2$,".html")) AND (NOT POS(File2$,".HTML")) THEN
	File2$= File2$&".html"
END IF
Filename$=FileNameString(Sdevice$,File2$)

F_type= DoesFileExist(Filename$)
SELECT F_type
CASE 1
IF NOT LEN(Table_file$) THEN
        Stat=SetInputFormat("a1",",Y,N,")
        Stat=GetValue("FILE EXISTS: OVERWRITE  Yes or No",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Answer$= "N" THEN
                GOTO Ask_file
        END IF
END IF
CASE 2
        Stat=SetInputFormat("a1",",P,")
        Stat=GetValue("COPY NAME IS A DIRECTORY: Proceed ",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        GOTO Ask_file
END SELECT


Pick_table:!

IF LEN(Table_name$) THEN
	Table1= FindNamedEntity(Table_name$)
	IF NOT ValidGeometry(Table1) THEN Leave
ELSE
	stat= SetInputFormat("n4","")
	Table1=PickEntity("PICK TABLE")
	IF (ExitScript(Destination$)) THEN Leave
	IF NOT  ValidGeometry(Table1) THEN Pick_table
END IF

IF LEN(Table_title$) THEN
	Title$= Table_title$
ELSE
	Stat=SetInputFormat("a99","")
	Stat=GetValue("TABLE TITLE  None ",Title$)
	IF ExitScript(Destination$) THEN Leave
	Title$= TRIM$(Title$)
	SELECT Title$
	CASE "None","NONE","N","n"
		Title$=""
	END SELECT
END IF


Stat= ExportHtmlTable(Table1,Filename$,Title$,Backround_color,Table_color,Caption_color,Cell_spacing,Cell_padding)

	SELECT System_id
	CASE 1
		Filepath$= " file:"&Filename$&" &"
	CASE ELSE
! 		Filepath$= " 'file:///"&Drive_letter$&"|"&Filename$&"' &"
		Filepath$= "file:///"&Drive_letter$&"|"&Filename$
	END SELECT
	A$ = UniformPathName(Filename$)
	Stat = MessageFileBox(A$,"HTML_VIEW","")

IF Delete_table THEN Stat= DeleteEntity(Table1)

GOTO Leave

Leave:!
Stat=SetInputFormat("n4","")
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
