Skip to content

Instantly share code, notes, and snippets.

View sebnilsson's full-sized avatar
☁️

Seb Nilsson sebnilsson

☁️
View GitHub Profile
@sebnilsson
sebnilsson / ConvertWordsToPdfs.cls
Last active August 22, 2020 03:48
VBA: Loop through all files in a directory and convert them to PDF-files
Sub ConvertWordsToPdfs()
Dim directory As String
directory = "C:\Wordup" ' The starting directory
Dim fso, folder, files
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(directory)
Set files = folder.files
@sebnilsson
sebnilsson / ValidRfc3986Chars.cs
Created October 4, 2011 14:54
Valid RFC 3986 Chars
// According to:
// http://www.ietf.org/rfc/rfc3986.txt
public static char[] ValidRfc3986Chars = new[] {
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9',
'-','.','_','~',':','/','?','#','[',']','@','!','$','&','\'','(',')','*','+',',',';','=',
};
@sebnilsson
sebnilsson / gist:1275714
Last active September 27, 2015 13:17
UrlHelper.ToAbsolute
public static string ToAbsolute(this UrlHelper urlHelper, string appRelativeUrl,
string relativeHostName = null, bool? useSecureConnection = null) {
var httpContext = urlHelper.RequestContext.HttpContext;
if(string.IsNullOrWhiteSpace(relativeHostName) && httpContext.Request == null) {
throw new ArgumentException("The provided host-name cannot be null or empty if there is no current HttpContext.", "relativeHostName");
}
relativeHostName = (relativeHostName ?? string.Empty).Trim(new[] { '/' });
if(!Uri.IsWellFormedUriString(relativeHostName, UriKind.Relative)) {
@sebnilsson
sebnilsson / gist:1709905
Created January 31, 2012 10:59
Init Google Maps via Javascript
// Needs reference to 'http://maps.google.com/maps/api/js?sensor=false'. Optional: '&language=en'.
// mapElement should contain a 'data-locations'-attribute with JSON-formatted objects
// with properties 'lat', 'long', 'title' and optional 'description'
var initMap = function (mapElement) {
var dataLocations = $(mapElement).attr('data-locations'); // Find DOM-attribute containing locations
var markerPositions = ($.parseJSON(dataLocations) || { locations: [] }).locations || [];
var latlng = new google.maps.LatLng(59.33279, 18.06449);
@sebnilsson
sebnilsson / gist:1759713
Created February 7, 2012 13:38
Load jQuery async
// Load jQuery
(function () {
var jq = document.createElement('script'); jq.type = 'text/javascript'; jq.async = true;
jq.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(jq, s);
})();
@sebnilsson
sebnilsson / RenderSectionWebPageBaseExtensions.cs
Last active September 30, 2015 19:47
RenderSection with Default Content
using System;
using System.Web.WebPages;
public static class WebPageBaseExtensions
{
public static HelperResult RenderSection(this WebPageBase page, string sectionName, Func<dynamic, HelperResult> defaultContents)
{
if (page.IsSectionDefined(sectionName))
{
return page.RenderSection(sectionName);
var timeoutId = 0;
var scrollEvent = function() {
alert('Scroll event was fired');
};
$(window).scroll(function () {
clearTimeout(timeoutId );
timeoutId = setTimeout(scrollEvent, 500);
});
@sebnilsson
sebnilsson / gist:3832965
Last active October 11, 2015 08:47
ASP.NET MVC Detect If Index Page
string currentAction = Convert.ToString(this.ViewContext.Controller.ValueProvider.GetValue("action").RawValue);
string currentController = Convert.ToString(this.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue);
bool isIndexPage = currentAction.Equals("index", StringComparison.InvariantCultureIgnoreCase)
&& currentController.Equals("home", StringComparison.InvariantCultureIgnoreCase);
@sebnilsson
sebnilsson / sort-and-remove-duplicates.js
Last active October 11, 2015 11:28
Sort and remove duplicates
var items = [ 'ABC', '123', 'BAA' ];
items.sort(function (a, b) {
var aVal = a.val.toLowerCase();
var bVal = b.val.toLowerCase();
return ((aVal < bVal) ? -1 : ((aVal > bVal) ? 1 : 0));
});
var last = items[0];
for (var i = 1; i < items.length; i++) {
var item = items[i];
@sebnilsson
sebnilsson / is-number.js
Last active October 11, 2015 15:27
JavaScript isNumber / isNumberOrEmpty
var isNumber = function(number) {
return !isNaN(parseFloat(number)) && isFinite(number);
};
var isNumberOrEmpty = function(number) {
number = (number || 0).toString().replace(' ', '').replace(',', '.');
return !isNaN(parseFloat(number)) && isFinite(number);
};