...
注:上述设置后,会出现数据无法完全居右的情况,产生原因:这个是第三方插件为了迎合excel本身的效果做的,对一个数字来说,如果设置了单元格格式为数值类型和没有设置为数值类型的,在excel中可以看到右侧距离右边框是有差异的,而第三方插件为了体现这种差异在生成web时故意加了一个空格作为显示值。
如需要彻底居右,可参考以下宏代码对空格进行处理。如需要彻底居右,可参考以下宏代码对空格进行处理(一个服务端宏一个客户端宏结合使用,可作为宏资源包)。
代码块 |
---|
//类型:ServerSide 电子表格服务端宏,对象spreadsheetReport,事件onBeforeOutput 对象: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 && celldisplayValue.getStringValueendsWith(" ").contains(" ")) {) { 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.value = cell.getStringValue().trim().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; } } } |
最终效果: