📄 wks_utils.c
字号:
tr.C1.nVal = -1;
}
else // Else no TreeNode return error...
iRet = WKS_UTILS_INVALID_TREENODE_ERROR;
return iRet;
}
/**
Gets the current row wise selection from an Origin worksheet or Excel workbook in
native Origin or Excel format. The selected range must be contiguous. A comma
separated selection string with one entry for each selected or partially selected
row is returned along with the number of entries.
Example:
string strSelectedRanges;
int iRet, iNumRowRanges;
iRet = GetRowWiseWksSelection(strSelectedRanges, iNumRowRanges);
Parameters:
strSelectedRanges=Output selected row ranges
iNumRowRanges=Number of selected row ranges (one for each selected or partially selected row)
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 row. 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 GetRowWiseWksSelection( string& strSelectedRanges, int& iNumRowRanges, BOOL bSelectAll ) // bSelectAll = FALSE
{
int ii, iPageType, iSelectionType, iC1, iC2, iR1, iR2;
string strWindowName, strSheetName, strCol1Name, strCol2Name, strCurrentRange;
PageBase pbActiveWindow;
Worksheet wksActiveWorksheet;
Column col1, col2;
// Initialize to NULL and 0
strSelectedRanges.Empty();
strSheetName.Empty();
iNumRowRanges = 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
iC2 = wksActiveWorksheet.GetNumCols() - 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
// If the selection is discontiguous...
if( iSelectionType & WKS_SEL_DISCONTIGUOUS )
return WKS_UTILS_BAD_SEL_ERROR; // Return an error code
// If there is no selection...
if( iSelectionType == WKS_SEL_NONE )
return WKS_UTILS_NO_SEL_WARNING; // Return a warning code
}
iNumRowRanges = iR2 - iR1 + 1;
// Get first column name...if column is not valid...
col1.Attach( wksActiveWorksheet, iC1 );
if( !col1.IsValid() )
return WKS_UTILS_BAD_SEL_ERROR; // Return an error code
strCol1Name = col1.GetName();
// Get second column name...if column is not valid...
col2.Attach( wksActiveWorksheet, iC2 );
if( !col2.IsValid() )
return WKS_UTILS_BAD_SEL_ERROR; // Return an error code
strCol2Name = col2.GetName();
for( ii = iR1; ii < iR2 + 1; ii++ )
{
// 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, strCol1Name, ii + 1, strCol2Name, ii + 1 );
else // Else format selection range as Origin worksheet
strCurrentRange.Format( WKS_UTILS_WKS_CONTIG_SEL, strWindowName, strCol1Name, ii + 1, strCol2Name, ii + 1 );
// If not first range append comma separator
if( ii != iR1 )
strSelectedRanges += ", ";
// Append current range
strSelectedRanges += strCurrentRange;
}
return WKS_UTILS_NO_ERROR; // Return no error and good selected ranges string
}
int FindEmptyColumn(Worksheet &wks, int nStartCol)
{
for( int nCol = nStartCol; nCol < wks.GetNumCols(); nCol++ )
{
/// EJP 09-30-2003 v7.5710 QA70-4073.63 FIX_FIND_EMPTY_TEXT_COL
///Dataset ds(wks.Columns(nCol));
///if( -1 == ds.GetUpperIndex() )
/// return nCol;
int i1, i2;
Column col;
col = wks.Columns(nCol);
if( col && col.GetRange(i1, i2) )
{
if( -1 == i2 )
return nCol;
}
/// end FIX_FIND_EMPTY_TEXT_COL
}
return -1; // no empty columns
}
/**
Parse a worksheet/workbook selection string and return the data contained in the
worksheet selection range.
Parameters:
strWksSelection=Input worksheet/workbook selection string having form "[Book1]Sheet1!$A$1:$B$7"
or "Worksheet_A[1]:B[7]"
mData=Output data
Return:
Returns WKS_UTILS_NO_ERROR and a matrix containing the data contained in the worksheet
selection range on success and returns WKS_UTILS_BAD_SEL_ERROR on failure.
*/
int GetDataInWksSelectionRange(string strWksSelection, matrix& mData)
{
string strWksName, strSheetName;
int iC1, iC2, iR1, iR2, iPageType;
WorksheetPage wpWksPage;
Worksheet wksWorksheet;
// Parse worksheet selection range...if valid...
if( ParseWksSelection(strWksSelection, strWksName, strSheetName, iC1, iC2, iR1, iR2) )
{
wpWksPage = Project.WorksheetPages(strWksName); // Get WorksheetPage
if( wpWksPage ) // If WorksheetPage is valid...
{
iPageType = wpWksPage.GetType(); // Get window type
if( EXIST_WKS == iPageType ) // If window is a worksheet...
wksWorksheet = (Worksheet) wpWksPage.Layers(); // Get active layer in worksheet
else if( EXIST_EXTERN_WKS == iPageType ) // Else if window is an Excel workbook...
{
wksWorksheet = (Worksheet) wpWksPage.Layers(strSheetName); // Get specified layer in workbook
wksWorksheet.UpdateOrigin(); // Update Origin from Excel
}
if( wksWorksheet ) // If worksheet is valid...
if( mData.CopyFromWks(wksWorksheet, iC1, iC2, iR1, iR2) ) // Copy data from worksheet
return WKS_UTILS_NO_ERROR; // If copy is successful...return with no error
}
}
return WKS_UTILS_BAD_SEL_ERROR; // "Else"...return an error code
}
/**
Get a single column worksheet/workbook range without rows.
Parameters:
strRange=Input worksheet/workbook range with row ranges
Return:
Returns a single column worksheet/workbook range without rows.
*/
string GetSingleColumnRangeWithoutRows(const string& strRange)
{
string str;
if( strRange[0] == '[' ) // If first character is [ then Excel range...
{
str = strRange.GetToken(0, '$'); // Get book and sheet
str += "$"; // Add token separator back
str += strRange.GetToken(1, '$'); // Add first column name without rows
}
else // Else must be Origin worksheet
str = strRange.GetToken(0, '['); // Get worksheet and first column names without rows
return str;
}
/**
Parse a column selection string and return the worksheet/workbook name, workbook sheet
name (if Excel workbook), and column index (0 based offset, as an integer).
Parameters:
strColSelection=Input column selection string
strWksName=Output worksheet/workbook name
strSheetName=Output workbook sheet name (if Excel workbook, else NULL)
iC1=Output index of selected column (0 based)
Return:
Returns TRUE, worksheet/workbook name, workbook sheet name (if Excel), and column index
(0 based offset, as an integer) on success and FALSE on failure.
*/
BOOL ParseColSelection( string strColSelection, string& strWksName, string& strSheetName, int& iC1 )
{
int ii;
string str;
strWksName.Empty(); // Initialize
strSheetName.Empty();
iC1=-1;
// Trim white space
str = strColSelection;
str.TrimLeft();
str.TrimRight();
// Excel workbbook format: str=="[Book1]Sheet1!$A"
// Origin worksheet format: str=="Worksheet_A"
if( str.GetAt(0) == '[' ) // If first character is [ assume Excel column format
{
Worksheet wksBook;
Worksheet wksSheet;
string strCheckSheetName;
// str=="[Book1]Sheet1!$A"
str = str.Mid(1); // Strip off [ character
// str=="Book1]Sheet1!$A"
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"
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"
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 $ character
// str=="A"
if( !wksSheet.Columns( str ).IsValid() ) // If selected column is not valid return error
return FALSE;
iC1 = wksSheet.Columns( str ).GetIndex(); // Get column number of selected column
if( iC1 < 0 ) // If column not valid return error
return FALSE;
}
else // Else first character not [ assume Origin column format
{
Worksheet wksWorksheet;
// str=="Worksheet_A"
ii = str.Find('_'); // Find _ character
if( ii < 0 ) // If no _ return error
return FALSE;
strWksName = str.Left(ii); // Get Origin worksheet name
wksWorksheet.Attach( strWksName );// Attach to worksheet
if( !wksWorksheet.IsValid() ) // If worksheet not valid return error
return FALSE;
str = str.Mid( ii + 1 ); // Strip off worksheet name and _ character
// str=="A"
if( !wksWorksheet.Columns( str ).IsValid() ) // If selected column is not valid return error
return FALSE;
iC1 = wksWorksheet.Columns( str ).GetIndex(); // Get column number of selected column
if ( iC1 < 0 ) // If not valid return error
return FALSE;
}
return TRUE; // Success!
}
/**
Add the current selected columns to subnodes in the specified TreeNode.
Example:
See function call in StatisticsOn.c.
Parameters:
tr=Input TreeNode to which selected columns will added
bAppend=Input option to append new columns (true) or to reset tree and
add starting in first subnode (default false)
bSelectAll=Input option to select all columns in worksheet/workbook sheet if there are
none selected (true) or to return a no selection warning code if there is no selection
(default false)
Return:
Returns WKS_UTILS_NO_ERROR on success and a WKS_UTILS warning or error code on failure.
*/
int AddColSelectionsToTreeNode(TreeNode& tr, bool bAppend, bool bSelectAll) // bAppend = false, bSelectAll = false
{
string strRange, strNodeName, strSelectedRanges;
string strWksName, strSheetName;
int ii, iRet, iBeginNodeNum, iNumRanges;
int iC1, iC2, iR1, iR2;
TreeNode trSubNode; // Must use TreeNode here since Tree is always a standalone copy
if( tr ) // If TreeNode is valid...
{
iRet = GetNonContiguousWksSelection(strSelectedRanges, iNumRanges); // Get current selection range
if( iRet ) // If no selection or bad selection...
{
if( bSelectAll && ( iRet == WKS_UTILS_NO_SEL_WARNING ) ) // If bSelectAll is true and no selection...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -