// Filename:	WKS.OGS
// Creation:	by Joe Przechocki, 8/9/96
// Purpose:	Worksheet Analysis Menu scripts common to Context Menus and Standard tool
//**********************************************
// Modification Log:
//		RKM 11/09/96 Added [GoToRow][AddNewCol]
//		RKM 11/11/96 added [Worksheet][Column][ColorPalette]
//		RKM 11/12/96 added matrix sections
//		RKM 11/20/96 removed [Worksheet] [Column] [ColorPalette] sections, 
//		put the script in the string for ID
//		RKM 12/04/96 added 6 sort sections for Context Sensitive menus
//		RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG
//		SDB 1/7/99   v6.0148 REPORT_TO_OUTPUTLOG
//		CPY 6/30/00 v6.0295 t7988 ADD_SUM_TO_ROW_STAT
//		CPY v6.0303 t8198 7/13/00 LARGE_ROW_NUMBER_LOW_SIG_DIGITS
//		GJL 5/28/02 v7.0313 QA70-1868 ADD_DATA_FROM_PAGE_LABEL
//		GJL 5/28/02 v7.0313 QA70-1889 CHECK_FREQ_COUNT_INPUT_ERROR
//		GJL 11/26/02 v7.0445 QA70-3136 FREQ_COUNT_INCLUDE_TRUE_MAX
//		GJL 6/06/02 v7.0316 QA70-2003 ADD_DDK_DIRECT_MATRIX_CONVERT_DB
//		ML 4/20/2003 QA70-4155 CLASS_SECTION_NAME_WITH_ARG_FOR_70
//		LAS 7/17/03 QA-3648 Modified [ConvSparse]
//	CPY 8/8/06 QA70-8626 GOTO_ROW_CHANGED_TO_GOTO
//	CPY 9/18/06 OC_BASED_WKS_SCRIPT	I also remove all old LT based analysis scripts
//	SY 2006-09-27 v8.0489 QA70-7215 COL_MOVE_CMD_USING_XF
//	CPY 2006-12-09 NORMALIZE_USING_XF
//	Max 3/29/07 FREQUENCYCOUNT_USING_XF
/// Max 10/25/07 QA70-10581 LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
/// Max 11/19/08 QA70-12615 v8.0975b WKS_NORMALIZE_SHOULD_WORK_ON_NON_Y_TYPE_DATA
/// Sim 06-02-2009 QA80-4584 EXTRACT_WKS_SUPPORT_AUTO_UPDATE
/// Max 9/02/09 v8.1088-1 QA70-13673 REPLACE_vnormalize_WITH_rnormalize
/// Max 9/02/09 v8.1088-1 QA70-10581 USE_NEW_LT_WAY_TO_RUN_XF
/// CPY 10/5/09 QA70-14412 RESTORE_DESCSTATS_LT_CODE_FOR_USERS_SCRIPT
/// EJP 2010-06-28 ORG-259-P2 TBAR_BTNS_FOR_ADD_MOVE_MATRIX_OBJ
//**********************************************

//CPY 2006-12-09 NORMALIZE_USING_XF
[Normalize]
	/// Max 11/19/08 QA70-12615 v8.0975b WKS_NORMALIZE_SHOULD_WORK_ON_NON_Y_TYPE_DATA
	///// Max 10/25/07 QA70-10581 LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
	////normalize -d;
	//run -xf normalize -d;
	///// END LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
	/// Max 9/02/09 v8.1088-1 QA70-13673 REPLACE_vnormalize_WITH_rnormalize
	// run -xf vnormalize -d;
	run -xf rnormalize -d;
	/// END REPLACE_vnormalize_WITH_rnormalize
	/// END WKS_NORMALIZE_SHOULD_WORK_ON_NON_Y_TYPE_DATA
//

/// Max 3/29/07 FREQUENCYCOUNT_USING_XF
[FrequencyCount]
	/// Max 10/25/07 QA70-10581 LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
	//freqcounts -d;
	run -xf freqcounts -d;
	/// END LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
///

[Sort]
	#Arranges the worksheet in ascending/descending order according to the highlighted column
	/*Laurie modified v3.83*/
	if (exist(%C)!=1) 
	{
		type -b $general.SelectionError;
		break 1;
	} 
	%A=$General.SortUporDown;
	%B=$General.SortWksorCol;

	checkVar SortOrder 1;
	checkVar selectorder 1;

	/* QZCao_undo, v3.69 */
	undo -wc %H;
	/* QZCao_undo */
	
	if(SortOrder < 1 || SortOrder>2) SortOrder=1;
	if (selectorder<1 || selectorder>2) selectorder=1;

	if (wks.c1==wks.c2)
	{
		getn $WksAnaly.SortSort sortorder:A $WksAnaly.SortSelect selectorder:B $WksAnaly.SortSortData;
		if (sortorder==1 && selectorder==1)
		sort -w %H %C;
		
		if (sortorder==1 && selectorder==2)
		sort -c wks.c1 wks.c2 %C;
		
		if (sortorder==2 && selectorder==2)
		sort -cd wks.c1 wks.c2 %C;
		
		if (sortorder==2 && selectorder==1)
		sort -wd %H %C;
		
	}
	else
	{
		getn $WksAnaly.SortSecond sortorder:A $WksAnaly.SortRegion;
		if (sortorder==1)
			sort -c wks.c1 wks.c2 %C;
		else
			sort -cd wks.c1 wks.c2 %C;
	}
	return 0;

[SortRangeAsc]
	sort -c wks.c1 wks.c2 %C;
	return 0;

[SortRangeDesc]
	sort -cd wks.c1 wks.c2 %C;
	return 0;

[SortColAsc]
	sort -c wks.c1 wks.c2 %C;
	return 0;

[SortColDesc]
	sort -cd wks.c1 wks.c2 %C;
	return 0;

[SortWksAsc]
	sort -w %H %C;
	return 0;

[SortWksDesc]
	sort -wd %H %C;
	return 0;

[AddNewCol]
	if(run.section(,CheckForSelection, AddColNoSelection)==0)
	{
		v1=1;
		GetNum -s $EXEmessage.HowManyCol v1 $EXEmessage.HowManyColTitle;
		worksheet -a v1;
	}
	return 0;

/// EJP 2010-06-28 ORG-259-P2 TBAR_BTNS_FOR_ADD_MOVE_MATRIX_OBJ
[AddOneNewCol]
	worksheet -a 1;
	return 0;
/// end TBAR_BTNS_FOR_ADD_MOVE_MATRIX_OBJ

[Multix]
	wks.multix=wks.multix==0?1:0;
	return 0;

[CheckForSelection]
	if(wks.iscolsel()!=0)
	{
		type -n $Wksheet.%1;
	}
	return 0;
	
[GoToRow]
//--------- CPY 8/8/06 QA70-8626 GOTO_ROW_CHANGED_TO_GOTO
/*
	v1=y1+1;
	GetNum -s $EXEmessage.GotoRow v1 $EXEmessage.GotoTitle;
	y1=v1-1;
*/
	/// Max 10/25/07 QA70-10581 LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
	//dlgRowColGoto -d;
	run -xf dlgRowColGoto -d;
	/// END LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
//---------
	return 0;

//---- CPY 9/18/06 OC_BASED_WKS_SCRIPT
[WScriptDlg]
	if(run.LoadOC(Originlab\WScriptDlg.c) == 0)
		OpenWscriptDlg;
	else
		type "failed to load WScriptDlg.c";
//----

/// SY 2006-09-27 v8.0489 QA70-7215 COL_MOVE_CMD_USING_XF
[MoveFirst]
	/// Max 9/02/09 v8.1088-1 QA70-10581 USE_NEW_LT_WAY_TO_RUN_XF
	//colmove op:=0;
	run -xf colmove op:=0;
	///---
[MoveLast]
	/// Max 9/02/09 v8.1088-1 QA70-10581 USE_NEW_LT_WAY_TO_RUN_XF
	// colmove op:=1;
	run -xf colmove op:=1;
	///---

[MoveRight]
	/// Max 9/02/09 v8.1088-1 QA70-10581 USE_NEW_LT_WAY_TO_RUN_XF
	// colmove op:=2;
	run -xf colmove op:=2;
	///---

[MoveLeft]
	/// Max 9/02/09 v8.1088-1 QA70-10581 USE_NEW_LT_WAY_TO_RUN_XF
	// colmove op:=3;
	run -xf colmove op:=3;
	///---

[MoveSwap]
	colswap;
/// end COL_MOVE_CMD_USING_XF

///---Sim 06-02-2009 QA80-4584 EXTRACT_WKS_SUPPORT_AUTO_UPDATE
[ExtractWksData]
	if( run.LoadOC("Originlab\ExtractWksDataDlg.c", 16) == 0 )
		run -oc OpenExtractWksModelssDialog;
	else
		type "failed to load ExtractWksDataDlg.c";
///---END QA80-4584 EXTRACT_WKS_SUPPORT_AUTO_UPDATE


//---- CPY 10/5/09 QA70-14412 RESTORE_DESCSTATS_LT_CODE_FOR_USERS_SCRIPT
//////////////////////////////////////////////////////////////////////////////////
/// old LT codes from Origin75, put here for supporting user's old scripts only
//////////////////////////////////////////////////////////////////////////////////
// stats on col with selected data
//      run.section(WKS,DescStats);
// stats on rows
//      run.section(WKS,DescStats, 1);
[DescStats]
	if((0 < macro.narg) && %1)
	{
		run.section(,StatRow);
	}
	else
	{
		run.section(,StatCol);
	}
	return 0;
/// end CLASS_SECTION_NAME_WITH_ARG_FOR_70
[StatCol]
//////////////////////////////////////////////////////////////////////////////////
/// old LT codes from Origin75, put here for supporting user's old scripts only
//////////////////////////////////////////////////////////////////////////////////
//  Descriptive statistics on the selected columns; results in new worksheet
	if (wks.sel&8!=8) 
	{
		type -b $general.SelectionError;
		break 1;
	} 
	%W = %H; /* save worksheet name */
	ii=wks.sel;
	win -T Wks ColStat;

	if(ii&2==2)
		Range.Choice=1;
	else
		Range.Choice=2;

	store.v1$=%W;
	count=1;
	loop(ii,1,%W!wks.iscolsel())
	{
		jj=%W!wks.c1+%(%W,@O,ii-1);
		if ((%W!wks.col$(jj).type)!=2 && (%W!wks.col$(jj).format)!=2)
		{
			%A=%(%W,@Col,jj);
			wcol(1)[count]$ = "%A";
						//--------- CPY v6.0303 t8198 7/13/00 LARGE_ROW_NUMBER_LOW_SIG_DIGITS
			//wcol(2)[count]$ = "[$(%W!wks.r1):$(%W!wks.r2)]";
			wcol(2)[count]$ = "[$(%W!wks.r1,%d):$(%W!wks.r2,%d)]";
						//---------	end t8198 $(x,%d) will get integer row numbers
			count+=1;
		}
	}
	Recalculate.run();
	// GJL 5/28/02 v7.0313 QA70-1868 ADD_DATA_FROM_PAGE_LABEL	
	page.label$=statlabel.text$;
	page.title=3;
	return 0;
	// End GJL 5/28/02 v7.0313 QA70-1868 ADD_DATA_FROM_PAGE_LABEL
[StatRow]
//////////////////////////////////////////////////////////////////////////////////
/// old LT codes from Origin75, put here for supporting user's old scripts only
//////////////////////////////////////////////////////////////////////////////////
//  Descriptive statistics across rows of the selection; results in new worksheet
	if (wks.sel&8!=8) 
	{
		type -b $general.SelectionError;
		break 1;
	} 
	if (wks.iscolsel()!=(wks.c2-wks.c1+1)) 
	{
		type -b $general.ContiguousError;
		break 1;
	} 

	select1=wks.c1;
	select2=wks.c2;

	loop(ii,wks.c1,wks.c2)
	{
		if ((wks.col$(ii).type)==2 || (wks.col$(ii).format)==2)
		{
			if(ii==select1)
				select1+=1;
			else
				set %(%H,ii) -es 0;
		}
	}

	for(ii=select2;ii>=select1;ii-=1)
	{
		if ((wks.col$(ii).type)==2 || (wks.col$(ii).format)==2)
			select2-=1;
		else
			break;
	}
	if(select2<select1)
	{
		type -b $general.ContiguousError;
		break;
	}
	//----------- CPY 6/30/00 v6.0295 t7988 ADD_SUM_TO_ROW_STAT
	//sum(%H,select1,select2);
	_sum = sum(%H,select1,select2);
	//-----------
	get %(%H,select1) -e ii;
	ii=ii<wks.r2?ii:wks.r2;
	%W = %H; /* save worksheet name */
	win -T Wks RowStat;
	%A="Col(%(%W,@Col,select1)) => Col(%(%W,@Col,select2))";
	// GJL 5/28/02 v7.0313 QA70-1868 ADD_DATA_FROM_PAGE_LABEL
	%B=$WksStat.StatLabel;
	label -s -sa -n stat "%B\r\n%A";
	page.label$="%B %A";
	page.title=3;
	// End GJL 5/28/02 v7.0313 QA70-1868 ADD_DATA_FROM_PAGE_LABEL

	set %H -er (ii-%W!wks.r1+1);
	col(row)=data(%W!wks.r1,ii,1);
	copy -b %W!wks.r1 _mean col(mean) -b 1 -e (ii-%W!wks.r1+1);
	copy -b %W!wks.r1 _sd col(sd) -b 1 -e (ii-%W!wks.r1+1);
	copy -b %W!wks.r1 _min col(min) -b 1 -e (ii-%W!wks.r1+1);
	copy -b %W!wks.r1 _max col(max) -b 1 -e (ii-%W!wks.r1+1);
	copy -b %W!wks.r1 _npts col(n) -b 1 -e (ii-%W!wks.r1+1);
	copy -b %W!wks.r1 _range col(range) -b 1 -e (ii-%W!wks.r1+1);
	//----------- CPY 6/30/00 v6.0295 t7988 ADD_SUM_TO_ROW_STAT
	copy -b %W!wks.r1 _sum col(sum) -b 1 -e (ii-%W!wks.r1+1);
	//-----------
	col(se)=col(sd)/sqrt(col(n));
	delete -v select1;
	delete -v select2;
	return 0;
//---- end RESTORE_DESCSTATS_LT_CODE_FOR_USERS_SCRIPT