1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
(() => {
'use strict';
let getCommentsData = (opt_offset, opt_comments) => {
let offset = opt_offset || 0;
let body = {
'app': kintone.app.getId(),
'record': kintone.app.record.getId(),
'offset': offset
};
let comments = opt_comments || [];
return kintone.api(kintone.api.url('/k/v1/record/comments.json', true), 'GET', body).then((resp) => {
comments = comments.concat(resp.comments);
if (resp.older === true) {
return getCommentsData(offset + 10, comments);
}
return comments;
});
};
// Escape
let escapeStr = (value) => {
return '"' + (value ? value.replace(/"/g, '""') : '') + '"';
};
let createCSVData = (comments) => {
let comments_csv = [];
// Name of the column in the CSV
let column_row = ['Comment ID', 'Text', 'Created date and Time',
'User code of commenter',
'Display name of commenter',
'Mentioned user', 'Mention type'];
comments_csv.push(column_row);
for (let i = 0; i < comments.length; i++) {
let row = [];
let mentions_code = [];
let mentions_type = [];
if (comments[i].mentions[0] === undefined) {
comments[i].mentions.code = null;
}
for (let k = 0; k < comments[i].mentions.length; k++) {
mentions_code.push(comments[i].mentions[k].code);
mentions_type.push(comments[i].mentions[k].type);
}
row.push(escapeStr(comments[i].id)); // Comment ID
row.push(escapeStr(comments[i].text)); // Text
row.push(escapeStr(comments[i].createdAt)); // Created date and time
row.push(escapeStr(comments[i].creator.code)); // User code of commenter
row.push(escapeStr(comments[i].creator.name)); // Display name of commenter
row.push(escapeStr(mentions_code.join(','))); // Mentioned user
row.push(escapeStr(mentions_type.join(','))); // Mention type
comments_csv.push(row);
}
return comments_csv;
};
// Record detail show
kintone.events.on(['app.record.detail.show'], (event) => {
// Create button in header
let header_element = kintone.app.record.getHeaderMenuSpaceElement();
let csv_button = document.createElement('button');
csv_button.id = 'export-comment-csv';
csv_button.innerText = 'Export comments as CSV';
csv_button.onclick = () => {
getCommentsData().then((comments) => {
// Create CSV data
let csv = createCSVData(comments);
// Export CSV
let csvbuf = csv.map((e) => {
return e.join(',');
}).join('\r\n');
let bom = new Uint8Array([0xEF, 0xBB, 0xBF]);
let blob = new Blob([bom, csvbuf], {type: 'text/csv'});
let url = (window.URL || window.webkitURL).createObjectURL(blob);
// File name:[APPID]_[RecordNumber].csv
let appId = kintone.app.getId();
let recordId = kintone.app.record.getId();
let fileName = appId + '_' + recordId + '_comments.csv';
if (window.navigator.msSaveOrOpenBlob) {
// Use IE
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
// Except IE
let link = document.createElement('a');
let e = document.createEvent('MouseEvents');
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
link.download = fileName;
link.href = url;
link.dispatchEvent(e);
}
});
};
header_element.appendChild(csv_button);
return event;
});
})();
|