Summary_code
Code DVJS20_Tasks_groupBy_Role_Total_table
````ad-info
title: DVJS20_Tasks_groupBy_Role_Total_table =>To display a table by role and by month, where each cell shows the number of tasks completed for the month and for that role
collapse: close
icon:
color:
// T02. Require :
// #####################################################################
// version : 2022-05-01 v1.00 Justdoitcc
function print(...aArgs) {
let aText = [...aArgs];
let sTextJoined = aText
.join("")
.replace(/[ ]/g, " ")
.replace(/(\r\n|\r|\n)/g, "<br>");
dv.el("span", sTextJoined);
return true;
}
// M11. [Debug]:Define pages: gather all relevant pages
// #####################################################################
// let pages = dv.pages(
// '#Project and "100_Project/01_dataviewjs/01_by_example/Q12_Tasks_Sum/Q12_test_data"'
// );
// M21. [Debug]:Define a_Role1_pages_task_text: get completed task.text
// #####################################################################
// let a_Role1_pages_task_text =
// pages("#Project and #Role1")
// .file.tasks.where((task) => task.completed).text;
// for \#Role1: get completed task.completion
// M23. Define a_Role1_pages_task_completion: ["2022-03-07","2022-07-18"]
// #####################################################################
let a_Role1_pages_task_completion = dv
.pages("#Project and #Role1")
.file.tasks.where((task) => task.completed)
.completion.map((e) => e.toFormat("yyyy-MM-dd"));
// M23.DB01 Debug Output: a_Role1_pages_task_completion:
// #####################################################################
// print("The following is the content of the `a_Role1_pages_task_completion`.\n");
//print(JSON.stringify(a_Role1_pages_task_completion, null, 2), "\n");
//The following is the content of the a_Role1_pages_task_completion.
// [
// "2022-03-07",
// "2022-07-18",
// "2022-06-08",
// "2022-05-18",
// "2022-06-18",
// "2022-06-28",
// "2022-07-19",
// "2022-06-19",
// "2022-05-19",
// "2022-06-29",
// "2022-06-09",
// "2022-07-19"
// ]
// for \#Role1: get completed task.completion
// M25. Define h_Role1_pages_task_completion_YYYYMM:{RolesMonth: Role1}
// #####################################################################
let h_Role1_pages_task_completion_YYYYMM = {};
for (let completion of a_Role1_pages_task_completion) {
let s_YYYYMM = completion.replace(/-\d{2}$/, "");//=>"2022-06"
if (s_YYYYMM in h_Role1_pages_task_completion_YYYYMM) {
h_Role1_pages_task_completion_YYYYMM[s_YYYYMM]++;
} else {
h_Role1_pages_task_completion_YYYYMM[s_YYYYMM] = 1;
}
}
// M25.DEB01 Debug Output: h_Role1_pages_task_completion_YYYYMM:
// #####################################################################
// print("The following is the content of the `h_Role1_pages_task_completion_YYYYMM`.\n");
// print(JSON.stringify(h_Role1_pages_task_completion_YYYYMM, null, 2), "\n");
//The following is the content of the h_Role1_pages_task_completion_YYYYMM.
// {
// "2022-07": 3,
// "2022-06": 6,
// "2022-05": 2,
// "2022-03": 1
// }
// for \#Role2: get completed task.completion
// M33. Define a_Role2_pages_task_completion: [ "2022-04-03","2022-07-04",]
// #####################################################################
let a_Role2_pages_task_completion = dv
.pages("#Project and #Role2")
.file.tasks.where((task) => task.completed)
.completion.map((e) => e.toFormat("yyyy-MM-dd"));
// M33.DEB01 Debug Output: a_Role2_pages_task_completion:
// #####################################################################
//print("The following is the content of the `a_Role2_pages_task_completion`.\n");
//print(JSON.stringify(a_Role2_pages_task_completion, null, 2), "\n");
//The following is the content of the a_Role2_pages_task_completion.
// [
// "2022-04-03",
// "2022-07-04",
// "2022-07-14",
// "2022-06-04",
// "2022-05-04",
// "2022-07-05",
// "2022-06-05",
// "2022-05-05",
// "2022-07-15",
// "2022-07-25",
// "2022-07-15",
// "2022-05-15",
// "2022-05-25"
// ]
// for \#Role2: get completed task.completion
// M35. Define h_Role2_pages_task_completion_YYYYMM: {RolesMonth: Role2}
// #####################################################################
let h_Role2_pages_task_completion_YYYYMM = {};
for (let completion of a_Role2_pages_task_completion) {
let s_YYYYMM = completion.replace(/-\d{2}$/, "");//"2022-06-19"=>"2022-06"
if (s_YYYYMM in h_Role2_pages_task_completion_YYYYMM) {
h_Role2_pages_task_completion_YYYYMM[s_YYYYMM]++;
} else {
h_Role2_pages_task_completion_YYYYMM[s_YYYYMM] = 1;
}
}
// M35.DEB01 Debug Output: h_Role2_pages_task_completion_YYYYMM:
// #####################################################################
// print("The following is the content of the `h_Role2_pages_task_completion_YYYYMM`.\n");
// print(JSON.stringify(h_Role2_pages_task_completion_YYYYMM, null, 2), "\n");
//The following is the content of the h_Role2_pages_task_completion_YYYYMM.
// {
// "2022-07": 6,
// "2022-06": 2,
// "2022-05": 4,
// "2022-04": 1
// }
// M41. Define aRolesMonth_unique: get unique => sort
// #####################################################################
let aRolesMonth = [
...Object.keys(h_Role1_pages_task_completion_YYYYMM),
...Object.keys(h_Role2_pages_task_completion_YYYYMM),
];//get each RolesMonth for \#Role1 or \#Role2
let aRolesMonth_unique = [...new Set(aRolesMonth)];//get unique RolesMonth
aRolesMonth_unique.sort();//=>2022-03,2022-04,2022-05,2022-06,2022-07
// M51. Define aoa_RolesMonth_Role1_Role2 : array of arrays
// #####################################################################
// [
// ["2022-03", 1, 0],
// ["2022-04", 0, 1],
// ["2022-05", 2, 4],
// ["2022-06", 6, 2],
// ["2022-07", 3, 6],
// ]
let aoa_RolesMonth_Role1_Role2 = [];
for (let sRolesMonth of aRolesMonth_unique) {
let s_MM = sRolesMonth.replace(/^\d{4}-/, ""); //"2022-06"=>"06"
let i_Role1 = dv.func.default(
h_Role1_pages_task_completion_YYYYMM[sRolesMonth],
0
); //=>6
let i_Role2 = dv.func.default(
h_Role2_pages_task_completion_YYYYMM[sRolesMonth],
0
); //=>2
aoa_RolesMonth_Role1_Role2.push([s_MM, i_Role1, i_Role2]);
}
// M51.DEB01 Debug Output: aoa_RolesMonth_Role1_Role2
// #####################################################################
//print("The following is the content of the `aoa_RolesMonth_Role1_Role2`.\n");
//print(JSON.stringify(aoa_RolesMonth_Role1_Role2, null), "\n");
//The following is the content of the aoa_RolesMonth_Role1_Role2.
//["03",1,null],["04",null,1],["05",2,4],["06",6,2],["07",3,6] //=>without using dv.func.default()
//["03",1,0],["04",0,1],["05",2,4],["06",6,2],["07",3,6] //=>with using dv.func.default()
// M61. print a table of aoa_RolesMonth_Role1_Role2
// #####################################################################
dv.header(5, "M61. Print a table of the `aoa_RolesMonth_Role1_Role2`");
// dv.table(
// ["Roles/Month", "Role1", "Role2"],
// [
// ["2022-03", 1, 0],
// ["2022-04", 0, 1],
// ["2022-05", 2, 4],
// ["2022-06", 6, 2],
// ["2022-07", 3, 6],
// ]
// );
dv.table(["Roles/Month", "Role1", "Role2"], aoa_RolesMonth_Role1_Role2);
````