LoginSignup
0
0

More than 3 years have passed since last update.

【UiPath】カスタムアクティビティを作成してみた_祝日判定②

Posted at

前回の記事
前回の記事で祝日判定のカスタムアクティビティをを紹介したのだが、エラーチェック
を追加するのを忘れていたのでコードを追加しようと思う。

仕様確認

日付を入力する箇所に文字列を入れて実行すると...
uipath_4.PNG

プログラム上のエラー表示されるみたい
uipath_5.png

独自エラーチェックを追加

祝日一覧取得APIでは3年分しか取得できません、ですので去年~来年に該当しない
日付が入力されて場合、メッセージ:「去年~来年までの日付を入力してください」
が出るようにしよう。
uipath_7.PNG
下記みたいな感じでエラーを出すことができました。
uipath_6.png

ソース

Class1.cs
 using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Collections;
using System.IO;
using System.Net;
using System.Activities;
using System.ComponentModel;

namespace ClassLibrary1
{
    public class Class1 : CodeActivity
    {

        [Category("Input")]
        [RequiredArgument]
        public InArgument<String> inputDay { get; set; }

        [Category("Output")]
        public OutArgument<String> outputDay { get; set; }

        protected override void Execute(CodeActivityContext context)
        {

            //画面入力された日付取得
            var Arg = inputDay.Get(context);
            DateTime date = DateTime.Parse(Arg.ToString());

            //Add start
            DateTime now = System.DateTime.Now;
            DateTime from = new DateTime(now.AddYears(-1).Year, 1, 1);
            DateTime to = new DateTime(now.AddYears(+1).Year, 12, 31);

            if (!(date >= from && date <= to))
            {

                throw new Exception("去年~来年までの日付を入力してください");

            }
            //Add end

            var result = outputMethod(date);

            //祝日判定結果をuipathに渡す
            outputDay.Set(context, result);
        }

        public string outputMethod(DateTime date)
        {
            //3年間分の祝日一覧を取得(当年・去年・来年)
            string url = "https://holidays-jp.github.io/api/v1/date.json";
            var req = WebRequest.Create(url);
            req.Headers.Add("Accept-Language:ja,en-us;q=0.7,en;q=0.3");
            var res = req.GetResponse();
            string responseFromServer = "";

            using (Stream dataStream = res.GetResponseStream())
            {

                StreamReader reader = new StreamReader(dataStream);
                responseFromServer = reader.ReadToEnd();

            }

            //取得した祝日一覧をjson形式に変換
            var jsonValues = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseFromServer);
            ArrayList dayList = new ArrayList();
            ArrayList dayListName = new ArrayList();

            //祝日一覧を配列に設定
            foreach (var key in jsonValues.Keys)
            {

                dayList.Add(key);
                dayListName.Add(jsonValues[key]);

            }

            /*入力された日付が祝日一覧に存在するか確認
            一致する場合は祝日名を呼び出し元に渡す
            一致しない場合は入力された日付が平日、土日を
            渡す          
             */
            int flg = 1;
            string outputDay = "";
            string outputDayName = "";

            for (int i = 0; i <= (dayList.Count - 1); i++)
            {
                flg = date.CompareTo(DateTime.Parse(dayList[i].ToString()));

                if (0 == flg)
                {

                    outputDay = DateTime.Parse(dayList[i].ToString()).ToString("yyyy/MM/dd");
                    outputDayName = dayListName[i].ToString();
                    break;

                }
            }

            if (0 != flg)
            {

                int number = ((int)date.DayOfWeek);
                string[] week = { "Su", "M", "Tu", "W", "Th", "F", "Sa", };
                string dayOfWeek = week[(int)number];

                string result = date.ToString("yyyy/MM/dd");
                outputDay = result;

                if (dayOfWeek == "Su" || dayOfWeek == "Sa")
                {
                    outputDayName = "土日";
                }
                else
                {
                    outputDayName = "平日";

                }
            }
            return outputDayName;
        }

    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0