kintone Utility Library for JavaScriptを使用した際の複数のqueryの設定方法

お世話になっております。

タイトルの件について「kintone Utility Library for JavaScript」の使い方を参考に

一覧画面に日付で絞込表示をした後、関連レコードを週、月、年で集計したいと考えております。

フォームには下記4項目があります。

*-----------------------------------*

・日付※日付

・年度※ドロップダウン(H27~H30)

・月※ドロップダウン(4月~3月)

・ドロップダウン※ドロップダウン(選択1~選択3)

※日付を入力した際にその日付に合う、年度、月を選択しております。

【例】日付が「2017-08-25」であれば、年度がH29、月が8月です。

*-----------------------------------*

現在下記のようにコードを設定しており、週計は日付の絞込で集計はできますが、月計、年度計のqueryの指定方法に悩んでおります。

kintone.events.on('app.record.index.show', function(event) {
    //週計用※日付で週を絞込表示したレコードを集計
    var param1 = {
        app: kintone.app.getId(),
        query: kintone.app.getQueryCondition(),
        totalCount: true
    };
    kintoneUtility.rest.getAllRecordsByQuery(param1).then(function(resp) {
        var id1 = resp.records.filter(function(data){ return data.ドロップダウン.value === "選択1";}).length;
        window.alert(id1);
    }).catch(function(error) {window.alert(error.message);});
    //月計用※週計で絞込表示した月のレコードを集計
    var param2 = {
        app: kintone.app.getId(),
        query: ,
        totalCount: true
    };
    kintoneUtility.rest.getAllRecordsByQuery(param2).then(function(resp) {
        var id2 = resp.records.filter(function(data){ return data.ドロップダウン.value === "選択1";}).length;
        window.alert(id2);
    }).catch(function(error) {window.alert(error.message);});
    //年度計用※週計で絞込表示した年度のレコードを集計
    var param3 = {
        app: kintone.app.getId(),
        query: ,
        totalCount: true
    };
    kintoneUtility.rest.getAllRecordsByQuery(param3).then(function(resp) {
        var id3 = resp.records.filter(function(data){ return data.ドロップダウン.value === "選択1";}).length;
        window.alert(id3);
    }).catch(function(error) {window.alert(error.message);});
});

お手数でございますが、ご教授いただければ幸いでございます。

よろしくお願いいたします。

 

Nao さん

getQueryCondition の指定日付を取り出して、月換算、年度換算してはいかがですか?

kintone.app.getQueryCondition() の例

“更新日時 >= “2017-08-06T15:00:00.000Z” and 更新日時 < “2017-08-13T15:00:00.000Z””

スペース区切りで配列変換

var tmp = kintone.app.getQueryCondition().split(’ ')

[“更新日時”, “>=”, ““2017-08-06T15:00:00.000Z””, “and”, “更新日時”, “<”, ““2017-08-13T15:00:00.000Z””]

var date1 = moment(tmp[2]);

あとは、月換算、年度換算

 

rex0220様

ご返信ありがとうございます。

ご教授いただいた方法について、設定したところ

※日付を2017-07-01~2017-07-31で絞込表示

var tmp = kintone.app.getQueryCondition().split(' ')
var date1 = moment(tmp[2]);
var date2 = tmp[2]

date1は「NaN」が返ってきて、date2は「2017-07-01」が返ってきました。

date1の「NaN」はmomentの設定が間違っているのでしょうか。

また、取り出した日付をどのように、下記query部分に月換算、年度換算すれば良いのでしょうか。

var param2 = {
    app: kintone.app.getId(),
    query: ,
    totalCount: true
};

度々お手数でございますが、ご教授いただければ幸いでございます。

よろしくお願いいたします。

 

 

コードは、あくまで例なので、抽出条件の指定に合わせて、配列の位置は変えてください。

kintone では、抽出条件が動的に変わりますので、日付のフィールドコード位置を検索して

使われたほうがいいと思います。

var start = tmp.indexOf(‘開始日’); // フィールドコード位置

 

月換算、年換算の開始・終了のコード例です。

切り貼りで未検証です。

詳細は、moment.js のマニュアルをご覧ください。

 

月換算

var startMonth = date1.startOf(‘month’).format(‘YYYY-MM-DD’);

var endMonth = date1.endOf(‘month’).format(‘YYYY-MM-DD’);

 

年換算

var stratYear, endYear;
if (date1.month() < 3) {
startYear = date1.add(-1,‘year’).format(‘YYYY’) + ‘-04-01’;
endYear = date1.format(‘YYYY’) + ‘-03-31’;
}
else {
startYear = date1.format(‘YYYY’) + ‘-04-01’;
endYear = date1.add(1,‘year’).format(‘YYYY’) + ‘-03-31’;
}

// date1.month() は、 0-11 の値

 

 

rex0220様

ご返信ありがとうございます。

ご教授いただいきましたコードを参考に作成して、値の取得ができました。

ありがとうございます。

大変恐縮ですが、もう一点、月計と年計のqueryの指定方法なのですが、

下記コードで設定した変数宣言の「Month」「nendo」をドロップダウン項目の年度と月でqueryに指定したいのですが、

宣言した変数名で指定しても、queryの設定方法が間違っているとエラーが出てしまいます。

(function () {
    'use strict';
    kintone.events.on("app.record.index.show", function(e) {
        var param1 = {
            app: kintone.app.getId(),
            query: kintone.app.getQueryCondition(),
            totalCount: true
        };
        if(!Number.isNaN)Number.isNaN = isNaN;
        var tmp = kintone.app.getQueryCondition().split('"');
        var date = moment(tmp[1]);
        var Month = date.format('M月');
        var Year;
        if (date.month() < 3) {
            Year = date.add(-1,'year').format('YYYY');
        } else {
            Year = date.format('YYYY');
        }
        var nendo = koyomi.format(Year, 'GN');
        //週計用※日付で週を絞込表示したレコードを集計
        kintoneUtility.rest.getAllRecordsByQuery(param1).then(function(resp) {
        }).catch(function(error) {window.alert(error.message);});
        var param2 = {
            app: kintone.app.getId(),
            query: '月 in ("Month")',
            totalCount: true
        };
        //月計用※週計で絞込表示した月のレコードを集計
        kintoneUtility.rest.getAllRecordsByQuery(param2).then(function(resp) {
            window.alert(Month + "です。");
        }).catch(function(error) {window.alert(error.message);});
        var param3 = {
            app: kintone.app.getId(),
            query: '年度 in ("nendo")',
            totalCount: true
        };
        //年度計用※週計で絞込表示した年度のレコードを集計
        kintoneUtility.rest.getAllRecordsByQuery(param3).then(function(resp) {
            window.alert(nendo + "です。");
        }).catch(function(error) {window.alert(error.message);});
    });
})();

度々お手数でございますが、ご教授いただければ幸いでございます。

よろしくお願いいたします。

変数ではなく、定数を指定しています。

query: ‘月 in (“Month”)’,

query: '月 = ’ + Month,

 

失礼、ドロップダウン項目でしたね。

query: ‘月 = ”’ + Month + ‘"’,

rex0220様

早速のご返答ありがとうございます。

ご教授いただいたように下記で設定し、queryの設定ができました。

query: ‘月 in ("’ + Month + ‘")’,

ありがとうございます。

今後ともよろしくお願いいたします。

 

もし、何年か分のデータがあるのでしたら、年指定もいるような気がします。

 

rex0220様

年指定について、アドバイスありがとうございます

現在のデータは、今年分のみになっているので、数年のデータを扱う際に

また、設定を考えてみたいと思います。

ありがとうございました。