2018年4月18日 星期三

DOS: 取得字串變數內的子字串

取得字串變數內的子字串
格式 %var:~m,n%
var 表字串變數名稱
m 為正值表前端要位移的字元數, 若 m 為負值則表由後面開始要選取的字元數(m=0表全部選取)
n 為經過前段處理(m 值的選取)後, 再選取的字元數, 若 n 為正值表選取字元的長度, 若 n 為負值則表由後面要刪除的字元數(n=0 表選取長度為0, 即不選取之意)

範例
設定參數
set line=1234567890ABCDEF

1. 位移10字元(~10)後取的 5 個字元
D:\>echo %line:~10,5%
ABCDE

2. 取字串最後10個字元
D:\>echo %line:~-10%
7890ABCDEF

3. 前端位移3個字元, 後端刪除4個字元
D:\>echo %line:~3,-4%
4567890AB

4. 先取後10個字元後, 再取4個長度的字元
D:\>echo %line:~-10,4%
7890

5. 先取後10個字元後, 刪除最後4個字元
D:\>echo %line:~-10,-4%
7890AB

2017年2月6日 星期一

VBA: 常用功能

## Application 級的功能
  ' 執行時停止螢幕異動
  Application.ScreenUpdating = False

## 目前工作資料取得
  ' 取得目前活頁簿的工作目錄及名稱
  ActiveWorkbook.Path
  ActiveWorkbook.Name
  ' 取得目前執行VBS的活頁簿其工作目錄及名稱
  ThisWorkbook.Path
  ThisWorkbook.Name

  ' 取得活頁簿的工作目錄(檔案路徑)
  strNowPath = Excel.ActiveWorkbook.Path
 
  ' 取得目前工作頁的名稱
  szName = ThisWorkbook.ActiveSheet.Name
  ' 取得目前資料格的 row 值
  endRow = ActiveCell.Row
  ' 取得 VBA 程式所在工作表內特定工作表的資料格內容
  A = ThisWorkbook.Worksheets(strNameSheet).Cells(1, 1)

## 指定目前的有效視窗
  Windows("D:\test\myfile.xls").Activate

## 新增指定名稱的工作表
  Sheets.Add().Name = "TestOK"
  Worksheets.Add(After:=Worksheets("ReadMe")).Name = "MySheet"

## 取得目前所有工作表的名稱
方式一
  For Each thisSheet In Sheets
    thisSheet.Name  的內容即為工作表名稱
  Next thisSheet
方式二
  For i = 1 To Worksheets.Count
    Worksheets(i).Name 的內容即為工作表名稱
  Next

## 刪除作用中的工作表
  '關閉警告視窗
  Application.DisplayAlerts = False
  '刪除作用中的工作表
  ActiveSheet.Delete
  '恢復警告視窗
  Application.DisplayAlerts = True
 

## 與工作表相關的指令
  ' 設定工作表是否可見
  Worksheets(toSheet).Visible = True
  ' 設定為作用中的工作表
  Worksheets(toSheet).Activate
  ' 選取工作表
  Worksheets(toSheet).Select
  ' 刪除工作表資料內容
  Worksheets(toSheet).Cells.Delete
  ' 選取目前活動工作表所有資料格並複製到剪貼簿
  ActiveSheet.Cells.Select
  Selection.Copy
  ' 將前項剪貼簿內容貼到目前活動工作表資料格內
  ' 請注意複製與貼上所選取的範圍宜相同
  ActiveSheet.Cells.Select
  ActiveSheet.Paste
  ' 設定欄寬
  Columns("A:A").Select
  Selection.ColumnWidth = 20
  或
  Columns("A:A").ColumnWidth = 20
  ' 隱藏工作表
  Worksheets("mySheet").Visible = False

## 讀取或設定資料格的方式
  m_year1 = Worksheets("Setting").Cells(5, 2)
  Worksheets("Setting").Cells(5, 2) = 1234
  ' 設定資料格內容為公式
  Sheets("總表_每日").Cells(myRow, 2).Formula = "=DSUM(DB_Open!$A:$E,5,$" & col(ii - 1) & "$2:$" & col(ii - 1) & "$3)"


## 找到有資料列的最後一列
  Range("A65530").Select
  '跳到該欄最後一列有資料的地方
  Selection.End(xlUp).Select
  'endRow 為最後一列有資料的列數
  endRow = ActiveCell.Row

VBA: 檔案處理

## 檔案處理
## 開啟檔案  
  Workbooks.Open Filename:= _
    "D:\20140325\test.xls"

## 關閉檔案  
Windows(szOriginalFileName).Activate
ActiveWindow.Close False

## 使用循序模式寫入檔案()
Open "MyFile.txt" For Output As #1
For inputData = 1 To 4
Print #1, inputData
Next inputData
Close #1


## 使用循序模式讀取檔案
EX1.
Open "MyFile.txt" For Input As #1
For retu = 1 To 4
Input #1, x
Cells(1, retu) = x
Next retu
Close #1

EX2.
' Open the file for Input.
Open "TEXTFILE.TXT" For Input As #1
' Read each line of the text file into a single string
' variable.
Do While Not EOF(1)
   Line Input #1, LineofText
   MsgBox LineofText
Loop
' Close the file.
Close #1



## 讀取特定目錄內的檔名
Private Sub readFileName()
    Dim szWorkPath As String   ' 目前工作檔案目錄
    Dim szFileName As String   ' 讀到的檔案名稱
    Dim szDirString As String  ' DIR 搜尋檔案的字串
    szWorkPath = ThisWorkbook.Path
    szDirString = szWorkPath & "\data\*.pdf"
    n = 0
    'Sheet3.Cells.Delete  '將之前的結果清除
    szFileName = Dir(szDirString, vbDirectory)
    szData = ""
    While szFileName <> ""
      n = n + 1
        szData = szData + "##" + szFileName
      szFileName = Dir() '讀取下一個檔案
    Wend
    MsgBox (n & szData)
End Sub

## 執行功能前的確認視窗
Dim szYesNo
szYesNo = MsgBox("確定執行發送功能 ?", vbYesNo, "發送視窗")
If szYesNo = vbYes Then
  MsgBox "Hello"
Else
  MsgBox "Naff Off"
End If

VBA: 程式(控制流程與回圈)

VBA 控制流程與回圈
分享: 7Headlines facebook PLURK twitter
1. If-Then

If 條件 Then
    敘述
End If

2. If-Then-Else

If 條件 Then
    敘述
Else
    敘述
End If

3. If-Then-ElseIf-Else

If 條件 Then
    敘述
ElseIf 條件
    敘述
ElseIf 條件
    敘述
Else
    敘述
End If

4. Select Case

Select Case 變數
Case 變數值 [to 變數值]
    程式敘述一
    …….
Case 變數值 [to 變數值]
    程式敘述二
    …….
[Case 變數值 [to 變數值]
    程式敘述n-1
    …….]
[Case Else
    程式敘述n
    …….]
End Select

5. Do While ...Loop

Do While 條件
    敘述
Loop

6. Do Until… Loop

Do Until 條件
     敘述
Loop

7. Do … Loop While

Do
      敘述
Loop While 條件

8. Do … Loop Until

Do
      敘述
Loop Until 條件

9. Do … Loop

Do
    敘述
Loop

10. For … Next

For 數值變數 = 初始值 To 終止值 [Step 增量]
    敘述
Next [數值變數]

11. For Each … Next

For Each 元素 In 群組
    敘述
Next [元素]

12. While … Wend

While 條件
    敘述
Wend

13. 跳離指令

*Exit Do:強制離開Do Loop迴圈。
*Exit For:強制離開For Next迴圈。
*Exit Sub:強制跳離Sub程序。
*Exit Function:強制跳離Function程序。
*Goto:需在程式碼前加上行號。
*End:無條件結束應用程式。

VBA: function

VBA Function What It Does
Abs Returns the absolute value of a number
Array Returns a variant that contains an array
Asc Converts the first character of string to its ASCII value
Atn Returns the arctangent of a number
CBool Converts an expression to Boolean data type
CByte Converts an expression to byte data type
CCur Converts an expression to currency data type
CDate Converts an expression to date data type
CDbl Converts an expression to double data type
CDec Converts an expression to decimal data type
Choose Selects and returns a value from a list of arguments
Chr Converts an ANSI value to a character
CInt Converts an expression to integer data type
CLng Converts an expression to long data type
Cos Returns the cosine of a number
CreateObject Creates an OLE Automation object
CSng Converts an expression to single data type
CStr Converts an expression to string data type
CurDir Returns the current path
CVar Converts an expression to variant data type
CVDate Converts an expression to date data type
CVErr Returns a user-defined error type
Date Returns the current system date
DateAdd Returns a date with a specific date interval added to it
DateDiff Returns the difference between two dates as a time interval
DatePart Returns an integer containing a specific part of a date
DateSerial Returns a date for a specified year, month, and day
DateValue Converts a string to date
Day Returns the day of the month of a date
Dir Returns the name of a file or directory that matches a pattern
DoEvents Yields execution so the operating system can process other events
EOF Returns True if the end of a text file has been reached
Error Returns the error message that corresponds to an error number
Exp Returns the base of the natural logarithms (e) raised to a power
FileAttr Returns the file mode for a text file
FileDateTime Returns the date and time when a file was last modified
FileLen Returns the number of bytes in a file
Fix Returns the integer portion of a number
Format Returns an expression in a particular format
FormatCurrency Returns a number as a string, formatted as currency
FormatDateTime Returns a number as a string, formatted as a date and/or time
FormatNumber Returns a number as a formatted string
FormatPercent Returns a number as a string, formatted as a percentage
FreeFile Returns the next file number available for use by the Open statement
GetAll Returns a list of key settings and their values (originally created with SaveSetting) from an application’s entry in the Windows registry
GetAttr Returns a code representing a file attribute
GetObject Retrieves an OLE Automation object from a file
GetSetting Returns a key setting value from an application’s entry in the Windows registry
Hex Converts from decimal to hexadecimal
Hour Returns the hour of a time
IIf Returns one of two parts, depending on the evaluation of an expression
Input Returns a specific number of characters from an open text file
InputBox Displays a box to prompt a user for input, and returns the value entered
InStr Returns the position of a string within another string
InStrRev Returns the position of a string within another string, beginning at the back end of the string
Int Returns the integer portion of a number
IsArray Returns True if a variable is an array
IsDate Returns True if a variable is a date
IsEmpty Returns True if a variable has not been initialized
IsError Returns True if an expression is an error value
IsMissing Returns True if an optional argument was not passed to a Procedure
IsNull Returns True if an expression contains no valid data
IsNumeric Returns True if an expression can be evaluated as a number
IsObject Returns True if an expression references an OLE Automation object
Join Returns a string created by joining a number of substrings contained in an array
LBound Returns the lower bound of an array
LCase Returns a string converted to lowercase
Left Returns a specified number of characters from the left of a string
Len Returns the length of a string, in characters
Loc Returns the current read or write position of a text file
LOF Returns the number of bytes in an open text file
Log Returns the natural logarithm of a number
LTrim Returns a copy of a string with no leading spaces
Mid Returns a specified number of characters from a string
MidB Returns a specified number of bytes from a specified position in a string string
Minute Returns the minute of a time
Month Returns the month of a date
MonthName Returns a string indicating the specified month
MsgBox Displays a modal message box and returns the ID of the button clicked
Now Returns the current system date and time
Oct Converts from decimal to octal
Replace Returns a string in which one substring is replaced with another
RGB Returns a number representing an RGB color value
Right Returns a specified number of characters from the right of a string
Rnd Returns a random number between 0 and 1
Round Rounds a number to a specific number of decimal places
RTrim Returns a copy of a string with no trailing spaces
Second Returns the second of a time
Seek Returns the current position in a text file
Sgn Returns an integer that indicates the sign of a number
Shell Runs an executable program
Sin Returns the sine of a number
Space Returns a string with a specified number of spaces
Split Returns an array consisting of a number of substrings
Sqr Returns the square root of a number
Str Returns a string representation of a number
StrComp Returns a value indicating the result of a string comparison
StrConv Returns a string variant converted as specified
String Returns a repeating character or string
StrReverse Returns the characters of a string in reverse order
Switch Evaluates a list of expressions and returns a value associated with the first expression in the list that is True
Tab Positions output in an output stream
Tan Returns the tangent of a number
Time Returns the current system time
Timer Returns the number of seconds since midnight
TimeSerial Returns the time for a specified hour, minute, and second
TimeValue Converts a string to a time serial number
Trim Returns a string without leading and spaces and replaces multiple spaces with a single space
TypeName Returns a string that describes the data type of a variable
UBound Returns the upper bound of an array
UCase Converts a string to uppercase
Val Returns the numbers contained in a string
VarType Returns a value indicating the subtype of a variable
Weekday Returns a number representing a day of the week
Weekday Name Returns a string indicating the specified weekday
Year Returns the year of a date

2016年6月15日 星期三

CSS: 選擇器

CSS3 選擇器

在 CSS 中,選擇器是一種模式,用於選擇需要添加樣式的元素。
"CSS" 列指示該屬性是在哪個 CSS 版本中定義的。(CSS1、CSS2 還是 CSS3。)
選擇器 例子 例子描述 CSS
.class .intro 選擇 class="intro" 的所有元素。 1
#id #firstname 選擇 id="firstname" 的所有元素。 1
* * 選擇所有元素。 2
element p 選擇所有 <p> 元素。 1
element,element div,p 選擇所有 <div> 元素和所有 <p> 元素。 1
element element div p 選擇 <div> 元素內部的所有 <p> 元素。 1
element>element div>p 選擇父元素為 <div> 元素的所有 <p> 元素。 2
element+element div+p 選擇緊接在 <div> 元素之後的所有 <p> 元素。 2
[attribute] [target] 選擇帶有 target 屬性所有元素。 2
[attribute=value] [target=_blank] 選擇 target="_blank" 的所有元素。 2
[attribute~=value] [title~=flower] 選擇 title 屬性包含單詞 "flower" 的所有元素。 2
[attribute|=value] [lang|=en] 選擇 lang 屬性值以 "en" 開頭的所有元素。 2
:link a:link 選擇所有未被訪問的鏈接。 1
:visited a:visited 選擇所有已被訪問的鏈接。 1
:active a:active 選擇活動鏈接。 1
:hover a:hover 選擇鼠標指針位於其上的鏈接。 1
:focus input:focus 選擇獲得焦點的 input 元素。 2
:first-letter p:first-letter 選擇每個 <p> 元素的首字母。 1
:first-line p:first-line 選擇每個 <p> 元素的首行。 1
:first-child p:first-child 選擇屬於父元素的第一個子元素的每個 <p> 元素。 2
:before p:before 在每個 <p> 元素的內容之前插入內容。 2
:after p:after 在每個 <p> 元素的內容之後插入內容。 2
:lang(language) p:lang(it) 選擇帶有以 "it" 開頭的 lang 屬性值的每個 <p> 元素。 2
element1~element2 p~ul 選擇前面有 <p> 元素的每個 <ul> 元素。 3
[attribute^=value] a[src^="https"] 選擇其 src 屬性值以 "https" 開頭的每個 <a> 元素。 3
[attribute$=value] a[src$=".pdf"] 選擇其 src 屬性以 ".pdf" 結尾的所有 <a> 元素。 3
[attribute*=value] a[src*="abc"] 選擇其 src 屬性中包含 "abc" 子串的每個 <a> 元素。 3
:first-of-type p:first-of-type 選擇屬於其父元素的首個 <p> 元素的每個 <p> 元素。 3
:last-of-type p:last-of-type 選擇屬於其父元素的最後 <p> 元素的每個 <p> 元素。 3
:only-of-type p:only-of-type 選擇屬於其父元素唯一的 <p> 元素的每個 <p> 元素。 3
:only-child p:only-child 選擇屬於其父元素的唯一子元素的每個 <p> 元素。 3
:nth-child(n) p:nth-child(2) 選擇屬於其父元素的第二個子元素的每個 <p> 元素。 3
:nth-last-child(n) p:nth-last-child(2) 同上,從最後一個子元素開始計數。 3
:nth-of-type(n) p:nth-of-type(2) 選擇屬於其父元素第二個 <p> 元素的每個 <p> 元素。 3
:nth-last-of-type(n) p:nth-last-of-type(2) 同上,但是從最後一個子元素開始計數。 3
:last-child p:last-child 選擇屬於其父元素最後一個子元素每個 <p> 元素。 3
:root :root 選擇文檔的根元素。 3
:empty p:empty 選擇沒有子元素的每個 <p> 元素(包括文本節點)。 3
:target #news:target 選擇當前活動的 #news 元素。 3
:enabled input:enabled 選擇每個啟用的 <input> 元素。 3
:disabled input:disabled 選擇每個禁用的 <input> 元素 3
:checked input:checked 選擇每個被選中的 <input> 元素。 3
:not(selector) :not(p) 選擇非 <p> 元素的每個元素。 3
::selection ::selection 選擇被用戶選取的元素部分。 3

2016年4月28日 星期四

Android: 繪圖

## 簡單圖形繪製
protected void onDraw(Canvas canvas) {//重寫的繪製方法
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);//繪製黑色背景
Paint paint = new Paint();//建立畫筆
paint.setColor(Color.WHITE);//設置畫筆顏色為紅色
canvas.drawRect(10, 10, 110, 110, paint);//繪製矩形
canvas.drawText("這是字符串", 10, 130, paint);//字符串,以字符串下面為基準
RectF rf1 = new RectF(10, 130, 110, 230);//定義一個矩形
canvas.drawArc(rf1, 0, 45, true, paint);//畫弧,順時針
canvas.drawLine(150, 10, 250, 110, paint);//畫線
RectF rf2 = new RectF(150, 130, 250, 230);//定義一個矩形
canvas.drawOval(rf2, paint);//畫圓 
}

## 貼圖功能
public void initBitmap(){ 
paint = new Paint();//建立一個畫筆
myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img);//獲得圖片資源
}
@Override
protected void onDraw(Canvas canvas) {//重寫的繪製方法
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setAntiAlias(true);//打開抗鋸齒
paint.setColor(Color.WHITE);//設置畫筆的顏色
paint.setTextSize(15);
canvas.drawBitmap(myBitmap, 10, 10, paint);//繪製圖片
canvas.save();  //儲存畫布
Matrix m1=new Matrix();
m1.setTranslate(500, 10);  // 平移
Matrix m2=new Matrix();
m2.setRotate(15);  // 旋轉
Matrix m3=new Matrix();
m3.setConcat(m1, m2);  
m1.setScale(0.8f, 0.8f);
m2.setConcat(m3, m1);
canvas.drawBitmap(myBitmap, m2, paint);
canvas.restore();
canvas.save();
paint.setAlpha(180);
m1.setTranslate(200,100);
m2.setScale(1.3f, 1.3f);
m3.setConcat(m1, m2);
canvas.drawBitmap(myBitmap, m3,  paint);
paint.reset();
canvas.restore();
paint.setTextSize(40);
paint.setColor(0xffFFFFFF);
canvas.drawText("圖片的寬度: "+myBitmap.getWidth(), 20, 380, paint);//繪製字符串,圖片的寬度
canvas.drawText("圖片的高度: "+myBitmap.getHeight(), 20, 430, paint);//繪製字符串,圖片的高度
paint.reset();
}