ویژگی های کمتر استفاده شده در NET. - بخش ششم
نویسنده: وحید محمدطاهری
تاریخ: ۱۳۹۶/۰۱/۳۰ ۱۴:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
static void AddChartButton( Workbook workBook,
Worksheet xlWorkSheetNew,
Range currentRange, int macroId,
int startRow, int endRow,
int startCol, int endCol,
string buttonImagePath )
{
var cell = currentRange.Next;
var width = cell.Width;
var height = 24;
var left = cell.Left;
var top = System.Math.Max( cell.Top + cell.Height - height, 0 );
var button = xlWorkSheetNew.Shapes.AddPicture( buttonImagePath,
MsoTriState.msoFalse,
MsoTriState.msoCTrue,
left, top, width, height );
var module = workBook.VBProject.VBComponents.Add( vbext_ComponentType.vbext_ct_StdModule );
module.CodeModule.AddFromString( GetMacro( macroId,
startRow, endRow,
startCol, endCol ) );
button.OnAction = "Macro" + macroId;
}
static string GetMacro( int macroId,
int startRow, int endRow,
int startCol, int endCol )
{
var sb = new StringBuilder();
var range = "ActiveSheet.Range(Cells(" + startRow + "," + startCol + "), Cells(" + endRow + "," + endCol + ")).Select";
sb.AppendLine( "Sub Macro" + macroId + "()" );
sb.AppendLine( "On Error Resume Next" );
sb.AppendLine( range );
sb.AppendLine( "ActiveSheet.Shapes.AddChart.Select" );
sb.AppendLine( "ActiveChart.ChartType = xlColumn" );
sb.AppendLine( "ActiveChart.SetSourceData Source:=" + range );
sb.AppendLine( "On Error GoTo 0" );
sb.AppendLine( "End Sub" );
return sb.ToString();
} و برای استفاده از آن میتوان مانند مثال زیر عمل کرد:
var excelApp = new Microsoft.Office.Interop.Excel.Application();
var fileName = @"C:\Users\Vahid\Desktop\VBA.xlsm";
var workBook = excelApp.Workbooks.Open( fileName );
var sheet = workBook.Sheets[1];
AddChartButton( workBook,
sheet,
sheet.Range["B1"],
1231,
1,
10,
1,
2,
@"C:\Users\Vahid\Desktop\BarChart.png");
excelApp.DisplayAlerts = false;
workBook.Close( true,
fileName ); خروجی مثال بالا
نکته: در صورتیکه بعد از اجرای برنامه، خطای " programmatic access to visual basic project is not trusted" رخ داد از این طریق میتوانید مشکل را حل کنید.
File -> Options -> Trust Center -> Trust Center Settings -> Macro Settings -> Trust Access to the VBA Project object model
class Program
{
volatile bool _shouldPartyContinue = true;
static void Main()
{
var firstDimension = new Program();
var secondDimension = new Thread( firstDimension.StartPartyInAnotherDimension );
secondDimension.Start( firstDimension );
Thread.Sleep( 5000 );
firstDimension._shouldPartyContinue = false;
Console.WriteLine( "Party Finish" );
}
void StartPartyInAnotherDimension( object input )
{
var currentDimensionInput = (Program)input;
Console.WriteLine( "let the party begin" );
while ( currentDimensionInput._shouldPartyContinue ) {}
Console.WriteLine( "Party ends: (" );
}
} class Program
{
public static void Main(string[] args)
{
Console.WriteLine( Number ); // Error
global::System.Console.WriteLine( "Console: " + Console ); //OK
}
public class System { }
// Define a constant called ‘Console’ to cause more problems.
const int Console = 7;
const int Number = 67;
} [DebuggerDisplay( "{DebuggerDisplay}" )]
public class DebuggerDisplayTest
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
[DebuggerBrowsable( DebuggerBrowsableState.Never )]
string DebuggerDisplay => $"{FirstName} {LastName} {Age} years old";
}
[DebuggerDisplay( "Age {Age > 0 ? Age : 25}" )]
public class DebuggerDisplayTest
{
//...
}