【Office Script】営業日判定

事前準備

営業日のテーブルを用意しましょう

 

指定日が営業日であるか

/**
 * 指定日付が営業日であるか
 * 
 * target_date: yyyymmdd形式の文字列
 * return : true: 営業日である, false: 営業日でない
 */
function main(workbook: ExcelScript.Workbook, target_date: string): boolean{
    
    // 営業日マスタ―テーブルから営業日のリストを取得する
    const table = workbook.getTable('営業日マスタテーブル');
    const column =  table.getColumnByName('日付');
    const businessDates = column.getRangeBetweenHeaderAndTotal().getValues().map(
        (row) => {return row[0]});

    // リスト内に指定された日付が存在するか
    const is_businessDates = businessDates.includes(target_date);
    return is_businessDates;
}

 

指定日付の○日後、○日前の営業日を取得する

スクリプトは営業日に実行する前提なので、休日に実行した場合nullを返却します。

/**
 * 指定日付の○営業日後の営業日を取得する
 * 
 * target_date: yyyymmdd形式の文字列
 * return : 営業日を取得できた場合:(yyyymmdd)
 *          営業日の取得に失敗した場合:null
 */
function main(workbook: ExcelScript.Workbook, target_date: string, date_offset: number): string {
    
    // 営業日マスタ―テーブルから営業日のリストを取得する
    const table = workbook.getTable('営業日マスタテーブル');
    const column =  table.getColumnByName('日付');
    const businessDates: string[] = column.getRangeBetweenHeaderAndTotal().getValues().map(
        (row) => {return String(row[0])});

    // 指定日付が存在しない場合、nullを返却する
    const index = businessDates.indexOf(target_date);

    if (index === -1) {
        return null;
    }

    // n日後の営業日を取得する
    const offset_bussiness_date = businessDates[index + date_offset];
    if (!offset_bussiness_date) {
        return null;
    }

    workbook.getWorksheet('営業日マスタ').getRange('F2').setValue(offset_bussiness_date);
    return offset_bussiness_date;
}