4、Snapman多人協做電子表格之——Exprtk腳本

Snapman多人協做電子表格是一個即時工做系統。java

Snapman中嵌入了Exprtk腳本進行公式數據運算。Exprtk是一種高性能的腳本,經測試它的數據運算性能只比C#和java底20%。express

 

1、Exprtk的類型和變量定義數組

  Exprtk的數據類型只有三種:Scalar(數字), Vector(數字數組), String(字符串);特別說明:Exprtk沒有字符串數組less

  基本的運算符有:+, -, *, /, %, ^ide

  賦值運算符有::=, +=, -=, *=, /=, %=函數

  判斷運算符有:=, ==, <>, !=, <, <=, >, >=性能

  邏輯運算符有:and, mand, mor, nand, nor, not, or, shl, shr,xnor, xor, true, false測試

  字符串運算符有:in, like, ilike, concatenationspa

  Exprtk的變量定義爲:操作系統

var iData := 100;          //數字變量定義
var sData := 'Hello word.';    //字符串變量定義
var vData[4] := {1,2,3,4};     //數組變量定義

   Snapman中Exprtk定義了四種特殊變量:

  一、單元格數字變量,列字母+行號數字,如:A1H234

  二、單元格字符串變量,列字母+行號數字+下劃線,如:A1_H234_

  三、選擇集數組,列字母+行號數字+冒號+列字母+行號數字,這是一個數字數組元素次序是按單元格從左往右從上往下,如:F2:G5

  四、列數組,列字母+冒號+列字母,這是一個數字數組元素次序是按單元格從左往右從上往下,如:F:H

  因此Snapman中Exprtk的變量定義都不要使用數字結尾或數字加下劃線結尾

2、Exprtk的if、switch、for、while、continue、break語句

  一、if語句的類型

    A、if表達式:if (x, y, z) ;若是x爲真返回y不然返回z;例如:

var x := 100;
var y := 300;
var z := 500;
var w := 58.7;
var v := 32;
var data := if ((x + 1) > 2y, z + 1, w / v) ;
print(tostring(data));

    B、if語句塊

if (x < y or (x + z) > y)
{
    z := x + 3;
    y := x - z;
}
else if (abs(2y - z) >= 3)
    y := x - z;
else
{
    z := abs(x * x);
    x * y * z;
};

  二、switch語句的類型

    switch語句會逐句判斷case的條件,成立就執行代碼表達式,若是case條件都不成立就執行default的代碼表達式,最後將代碼表達式的結果返回,如:

var x := 100;
var y := 300;
var z := 500;
var a := switch
{
case x > (y + z) : 2 * x / abs(y - z);
case x < 3       : sin(x + y);
default          : 1 + x;
};
print(tostring(a));

    注意:case的條件和語句只容許有一條語句

  三、for語句

  for語句和C語言相似,可是它是一個表達式因此記住大括號後面須要有分號,如:

for (var x := 0; (x < n) and (x != y); x += 1)
{
  y := y + x / 2 - z;
  w := u + y;
};

  四、while語句

  while語句和C語言相似,但它是一個表達式因此記住大括號後面須要有分號,如:

while ((i += 1) < 10)
{                    
  if (i < 5)         
    j -= i + 2;      
  else if (i % 2 == 0)
    break;           
  else               
    break[2i + 3];   
};

  五、continue、break、break[]語句

  continue、break和其C語言相似,break[]的意思是for和while語句是一條有返回值的表達式,break返回的是NaN,而break[]倒是返回中括號中表達式的值,如上一個樣例。

3、Exprtk的數組運算

  Exprtk有一個很是強大的數組運算,能夠對數組變量進行批量運算和賦值

  一、賦值運算符都支持數組變量,以下面樣例數組中每一個變量都增長了13:

var vData[5] := {1, 2, 3, 4, 5};
vData += 13;

  二、其餘支持數組的運算符:+, -, *, /, %, ^

  三、支持的函數:avg, max, min, mul, sum, count

  四、當存在2個數組變量的時候,運算以個數少的爲運算次數;當運算符爲單維運算符或者單維函數,數組變量會取第一個元素進行運算

4、Exprtk的Snapman函數接口

  一、tolower(str),將字符串str轉換成小寫字母並返回,例如:e2_:=tolower('AHFGHJHKJDewiuri73972389HHJGJG中華人民共和國')
  二、toupper(str),將字符串str轉換成大寫字母並返回,例如:e3_:=toupper('AHFGHJHKJDewiuri73972389HHJGJG中華人民共和國')
  三、tostring(double),將數字轉換成字符串並返回,例如:e4_:=tostring(0.4444488)
  四、tonumber(str),將字符串轉換成數字並返回,例如:e5:=tonumber('5643786587')
  五、get(row,col),獲取第row行、col列的單元格的數字,例如:e6:=get(7,1)
  六、set(row,col,double),設置第row行、col列的單元格的數字,例如:set(7,5,98766)
  七、get_(row,col),獲取第row行、col列的單元格的字符串,例如:e8_:=get_(8,2)
  八、set_(row,col,str),設置第row行、col列的單元格的字符串,例如:set_(9,5,'大不了的給開發商')
  九、set_progress(n),設置進度條,n必須大於等於0,例如:set_progress(10)
  十、set_total_progress(t),設置進度條的最大值,t必須大於0,例如:set_total_progress(100)
  十一、sleep(n),讓線程睡眠n毫秒,例如:sleep(1000)
  十二、get_tick_count(),獲取從操做系統啓動所通過的毫秒數,例如:E12:=get_tick_count()
  1三、save(),保存當前電子表格,例如:save()
  1四、count(vec),獲取數組vec的個數,例如:E14:=count(C4:C11)
  1五、print(str),打印str的內容,例如:print('Hello world.')
  1六、set_background(row,col,str_lafer),設置第row行、col列的單元格背景的lafer,例如:set_background(16,5,'[[18,#9845f2,8,8,8],[18,#F865f2,16,55,8]]')

  17,get_row_count(),獲取當前表格有內容的單元格行數,例如:set(18,5,get_row_count())
  18,get_col_count(),獲取當前表格有內容的單元格列數,例如:set(19,5,get_col_count())

  1九、E17或者e17,表格數據變量:字母表示列,數字表示行,例如:E17 := 100000.567;
  20、E18_或者e18_,表格字符串變量:字母表示列,數字表示行,例如:E18_ := '我是中國人。'
  2一、E19:E21或者A:B,表格數據變量數組,先從左到右而後從上到下,沒有數字表明從某列到某列,例如:E19:E21 := A17:A19

5、Exprtk自帶的函數

    +----------+---------------------------------------------------------+
    | FUNCTION | DEFINITION                                              |
    +----------+---------------------------------------------------------+
    | abs      | Absolute value of x.  (eg: abs(x))                      |
    +----------+---------------------------------------------------------+
    | avg      | Average of all the inputs.                              |
    |          | (eg: avg(x,y,z,w,u,v) == (x + y + z + w + u + v) / 6)   |
    +----------+---------------------------------------------------------+
    | ceil     | Smallest integer that is greater than or equal to x.    |
    +----------+---------------------------------------------------------+
    | clamp    | Clamp x in range between r0 and r1, where r0 < r1.      |
    |          | (eg: clamp(r0,x,r1))                                    |
    +----------+---------------------------------------------------------+
    | equal    | Equality test between x and y using normalised epsilon  |
    +----------+---------------------------------------------------------+
    | erf      | Error function of x.  (eg: erf(x))                      |
    +----------+---------------------------------------------------------+
    | erfc     | Complimentary error function of x.  (eg: erfc(x))       |
    +----------+---------------------------------------------------------+
    | exp      | e to the power of x.  (eg: exp(x))                      |
    +----------+---------------------------------------------------------+
    | expm1    | e to the power of x minus 1, where x is very small.     |
    |          | (eg: expm1(x))                                          |
    +----------+---------------------------------------------------------+
    | floor    | Largest integer that is less than or equal to x.        |
    |          | (eg: floor(x))                                          |
    +----------+---------------------------------------------------------+
    | frac     | Fractional portion of x.  (eg: frac(x))                 |
    +----------+---------------------------------------------------------+
    | hypot    | Hypotenuse of x and y (eg: hypot(x,y) = sqrt(x*x + y*y))|
    +----------+---------------------------------------------------------+
    | iclamp   | Inverse-clamp x outside of the range r0 and r1. Where   |
    |          | r0 < r1. If x is within the range it will snap to the   |
    |          | closest bound. (eg: iclamp(r0,x,r1)                     |
    +----------+---------------------------------------------------------+
    | inrange  | In-range returns 'true' when x is within the range r0   |
    |          | and r1. Where r0 < r1.  (eg: inrange(r0,x,r1)           |
    +----------+---------------------------------------------------------+
    | log      | Natural logarithm of x.  (eg: log(x))                   |
    +----------+---------------------------------------------------------+
    | log10    | Base 10 logarithm of x.  (eg: log10(x))                 |
    +----------+---------------------------------------------------------+
    | log1p    | Natural logarithm of 1 + x, where x is very small.      |
    |          | (eg: log1p(x))                                          |
    +----------+---------------------------------------------------------+
    | log2     | Base 2 logarithm of x.  (eg: log2(x))                   |
    +----------+---------------------------------------------------------+
    | logn     | Base N logarithm of x. where n is a positive integer.   |
    |          | (eg: logn(x,8))                                         |
    +----------+---------------------------------------------------------+
    | max      | Largest value of all the inputs. (eg: max(x,y,z,w,u,v)) |
    +----------+---------------------------------------------------------+
    | min      | Smallest value of all the inputs. (eg: min(x,y,z,w,u))  |
    +----------+---------------------------------------------------------+
    | mul      | Product of all the inputs.                              |
    |          | (eg: mul(x,y,z,w,u,v,t) == (x * y * z * w * u * v * t)) |
    +----------+---------------------------------------------------------+
    | ncdf     | Normal cumulative distribution function.  (eg: ncdf(x)) |
    +----------+---------------------------------------------------------+
    | nequal   | Not-equal test between x and y using normalised epsilon |
    +----------+---------------------------------------------------------+
    | pow      | x to the power of y.  (eg: pow(x,y) == x ^ y)           |
    +----------+---------------------------------------------------------+
    | root     | Nth-Root of x. where n is a positive integer.           |
    |          | (eg: root(x,3) == x^(1/3))                              |
    +----------+---------------------------------------------------------+
    | round    | Round x to the nearest integer.  (eg: round(x))         |
    +----------+---------------------------------------------------------+
    | roundn   | Round x to n decimal places  (eg: roundn(x,3))          |
    |          | where n > 0 and is an integer.                          |
    |          | (eg: roundn(1.2345678,4) == 1.2346)                     |
    +----------+---------------------------------------------------------+
    | sgn      | Sign of x, -1 where x < 0, +1 where x > 0, else zero.   |
    |          | (eg: sgn(x))                                            |
    +----------+---------------------------------------------------------+
    | sqrt     | Square root of x, where x >= 0.  (eg: sqrt(x))          |
    +----------+---------------------------------------------------------+
    | sum      | Sum of all the inputs.                                  |
    |          | (eg: sum(x,y,z,w,u,v,t) == (x + y + z + w + u + v + t)) |
    +----------+---------------------------------------------------------+
    | swap     | Swap the values of the variables x and y and return the |
    | <=>      | current value of y.  (eg: swap(x,y) or x <=> y)         |
    +----------+---------------------------------------------------------+
    | trunc    | Integer portion of x.  (eg: trunc(x))                   |
    +----------+---------------------------------------------------------+
    | acos     | Arc cosine of x expressed in radians. Interval [-1,+1]  |
    |          | (eg: acos(x))                                           |
    +----------+---------------------------------------------------------+
    | acosh    | Inverse hyperbolic cosine of x expressed in radians.    |
    |          | (eg: acosh(x))                                          |
    +----------+---------------------------------------------------------+
    | asin     | Arc sine of x expressed in radians. Interval [-1,+1]    |
    |          | (eg: asin(x))                                           |
    +----------+---------------------------------------------------------+
    | asinh    | Inverse hyperbolic sine of x expressed in radians.      |
    |          | (eg: asinh(x))                                          |
    +----------+---------------------------------------------------------+
    | atan     | Arc tangent of x expressed in radians. Interval [-1,+1] |
    |          | (eg: atan(x))                                           |
    +----------+---------------------------------------------------------+
    | atan2    | Arc tangent of (x / y) expressed in radians. [-pi,+pi]  |
    |          | eg: atan2(x,y)                                          |
    +----------+---------------------------------------------------------+
    | atanh    | Inverse hyperbolic tangent of x expressed in radians.   |
    |          | (eg: atanh(x))                                          |
    +----------+---------------------------------------------------------+
    | cos      | Cosine of x.  (eg: cos(x))                              |
    +----------+---------------------------------------------------------+
    | cosh     | Hyperbolic cosine of x.  (eg: cosh(x))                  |
    +----------+---------------------------------------------------------+
    | cot      | Cotangent of x.  (eg: cot(x))                           |
    +----------+---------------------------------------------------------+
    | csc      | Cosecant of x.  (eg: csc(x))                            |
    +----------+---------------------------------------------------------+
    | sec      | Secant of x.  (eg: sec(x))                              |
    +----------+---------------------------------------------------------+
    | sin      | Sine of x.  (eg: sin(x))                                |
    +----------+---------------------------------------------------------+
    | sinc     | Sine cardinal of x.  (eg: sinc(x))                      |
    +----------+---------------------------------------------------------+
    | sinh     | Hyperbolic sine of x.  (eg: sinh(x))                    |
    +----------+---------------------------------------------------------+
    | tan      | Tangent of x.  (eg: tan(x))                             |
    +----------+---------------------------------------------------------+
    | tanh     | Hyperbolic tangent of x.  (eg: tanh(x))                 |
    +----------+---------------------------------------------------------+
    | deg2rad  | Convert x from degrees to radians.  (eg: deg2rad(x))    |
    +----------+---------------------------------------------------------+
    | deg2grad | Convert x from degrees to gradians.  (eg: deg2grad(x))  |
    +----------+---------------------------------------------------------+
    | rad2deg  | Convert x from radians to degrees.  (eg: rad2deg(x))    |
    +----------+---------------------------------------------------------+
    | grad2deg | Convert x from gradians to degrees.  (eg: grad2deg(x))  |
    +----------+---------------------------------------------------------+

 

6、Exprtk樣例

//一、各行求和
for(var row := get_row_count() - 3; row > 1; row := row - 1)
{
    var isum := 0;
    for(var col := get_col_count() - 2; col >= 1; col := col - 1)
    {
        isum += get(row,col);
    }
    set(row,get_col_count() - 1,isum);
};
//二、各列求和
for(var col := get_col_count() - 1; col >= 1; col := col - 1)
{
    var isum := 0;
    for(var row := get_row_count() - 3; row > 1; row := row - 1)
    {
        isum += get(row,col);
    }
    set(get_row_count() - 2,col,isum);
};

 

  Exprtk的具體語法,請參考:Exprtk語言語法說明

7、Snapman的下載地址

  Snapman下載的官網地址:http://www.snapman.xyz

  Snapman技術支持QQ羣:596654328

  

相關文章
相關標籤/搜索