...
电子表格内,选中应用了数据格式的字段对应的单元格,双击或者点击Smartbi工具栏上的【属性】按钮;选择【其他】→勾选上使用显示值,数据格式即可生效。
如果需要额外对数据进行汇总求和,则要提前在数据集中进行汇总。
若只是希望设置保留小数位,如保留两位小数,则可以在电子表格对单元格右键设置单元格格式
注:上述设置后,会出现数据无法完全居右的情况,产生原因:这个是第三方插件为了迎合excel本身的效果做的,对一个数字来说,如果设置了单元格格式为数值类型和没有设置为数值类型的,在excel中可以看到右侧距离右边框是有差异的,而第三方插件为了体现这种差异在生成web时故意加了一个空格作为显示值。
如需要彻底居右,可参考以下宏代码对空格进行处理(一个服务端宏一个客户端宏结合使用,可作为宏资源包)。
代码块 |
---|
// 电子表格服务端宏,对象spreadsheetReport,事件onBeforeOutput
function main(spreadsheetReport) {
var type = spreadsheetReport.outputType;
if (type == "refresh") {
var workbook = spreadsheetReport.workbook; //获取电子表格工作簿
var worksheets = workbook.worksheets;
for (var i = 0; i < worksheets.count; i++) {
var worksheet = worksheets.get(i); //sheet1
var sheetName = worksheet.name;
var cells = worksheet.cells;
var maxDisplayRange = cells.maxDisplayRange; //表格最大范围
//对表格中的值进行循环判断
for (var x = 0; x < maxDisplayRange.rowCount; x++) {
for (var y = 0; y < maxDisplayRange.columnCount; y++) {
var cell = cells.get(x, y);
var displayValue = cell.getStringValue();
if (cell.getValue() != null && displayValue.endsWith(" ")) {
cell.value = cell.value + "";
displayValue = displayValue.trim();
var spaceCellKey = sheetName + "/" + "spaceCell";
var spaceCell = spreadsheetReport.customProperties.get(spaceCellKey);
if (!spaceCell) {
spaceCell = [];
spreadsheetReport.customProperties.put(spaceCellKey, spaceCell)
}
spaceCell.push({
cell: [cell.getRow(), cell.getColumn()],
displayValue: displayValue
});
}
}
}
}
}
} |
代码块 |
---|
// 电子表格客户端宏,对象spreadsheetReport,事件onRenderReport
function main(spreadsheetReport) {
try {
spreadsheetReport.onAfterRefresh.subscribe(function() {
if (!spreadsheetReport.tableGrid) {
spreadsheetReport.initTableGrid();
}
var sheetName = spreadsheetReport.sheetNames[spreadsheetReport.currentSheetIndex];
var spaceCellKey = sheetName + "/" + "spaceCell";
var win = spreadsheetReport.elemSheetFrame.contentWindow;
spaceCell(spreadsheetReport, win, spaceCellKey);
});
// 添加照相机加载完成后执行的事件
spreadsheetReport.onPictureIframeLoad.subscribe(cameraSpaceCell, spreadsheetReport);
function cameraSpaceCell(spreadsheetReport, iframe) {
var idx2 = iframe.src.lastIndexOf("_");
var idx1 = iframe.src.lastIndexOf("_", idx2 - 1) + 1;
var index = iframe.src.substring(idx1, idx2);
var sheetName = spreadsheetReport.sheetNames[index];
var spaceCellKey = sheetName + "/" + "spaceCell";
if (!(spreadsheetReport.pictureFramesTableGrid && spreadsheetReport.pictureFramesTableGrid[iframe.name])) {
spreadsheetReport.initTableGrid(iframe);
}
var win = iframe.contentWindow;
spaceCell(spreadsheetReport, win, spaceCellKey);
}
} catch (e) {}
}
function spaceCell(spreadsheetReport, win, spaceCellKey) {
var shapesMap = spreadsheetReport.shapesMap;
var spaceCells = win.customProperties && win.customProperties[spaceCellKey];
if (!spaceCells) {
return;
}
var tableGrid = spreadsheetReport.tableGrid;
for (var i = 0; i < spaceCells.length; i++) {
var spaceCell = spaceCells[i];
var cell = spaceCell.cell;
var row = cell[0];
var column = cell[1];
var tableCell = spreadsheetReport.getCellByPosition(win, row, column);
if (tableCell) {
tableCell.innerText = spaceCell.displayValue;
}
}
} |
最终效果: