Skip to content

Instantly share code, notes, and snippets.

View AnshulKuthiala's full-sized avatar

AnshulKuthiala

View GitHub Profile
@AnshulKuthiala
AnshulKuthiala / PopulateDataArray.cs
Created December 2, 2018 11:06
[Populate Data Array] Resize table and populate data array #ExcelHelper
public static void PopulateDataArray(this ListObject table, object[,] dataArray)
{
int columns = dataArray.GetLength(1);
int rows = dataArray.GetLength(0);
table.DataBodyRange?.Delete();
if (rows > 0 && columns > 0)
{
table.ListRows.Add();
table.ListRows[1].Range[1, 1].Value2 = "Dummy";
Range range = table.Range.Cells[1, 1].Resize[rows + 1, columns];
@AnshulKuthiala
AnshulKuthiala / XlApp.cs
Created November 30, 2018 15:05
[Excel Application Instance] #ExcelHelper
public static Microsoft.Office.Interop.Excel.Application xlApp = Globals.ThisWorkbook.Application;
@AnshulKuthiala
AnshulKuthiala / TimeDifference.cs
Last active November 30, 2018 14:34
[Calculate Time Difference]
DateTime timeStart = DateTime .Now;
//Perform Activity
TimeSpan difference = DateTime.Now.Subtract(timeStart);
MessageBox.Show($"Completed in {difference.ToString(@"hh\:mm\:ss\:fff")}");
@AnshulKuthiala
AnshulKuthiala / PopulateIf.cs
Last active November 25, 2018 04:21
[Populate if condition is met] Checks for condition and returns data #ExcelHelper
public static object PopulateIf<T>(this T field, bool condition, object valueIfFalse = null)
{
if (condition)
{
return field;
}
else
{
return valueIfFalse;
}
@AnshulKuthiala
AnshulKuthiala / InitializeStatusForm.cs
Last active December 17, 2018 14:16
[Status update through form] Can update status for both Async and sync tasks
public static IProgress<Tuple<string, string>> InitializeStatusForm(out FormShowStatus statusForm)
{
statusForm = new FormShowStatus();
statusForm.Show();
return statusForm.Status;
}
@AnshulKuthiala
AnshulKuthiala / CutCopyMode.cs
Created November 17, 2018 19:38
[CutCopyMode = false] #ExcelHelper
ExcelHelper.xlApp.CutCopyMode = 0;
@AnshulKuthiala
AnshulKuthiala / XIRR.cs
Created November 11, 2018 10:29
[XIRR for C#]
public class XIRR
{
private const Double DaysPerYear = 365.0;
private const int MaxIterations = 100;
private const double DefaultTolerance = 1E-6;
private const double DefaultGuess = 0.1;
private static readonly Func<IEnumerable<CashItem>, Double> NewthonsMethod =
cf => NewtonsMethodImplementation(cf, Xnpv, XnpvPrime);
@AnshulKuthiala
AnshulKuthiala / NumbersToWords.cs
Created November 10, 2018 07:39
[NumbersToWords]
using System;
using System.Collections.Generic;
using System.Text;
public static class NumbersToWords
{
public static string ConvertNumberToWords(decimal value, bool appendOnly = false, string majorCurrency = "rupees", string minorCurrency = "paisa")
{
if (value < 0 || value > decimal.MaxValue)
@AnshulKuthiala
AnshulKuthiala / Levenshtein.cs
Created November 10, 2018 07:38
[Levenshtein C#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Levenshtein
{
/// <summary>
/// Computes the Damerau-Levenshtein Distance between two strings, represented as arrays of
@AnshulKuthiala
AnshulKuthiala / Gmail.cs
Created November 10, 2018 07:37
[Gmail API] Access Gmail Api from C#
using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;