⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wks_utils.c

📁 图像处理的压缩算法
💻 C
📖 第 1 页 / 共 4 页
字号:
/*------------------------------------------------------------------------------*
 * File Name: Wks_Utils.c														*
 * Creation: GJL 5/22/2002														*
 * Purpose: Origin C file containing Worksheet Utilities						*
 * Copyright (c) OriginLab Corp.	2002-2007									*
 * All Rights Reserved															*
 * 																				*
 * Modification Log:															*
 * EJP 09-30-2003 v7.5710 QA70-4073.63 FIX_FIND_EMPTY_TEXT_COL					*
 *------------------------------------------------------------------------------*/

////////////////////////////////////////////////////////////////////////////////////
// Included header files
//////////////////////////////////////////////////////////////////////////////////
//
#include <origin.h>       // EXIST_ codes
#include "Wks_Utils.h"      // Function prototypes and non-localized constants
#include <tree_utils.h>
 
//
////////////////////////////////////////////////////////////////////////////////////

/** 
		Gets the current selection from an Origin worksheet or Excel workbook in native
		Origin or Excel format. The selected range may be either contiguous or not. A comma
		separated selection string with one entry for each selected or partially selected
		column is returned along with the number of entries.
	Example:
		string strSelectedRanges;
		int iRet, iNumRanges;
		iRet = GetNonContiguousWksSelection( strSelectedRanges, iNumRanges );
	Parameters:
		strSelectedRanges=Output selected ranges
		iNumRanges=Number of selected ranges (one for each selected or partially selected column)
		bSelectAll=Returns entire worksheet/workbook sheet as selection range, default is FALSE
	Return:
		If successful returns WKS_UTILS_NO_ERROR and a formatted string containing the
		current Origin worksheet or Excel workbook selection. The selection string is a comma
		separated string with one entry for each selected or partially selected column. The number
		of entries is also returned. If there is a problem getting the selection then
		WKS_UTILS_BAD_SEL_ERROR, an empty string, and 0 are returned. If there is no selection
		then WKS_UTILS_NO_SEL_WARNING, an empty string, and 0 are returned.
*/
int GetNonContiguousWksSelection( string& strSelectedRanges, int& iNumRanges, BOOL bSelectAll ) // bSelectAll = FALSE
{
	int ii, iPageType, iSelectionType, iC1, iC2, iR1, iR2;
	string strWindowName, strSheetName, strColumnName, strCurrentRange;
	PageBase pbActiveWindow;
	Worksheet wksActiveWorksheet;
	vector<int> vSelCols;
	Column colN;
	
	waitCursor	wcCursor; // Put up a wait cursor

	// Initialize to NULL and 0
	strSelectedRanges.Empty();
	strSheetName.Empty();
	iNumRanges = 0;

	// Get active window...if not valid...
	pbActiveWindow = Project.Pages();
	if( !pbActiveWindow.IsValid() )
		return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	
	
	// Get window name and type
	strWindowName = pbActiveWindow.GetName();
	iPageType = pbActiveWindow.GetType();
	
	// If window is not a worksheet or workbook...
	if( iPageType != EXIST_WKS && iPageType != EXIST_EXTERN_WKS )
		return WKS_UTILS_BAD_SEL_ERROR; // Return an error code
	
	// Get active layer in worksheet or workbook...if error...
	wksActiveWorksheet = (Worksheet) Project.ActiveLayer();
	if( !wksActiveWorksheet.IsValid() )
		return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	
	
	// If window is an Excel workbook...
	if( iPageType == EXIST_EXTERN_WKS )
	{
		// Update Origin from Excel
		if( !wksActiveWorksheet.UpdateOrigin() )
			return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	

		// Get Excel sheet name...if error...
		if( !wksActiveWorksheet.GetName( strSheetName ) )
			return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	
	}

	// If user wants entire worksheet/workbook as selection range...
	if( bSelectAll )
	{
		// Simulate entire worksheet/workbook as selected...
		iSelectionType = WKS_SEL_ALL; // Select entire worksheet/workbook
		iC1 = 0; // Set number of first column
		iNumRanges = wksActiveWorksheet.GetNumCols(); // Get number of columns
		vSelCols.SetSize( iNumRanges ); // Set all columns as selected
		for( ii = 0; ii < iNumRanges; ii++ )
			vSelCols[ii] = ii;
		iC2 = iNumRanges - 1; // Set number of last column
		wksActiveWorksheet.GetBounds(iR1, 0, iR2, -1); // Set number of first row and last row
	}
	else // Get current worksheet/workbook selection
	{
		// Get selection type and range
		iSelectionType = wksActiveWorksheet.GetSelection( iC1, iC2, iR1, iR2 );

		// If editing a cell...
		if( iSelectionType & WKS_SEL_EDIT )
			return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	

		// Get list of columns selected or partially selected in worksheet/workbook
		if( !wksActiveWorksheet.GetSelectedColumns( vSelCols ) )
			return WKS_UTILS_BAD_SEL_ERROR; // Return an error code
		
		// Get number of selected or partially selected columns
		iNumRanges = vSelCols.GetSize();

		// If there is no selection...
		if( iSelectionType == WKS_SEL_NONE || iNumRanges == WKS_SEL_NONE )
			return WKS_UTILS_NO_SEL_WARNING; // Return a warning code
	}

	for( ii = 0; ii < iNumRanges; ii++ )
	{
		// Get current column name...if column is not valid...
		colN.Attach( wksActiveWorksheet, vSelCols[ii] );
		if( !colN.IsValid() )
			return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	
		strColumnName = colN.GetName();
		
		// If entire worksheet or entire columns are selected then override worksheet
		//   selection/bounds with upper and lower bounds of each column
		if( iSelectionType == WKS_SEL_ALL || iSelectionType & WKS_SEL_COLUMN )
		{
			iR1 = colN.GetLowerBound();
			iR2 = colN.GetUpperBound();
		}

		// Build range for current column
		if( iPageType == EXIST_EXTERN_WKS ) // If Excel workbook format selection range as such
			strCurrentRange.Format( WKS_UTILS_EXCEL_CONTIG_SEL, strWindowName, strSheetName, strColumnName, iR1 + 1, strColumnName, iR2 + 1 );
		else // Else format selection range as Origin worksheet
			strCurrentRange.Format( WKS_UTILS_WKS_CONTIG_SEL, strWindowName, strColumnName, iR1 + 1, strColumnName, iR2 + 1 );

		// If not first range append comma separator
		if( ii != 0 )
			strSelectedRanges += ", ";
		
		// Append current range
		strSelectedRanges += strCurrentRange;
	}
	
	return WKS_UTILS_NO_ERROR; // Return no error and good selected ranges string
}

/**
		Gets the current selection from an Origin worksheet or Excel workbook in native
		Origin or Excel format. The selected range must be contiguous.
	Example:
		string strSelectedRange;
		int iRet;
		iRet = GetContiguousWksSelection( strSelectedRange );
	Parameters:
		strSelectedRange=Output selected range
		bSelectAll=Returns entire worksheet/workbook sheet as selection range, default is FALSE
	Return:
		If successful returns WKS_UTILS_NO_ERROR and a formatted string containing the
		current Origin worksheet or Excel workbook selection. If bSelectAll is TRUE then
		the entire worksheet is returned as the selection string. If there is a problem
		getting the selection or if the selected range is not contiguous then
		WKS_UTILS_BAD_SEL_ERROR and an empty string are returned. If there is no 
		selection then WKS_UTILS_NO_SEL_WARNING and an empty string are returned.
*/
int GetContiguousWksSelection( string& strSelectedRange, BOOL bSelectAll ) // bSelectAll = FALSE
{
	int iPageType, iSelectionType, iC1, iC2, iR1, iR2;
	string strWindowName, strSheetName, strC1Name, strC2Name;
	PageBase pbActiveWindow;
	Worksheet wksActiveWorksheet;
	Column colN;
	
	waitCursor	wcCursor; // Put up a wait cursor
	
	// Initialize to NULL
	strSelectedRange.Empty();
	strSheetName.Empty();
	
	// Get active window...if not valid...
	pbActiveWindow = Project.Pages();
	if( !pbActiveWindow.IsValid() )
		return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	
	
	// Get window name and type
	strWindowName = pbActiveWindow.GetName();
	iPageType = pbActiveWindow.GetType();
	
	// If window is not a worksheet or workbook...
	if( iPageType != EXIST_WKS && iPageType != EXIST_EXTERN_WKS )
		return WKS_UTILS_BAD_SEL_ERROR; // Return an error code

	// Get active layer in workbook...if error...
	wksActiveWorksheet = (Worksheet) Project.ActiveLayer();
	if( !wksActiveWorksheet.IsValid() )
		return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	

	// If window is an Excel workbook...
	if( iPageType == EXIST_EXTERN_WKS )
	{
		// Update Origin from Excel
		if( !wksActiveWorksheet.UpdateOrigin() )
			return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	

		// Get Excel sheet name...if error...
		if( !wksActiveWorksheet.GetName( strSheetName ) )
			return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	
	}

	// If user wants entire worksheet/workbook as selection range...
	if( bSelectAll )
	{
		// Simulate entire worksheet/workbook as selected...
		iSelectionType = WKS_SEL_ALL;
		iC1 = 0;
		iC2 = wksActiveWorksheet.GetNumCols() - 1;
		wksActiveWorksheet.GetBounds(iR1, 0, iR2, -1);
	}
	else // Get current worksheet/workbook selection
	{
		iSelectionType = wksActiveWorksheet.GetSelection( iC1, iC2, iR1, iR2 );

		// If there is no selection...
		if( iSelectionType == WKS_SEL_NONE )
			return WKS_UTILS_NO_SEL_WARNING; // Else Return a warning code

		// If editing a cell or if the selection is not contiguous...
		if( iSelectionType & WKS_SEL_EDIT || iSelectionType & WKS_SEL_DISCONTIGUOUS )
			return WKS_UTILS_BAD_SEL_ERROR; // Return an error code
	}

	// Get first column name...if column is not valid...
	colN.Attach( wksActiveWorksheet, iC1 );
	if( !colN.IsValid() )
		return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	
	strC1Name = colN.GetName();
	
	// Get second column name...if column is not valid...
	colN.Attach( wksActiveWorksheet, iC2 );
	if( !colN.IsValid() )
		return WKS_UTILS_BAD_SEL_ERROR; // Return an error code	
	strC2Name = colN.GetName();

	if( iPageType == EXIST_EXTERN_WKS ) // If Excel workbook format selection range as such
		strSelectedRange.Format( WKS_UTILS_EXCEL_CONTIG_SEL, strWindowName, strSheetName, strC1Name, iR1 + 1, strC2Name, iR2 + 1 );
	else                 // Else format selection range as Origin worksheet
		strSelectedRange.Format( WKS_UTILS_WKS_CONTIG_SEL, strWindowName, strC1Name, iR1 + 1, strC2Name, iR2 + 1 );
	
	return WKS_UTILS_NO_ERROR; // Return no error and good selected range string
}

/**
		Parse a worksheet/workbook selection string and return the worksheet/workbook name, workbook sheet
		name (if Excel workbook), and column and row selection indices (0 based offsets, as integers).
	Example:
		See function omConvertWksToMatrixDirect in OMat.c for sample call.
	Parameters:
		strWksSelection=Input worksheet/workbook selection string
		strWksName=Output worksheet/workbook name
		strSheetName=Output workbook sheet name (if Excel workbook, else NULL)
		iC1=Output index of first selected column (0 based)
		iC2=Output index of last selected column (0 based)
		iR1=Output index of first selected row (0 based)
		iR2=Output index of last selected row (0 based)
	Return:
		Returns TRUE, worksheet/workbook name, workbook sheet name (if Excel), and column and row selection
		indices (0 based offsets, as integers) on success and FALSE on failure.
*/
BOOL ParseWksSelection( string strWksSelection, string& strWksName, string& strSheetName, int& iC1, int& iC2, int& iR1, int& iR2 )
{
	int ii;
	string str, strCol, strRow;
	
	strWksName.Empty();                  // Initialize
	strSheetName.Empty();
	iC1=iC2=iR1=iR2=-1;

	// Trim white space
	str = strWksSelection;
	str.TrimLeft();
	str.TrimRight();

	// Excel workbbook format:  str=="[Book1]Sheet1!$A$1:$B$7"
	// Origin worksheet format: str=="Worksheet_A[1]:B[7]"
	if( str.GetAt(0) == '[' )            // If first character is [ assume Excel workbook format
	{
		Worksheet wksBook;
		Worksheet wksSheet;
		string strCheckSheetName;

		// str=="[Book1]Sheet1!$A$1:$B$7"
		str = str.Mid(1);                // Strip off [ character
		
		// str=="Book1]Sheet1!$A$1:$B$7"
		ii = str.Find(']');              // Find ] character
		if( ii < 0 )                     // If no ] return error
			return FALSE;
		strWksName = str.Left(ii);       // Get Excel workbook name
		wksBook.Attach( strWksName );    // Attach to worksheet underlying workbook 
		if( !wksBook.IsValid() )         // If worksheet not valid return error
			return FALSE;
		str = str.Mid( ii + 1 );         // Strip off workbook name and ] character
		
		// str=="Sheet1!$A$1:$B$7"
		ii = str.Find('!');              // Find ! character
		if( ii < 0 )                     // If no ! return error
			return FALSE;
		strSheetName = str.Left(ii);     // Get Excel workbook sheet name from input string
		// Get Excel workbook sheet name from internal Origin workbook sheet
		wksSheet = (Worksheet) wksBook.GetPage().Layers( strSheetName ); // Get internal workbook sheet object by name
		if( !wksSheet.IsValid() )        // If worksheet not valid return error
			return FALSE;
		if( !wksSheet.GetName( strCheckSheetName ) ) // Get name of internal workbook sheet, if problem return error
			return FALSE;
		if( strSheetName.CompareNoCase( strCheckSheetName ) ) // If sheet names do not match return error 
			return FALSE;
 		str = str.Mid( ii + 1 );         // Strip off sheet name and ! character

		// str=="$A$1:$B$7"
		ii = str.Find('$');              // Find first $ character
		if( ii != 0 )                    // If $ is not next character return error
			return FALSE;		
		str = str.Mid( ii + 1 );         // Strip off first $ character
		
		// str=="A$1:$B$7"
		ii = str.Find('$');              // Find second $ character
		if( ii < 0 )                     // If no $ return error
			return FALSE;
		strCol = str.Left(ii);           // Get name of first column
		if( !wksSheet.Columns( strCol ).IsValid() ) // If first selected column is not valid return error  
			return FALSE;
		iC1 = wksSheet.Columns( strCol ).GetIndex(); // Get column number of first selected column
		if( iC1 < 0 )                    // If column not valid return error
			return FALSE;
		str = str.Mid( ii + 1 );         // Strip off name of first column and second $ character
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -