MacroType functions and UDF functions.
These functions list sheets as menu commands and UDF's.
list directly below its caller location.
deligate in an async macro.
as an async macro and called like it was in a manu.
or return a value. Show me I'm wrong.
writing outside the allowed array bounds of its return cell(s).
scratch space and intermediate results, graphs and such.
and a collections of the two.
if he wants to.
[ExcelCommand ( Name = "PushToNewSheet" , MenuName = "Class1" , MenuText = "PushToNewSheet" )]
public static void PushToNewSheet ( )
{
PushToNewSheetUDF ( "yaya" );
}
[ExcelFunction ( Name = "PushToNewSheetUDF" , Category = "Class1" , IsMacroType = true )]
public static string PushToNewSheetUDF ( [ExcelArgument (
Name = "New Sheet Name" , Description = "sheet to push into" )] String NewSheet )
{
return PushToNewSheet ( NewSheet );
}
private static String PushToNewSheet ( String NewSheet )
{
ExcelAsyncUtil . QueueAsMacro ( delegate ( )
{
bool bingo = ( bool ) XlCall . Excel ( XlCall . xlcWorkbookInsert , 1 );
if ( bingo == false )
{
Debug . WriteLine ( "Workbook Insert Failed" );
return;
}
object [ , ] active_sheet = ( object [ , ] ) XlCall . Excel ( XlCall . xlfGetWorkbook , 3 );
object [ , ] all_sheets = ( object [ , ] ) XlCall . Excel ( XlCall . xlfGetWorkbook , 1 );
int new_sheet_name = SortItOut ( all_sheets , active_sheet );
String OldName =( String ) all_sheets [ 0 , new_sheet_name ];
String NewName = Uniquify ( all_sheets , NewSheet , 0 );
XlCall . Excel ( XlCall . xlcWorkbookName , OldName , NewName );
XlCall . Excel ( XlCall . xlcWorkbookSelect , NewName );
} );
return String . Format ( "=PushToNewSheetUDF(\"{0}\")" , NewSheet );
}
private static String PushToNewSheetMacro ( String NewSheet )
{
bool bingo = ( bool ) XlCall . Excel ( XlCall . xlcWorkbookInsert , 1 );
if ( bingo == false )
{
Debug . WriteLine ( "Workbook Insert Failed" );
return "PushToNewSheetMacro() Workbook Insert Failed";
}
object [ , ] active_sheet = ( object [ , ] ) XlCall . Excel ( XlCall . xlfGetWorkbook , 3 );
object [ , ] all_sheets = ( object [ , ] ) XlCall . Excel ( XlCall . xlfGetWorkbook , 1 );
int new_sheet_name = SortItOut ( all_sheets , active_sheet );
String OldName =( String ) all_sheets [ 0 , new_sheet_name ];
String NewName = Uniquify ( all_sheets , NewSheet , 0 );
XlCall . Excel ( XlCall . xlcWorkbookName , OldName , NewName );
XlCall . Excel ( XlCall . xlcWorkbookSelect , NewName );
return NewName;
}
private static string Uniquify ( object [ , ] a , string p , int depth )
{
bool DebugMe = false ;
int l = a . Length ;
for ( int i=0 ; i < l ; i++ )
{
string ayh = (string)a [ 0 , i ] ;
if ( DebugMe )
Debug . WriteLine ( "{0}??endswith??{1}" , ayh , p );
if ( ayh.EndsWith ( p ) )
{
if ( DebugMe )
Debug . WriteLine ( "{0}!!endswith!!{1}" , ayh , p );
string arg = String . Format ( "{0}{1}" , p , depth );
return Uniquify ( a , arg , depth+1 ) ;
}
}
return p ;
}
[ExcelCommand ( Name = "SheetStudy" , MenuName = "Class1" , MenuText = "SheetStudy" )]
public static void SheetStudy ( )
{
bool WorkbookStructureProtected=( bool ) XlCall . Excel ( XlCall . xlfGetWorkbook , 14 );
bool WorkbookWindowProtected=( bool ) XlCall . Excel ( XlCall . xlfGetWorkbook , 15 );
XlCall . Excel ( XlCall . xlcWorkbookProtect , false , false );
object [ , ] active_sheets = (object[,])XlCall . Excel ( XlCall . xlfGetWorkbook , 3 ) ;
for ( int k=0 ; k < 2 ; k++ )
{
bool bingo=( bool ) XlCall . Excel ( XlCall . xlcWorkbookInsert , 1 );
Debug . WriteLine ( "{0}:{1}" , k , bingo );
if(bingo==true)
{
object [ , ] active_sheets_new = ( object [ , ] ) XlCall . Excel ( XlCall . xlfGetWorkbook , 3 );
object [ , ] all_sheets_new = ( object [ , ] ) XlCall . Excel ( XlCall . xlfGetWorkbook , 1 );
int new_sheet_name = SortItOut ( all_sheets_new , active_sheets_new );
String OldName = (String)all_sheets_new [ 0 , new_sheet_name ];
String NewName = String.Format("yaya{0:02}", k) ;
XlCall . Excel ( XlCall . xlcWorkbookName , OldName , NewName );
XlCall . Excel ( XlCall . xlcWorkbookSelect,NewName ) ;
//recover sheet id and rename
}
}
}
private static int SortItOut ( object [ , ] all , object [ , ] hot )
{
int eye = all . Length ;
string ott = ( string ) hot [ 0 , 0 ] ;
for ( int i = 0 ; i < eye ; i++ )
{
string awl = ( string ) all [ 0 , i ] ;
if ( awl == ott )
{
return i ;
}
}
return 0 ;
}