Skip to content

Instantly share code, notes, and snippets.

@zhangw
zhangw / gist:4248779
Created December 10, 2012 06:17
Truncate String
//truncate string
function strTruncate(str, n) {
var r = /[^\x00-\xff]/g; //the chinese char,or r = [\u4e00-\u9fa5];
if (str.replace(r, "mm").length > n) {
var m = Math.floor(n / 2);
for (var i = m, l = str.length; i < l; i++) {
var _l = str.substr(0, i).replace(r, "mm").length;
if (_l >= n) {
return str.substr(0, i + n - _l) + "...";
}
@zhangw
zhangw / gist:4248786
Created December 10, 2012 06:18
Truncate String
/// <summary>
/// 根据字符长度截断字符串
/// </summary>
/// <param name="str">
/// 需要截断的字符串
/// </param>
/// <param name="maxLength">
/// 字符串允许的最大长度(包含的byte数)
/// </param>
/// <param name="isDetectChinese">
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
namespace TestCoreService
{
public class FackIObjectSet<T> : IObjectSet<T>
where T : class
{
public static IDictionary<int, string> EnumToIDictionary<T>() where T : struct,IConvertible
{
if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type");
IDictionary<int, string> dictionary = new Dictionary<int, string>();
foreach (var enumobj in (T[])Enum.GetValues(typeof(T)))
{
string text = enumobj.ToString();
int value = enumobj.ToInt32(null);
dictionary.Add(value, text);
}
@zhangw
zhangw / compress-js-batch.ps1
Created December 27, 2012 09:14
use google-closure to compress js files with powershell in batches.
$filepath="C:\SourceCode\FCSLN\FCProperty\FCProperty\Content"
$compile="C:\Temp\google-closure\compiler.jar"
$jsfiles = ls $filepath -Recurse -Include *.js -Exclude *.min.js
Foreach($js in $jsfiles){$i=$js.FullName.LastIndexOf(".js")
$compressjs= $js.FullName.Remove($i)+".min.js"
java -jar $compile --compilation_level SIMPLE_OPTIMIZATIONS --js_output_file $compressjs $js.FullName
}
ls $filepath -Recurse -Include *.min.js
@zhangw
zhangw / cell.cs
Created January 22, 2013 04:50
Use openXML to set the cell value in excel.
Cell theCell = worksheetPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == addressName).FirstOrDefault();
if (theCell != null)
{
if (theCell.DataType != null && theCell.DataType == CellValues.SharedString)
{
var sharedStringTable = document.WorkbookPart.SharedStringTablePart.SharedStringTable;
var element = sharedStringTable.ChildElements[int.Parse(theCell.InnerText)];
if (string.IsNullOrEmpty(element.InnerText))
{
SharedStringItem sst = new SharedStringItem(new Text(text));
@zhangw
zhangw / gist:4601021
Last active June 13, 2022 18:51
Use openxml to create the new cellformat and apply it to cell. Use openxml to modify the cellformat of cell.
var styleSheet = document.WorkbookPart.WorkbookStylesPart.Stylesheet;
Fill fill = new Fill() { PatternFill = new PatternFill { ForegroundColor = new ForegroundColor { Rgb = fgcolor }, PatternType = PatternValues.Solid } };
styleSheet.Fills.Append(fill);
//or use the method Count(),not need to plus 1,but slowly.
var fid = styleSheet.Fills.Count++;
CellFormat cellFormat = null;
if (theCell.StyleIndex.HasValue)
{
var originalCellFormat = document.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.ToList()[(int)theCell.StyleIndex.Value] as CellFormat;
@zhangw
zhangw / gist:4617759
Created January 24, 2013 04:53
From the specific root dictionary,only include the spcific dicts,get the fullname of files using filter "CreationTime","LastWriteTime","!PSIsContainer" recursively.
$files = Get-Item -Path C:\SourceCode\PMDProj\PDMApplication\* -Include "bin","Scripts","Views","Content" `
| Get-ChildItem -Recurse -Force -Exclude "*.pdb" -ErrorAction:SilentlyContinue `
| Where {(($_.CreationTime -gt "2013-1-24") -or ($_.LastWriteTime -gt "2013-1-24")) -and !$_.PSIsContainer} `
| Select FullName
@zhangw
zhangw / gist:4617777
Created January 24, 2013 04:57
From the specific root dictionary,only include the spcific dicts,copy all the files to another dictionary and remove the specific files using filter "CreationTime","LastWriteTime","Extension" recursively.
Copy-Item -Recurse -Force -Path C:\SourceCode\PMDProj\PDMApplication\* -Include "bin","Scripts","Views","Content" C:\Users\Wen\Desktop\PMD
$items = Get-ChildItem -Path C:\Users\Wen\Desktop\PMD\* -Recurse -Force -ErrorAction:SilentlyContinue `
| Where {(($_.CreationTime -lt "2013-1-24") -or ($_.LastWriteTime -lt "2013-1-24 09:00:00") -or ($_.Extension -eq ".pdb")) -and !$_.PSIsContainer}
ForEach($item in $items)
{$item.Delete()
}
@zhangw
zhangw / gist:4653642
Created January 28, 2013 07:25
SqlSever Trigger For Insert/Delete/Update example.
ALTER TRIGGER [dbo].[CM_ApprovalMatrix_Settings_Trigger]
ON [dbo].[CM_ApprovalMatrix_Settings]
FOR INSERT,DELETE,UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @del INT,@ins INT,@status NVARCHAR(50)
SET @del = (SELECT COUNT(ID) FROM deleted)