Skip to content

Commit 82f4e97

Browse files
committed
Merged PR 4442: Log Export Data Result as a table.
Log Export Data Result as a table.
1 parent baa16cd commit 82f4e97

2 files changed

Lines changed: 85 additions & 9 deletions

File tree

‎demo/v2-demo/scripts/codesamples.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ function _Page_HasLayout() {
12721272

12731273
// Get a reference to the embedded report.
12741274
report = powerbi.get(embedContainer);
1275-
1275+
12761276
// Retrieve the page collection and check if the first page has a MobilePortrait layout.
12771277
report.getPages().then(function (pages) {
12781278
pages[0].hasLayout(models.LayoutType.MobilePortrait).then(function(hasLayout) {
@@ -1575,7 +1575,7 @@ function _Bookmarks_Capture() {
15751575
// Get a reference to the embedded report.
15761576
report = powerbi.get(embedContainer);
15771577

1578-
// Capture the current bookmark and prints the bookmark's
1578+
// Capture the current bookmark and prints the bookmark's
15791579
// state string to Log window.
15801580
report.bookmarksManager.capture()
15811581
.then(function (capturedBookmark) {
@@ -1800,8 +1800,8 @@ function _Visual_ExportData_Summarized() {
18001800

18011801
// Exports visual data
18021802
visual.exportData(models.ExportDataType.Summarized)
1803-
.then(function (data) {
1804-
Log.log(data);
1803+
.then(function (result) {
1804+
Log.logCsv(result.data);
18051805
})
18061806
.catch(function (errors) {
18071807
Log.log(errors);
@@ -1845,8 +1845,8 @@ function _Visual_ExportData_Underlying() {
18451845

18461846
// Exports visual data
18471847
visual.exportData(models.ExportDataType.Underlying)
1848-
.then(function (data) {
1849-
Log.log(data);
1848+
.then(function (result) {
1849+
Log.logCsv(result.data);
18501850
})
18511851
.catch(function (errors) {
18521852
Log.log(errors);

‎demo/v2-demo/scripts/logger.js‎

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ function InitLogger(divId) {
22

33
var Logger = {};
44

5-
Logger.log = function name(event) {
5+
// Normal character takes ~1.5 width more than a space ' '.
6+
Logger.spaceWidthCorrection = 1.3;
7+
8+
Logger.log = function (event) {
69
this.logText("Json Object\n" + JSON.stringify(event, null, " "));
710
};
811

9-
Logger.logText = function name(text) {
12+
Logger.logText = function (text) {
1013
let textbox = document.getElementById(divId);
11-
14+
1215
if (!textbox.value)
1316
{
1417
textbox.value = "";
@@ -19,5 +22,78 @@ function InitLogger(divId) {
1922
textbox.scrollTop = textbox.scrollHeight;
2023
};
2124

25+
Logger.logCsv = function (text) {
26+
let textbox = document.getElementById(divId);
27+
28+
if (!textbox.value)
29+
{
30+
textbox.value = "";
31+
}
32+
33+
let maxLength = 0;
34+
let lines = text.split("\r\n");
35+
let valuesPerLine = [];
36+
37+
let log = "> CSV result in table view: \n\n";
38+
if (!lines || lines.length === 0) {
39+
log += "No data";
40+
}
41+
else {
42+
// Calcualte values per line, and calculate max length for pretty print.
43+
for (let i = 0; i < lines.length; ++i) {
44+
valuesPerLine[i] = lines[i].split(",");
45+
valuesPerLine[i].forEach(function (val) {
46+
if (val.length > maxLength) {
47+
maxLength = val.length;
48+
}
49+
});
50+
}
51+
52+
// Add 2 spaces before and after.
53+
maxLength += 4;
54+
55+
// Print title line
56+
var title = this.getLineText(valuesPerLine[0], maxLength);
57+
log += title + "\n";
58+
log += this.repeatChar("-", title.length) + "\n";
59+
60+
// Print all lines
61+
for (let i = 1; i < lines.length; ++i) {
62+
log += this.getLineText(valuesPerLine[i], maxLength) + "\n"
63+
}
64+
}
65+
66+
textbox.value += log;
67+
textbox.scrollTop = textbox.scrollHeight;
68+
};
69+
70+
Logger.getLineText = function (values, spacesPerWord) {
71+
var text = "";
72+
_this = this;
73+
values.forEach(function (val) {
74+
text += _this.getCenteredText(val, spacesPerWord);
75+
});
76+
return text;
77+
};
78+
79+
Logger.getCenteredText = function (value, spaces) {
80+
var text = "";
81+
82+
let spacesBefore = (spaces - value.length) / 2;
83+
let spacesAfter = spaces - value.length - spacesBefore;
84+
text += this.repeatChar(" ", spacesBefore * this.spaceWidthCorrection);
85+
text += value;
86+
text += this.repeatChar(" ", spacesAfter * this.spaceWidthCorrection);
87+
return text;
88+
};
89+
90+
Logger.repeatChar = function (char, times) {
91+
let text = "";
92+
for (let i = 0; i < times; ++i) {
93+
text += char;
94+
}
95+
return text;
96+
};
97+
2298
return Logger;
2399
}

0 commit comments

Comments
 (0)