Home / ... / Deluge スクリプト / 関数 / 関数 - サンプルコード

関数 - サンプルコード


関数 - サンプルコード


日数の計算  CalculateDays()

日付の形式  dateformat()

うるう年  IsLeapYear()

次のうるう年  nextLeapYear()

収支の計算  updateincome() & updateexpense()

メール通知  EmailNotification()

フィールド値の更新  <UpdateFieldValue>()



1. 日数の計算
       CalculateDays():
2つの与えられた日付間の日数を計算する関数です。この関数は、整数(integer)値を返します。

関数の定義: 

int Calculations.CalculateDays(date sdate, date edate) 
{
days = (((input.edate - input.sdate) / 86400000)).toLong();
return days;
}

ここで、

int - 関数によって返されるデータの型は、int(整数)型です。

Calculationsこの関数が作成されたネームスペースです。

CalculateDays - 日数計算の関数名です。

sdate - 日付(date)型の引数です。

edate - 日付(date)型の引数です。


関数の呼び出し:

CalculateDays 関数は、レコードの追加時(on add) -> 完了時(on success) スクリプトにおいて実行されます。
この関数によって返却された値で、フォーム内の
数字フィールド、Number_of_days が更新されます。

on  add
{
on success
{
input.Number_of_days = thisapp.Calculations.CalculateDays(input.Start_Date, input.End_Date);
}
}

 


2. 日付の形式
       dateformat() :
与えられた日付から、年月日を取得する関数です。
                             この関数は、入力された日付を、文字列(string)として "日/月/年" の形式で返します。 (例: 12/2/2008)

関数の定義

string dateformat.getAustralian(date inputdate)
{
month = inputdate.getMonth();
day = inputdate.getDay();
year = inputdate.getYear();
outputstr = day + "/" + month + "/" + year;
return outputstr;
}

 


3. うるう年
       isLeapYear()
: 与えられた年がうるう年(a leap year)かどうかをチェックする関数です。
                            与えられた年がうるう年の場合には、関数は論理型の値 'true' を返します。

関数の定義

bool isLeapYear(int year)
{
leapyear = false;
if ((input.year % 4) == 0)
{
leapyear = true;
if (((input.year % 100) == 0) && ((input.year % 100) != 0))
{
leapyear = false;
}
}
return leapyear;
}

 

4. 次のうるう年
       nextLeapYear()
:
与えられた年の次の年がうるう年(a leap year)かどうかをチェックする関数です。
                                 ここで、
この関数は、別の関数である isLeapYear() を呼び出しています。

Function Definition

int nextLeapYear(int year)
{
if (thisapp.isLeapYear((input.year + 1)))
{
return (input.year + 1);
}
else if (thisapp.isLeapYear((input.year + 2)))
{
return (input.year + 2);
}
else if (thisapp.isLeapYear((input.year + 3)))
{
return (input.year + 3);
}
else
{
return (input.year + 4);
}
}

 

5. 収支の計算
   updateincome() & updateexpense()

サンプルとして、収支アプリケーションをご紹介します。このアプリケーションは、次の3つのフォームから構成されます。:

- 収入フォーム(Income form) :収入の詳細を入力します。

- 支出フォーム(Expense form) :支出の詳細を入力します。

- 現在高フォーム(Amount in Hand form)  :1つのレコードを持ち、その時点での、収入の合計と支出の合計の差額を表示します。

 

さて、収入や支出が追加/修正/削除されたときはいつでも、収入合計と支出合計が更新され、その時点での現在高(Amount in hand)を計算する必要があります。
これを実現するには、レコード追加時(
on add)と編集時(on edit)と削除時(on delete)の完了時(success)スクリプトに、同じ文の一式を書き込まなくてはなりません。
文を関数として定義することによって、ただ1箇所にコードを書き込むだけで、必要な箇所でその関数を呼び出すことができます。
関数の定義、関数の呼び出しを含む、このアプリケーションの完全なスクリプト(.ds ファイル)は、収支アプリケーション をご参照ください。

 

関数の定義: updateincomeupdateexpense という名の関数を定義します。:

    functions
{
void updateincome()
{
x = 0;
for each rec in Income
{
x = (x + rec.Amount_of_Income);
}
aih = Amount_In_Hand [ID != 0];
aih.Total_Income = x;
}

void updateexpense()

{
x = 0;
for each rec in Expense
{
x = (x + rec.Expense_Amount);
}
aih = Amount_In_Hand [ID != 0];
aih.Total_Expense = x;
}

}

 

関数の呼び出し: updateincomeupdateexpense という関数が呼び出され、収入フォームと支出フォームの、フォームのアクション>レコード追加時(on add)と編集時(on edit)と削除時(on delete)のスクリプト から実行されます。


 

6. メール通知
       EmailNotification():
ビューで選択したレコードに対して、メールを送信する関数です。

関数の定義
 
void Email.EmailNotification(string toaddress)
{
sendmail
(
To : input.toaddress
From : zoho.adminuserid
Subject : "Subject of the email"
Message : "Your message"
)
}

 

カスタムアクション

EmailNotification 関数は、ビューの定義内でカスタムアクションとして設定されます。フォーム内の EmailId フィールドの値は、引数として渡されます。

custom actions
(
"Send Mail" : Email.EmailNotification(toaddress = EmailId)
)

 

7. フィールド値の更新
       <UpdateFieldValue>():
ビューで選択したレコードに対して、
SampleForm フォーム内の Travel_Status フィールドの値を "Confirmed" に更新する関数です。

関数の定義

void test.ConfirmTrip(int id)
{
rec = SampleForm [ID == input.id];
rec.Travel_Status = "Confirmed";
}

 

カスタムアクション

ConfirmTrip 関数は、ビューの定義内でカスタムアクションとして設定されます。
選択されたレコードの ID フィールドの値は、引数値として渡されます。

custom actions
(
"Confirm" : test.ConfirmTrip(id = ID)
)

 

注記: 上記はサンプルです。必要に応じて、フォーム名とフィールド名を変更し、ご利用ください。


 


    Post a comment

    Your Name or E-mail ID (mandatory)

     

    Note: Your comment will be published after approval of the owner.




     RSS of this page