本帖最后由 浩 于 2021-9-3 14:43 编辑
这个例子可以看到问题
- const data = {
- "0":{
- "15,1":{
- "row":"15",
- "col":"1",
- "mapping_value":"sign_one::一级签字",
- "value":"管理员",
- "old_text":""
- },
- "16,1":{
- "row":"16",
- "col":"1",
- "mapping_value":"sign_one_time::一级签字日期",
- "value":"2021年09月03日",
- "old_text":""
- }
- }
- };
- _.forOwn(data, (item, pageIndex) => {
- const sheet = $.spread.getSheetObjForIndex(pageIndex)
- _.forOwn(item, (cellData) => {
- const key = cellData['mapping_value'].split('::')[0];
- const keyValue = cellData['mapping_value'].split('::')[1];
- let {row, col, old_text, value} = cellData;
- row = parseInt(row) + parseInt(sheet.getRowCount()) - parseInt(this.tempSheetJson[pageIndex]['rowCount']);
- const old = old_text.replace(new RegExp(" ", "gm"), ' ');
- if ($.common.isEmpty(value)) value = '';
- let style = $.sheet.getStyle(row, col, sheet); // 获取样式
- style.backColor = null; // 清空背景色
- style.wordWrap = true; // 自动换行
- sheet.getCell(row, col).text(old + value).cellType(new AutoWrapTextCellType());
- sheet.setStyle(row, col, style, GC.Spread.Sheets.SheetArea.viewport);
- })
- })
复制代码- function AutoWrapTextCellType() {
- }
- AutoWrapTextCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
- AutoWrapTextCellType.prototype.paint = function (ctx, value, x, y, w, h, style, context) {
- ctx.font = style.font;
- value = wrapString(ctx, value, w - 15);
- GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
- };
- function wrapString(c, str, maxWidth) {
- let width = c.measureText(str).width;
- if(width<=maxWidth) return str;
- const len = str.length;
- let newStrList = [];
- let index = 0;
- for (let i = 0; i < len; i++) {
- // 临时拼接字符串
- let _newStr = newStrList[index] + str[i];
- // 临时拼接字符串的长度
- let newWidth = c.measureText(_newStr).width;
- // 初始化数组
- if(!newStrList[index]) newStrList[index] = '';
- // 长度判断
- if(newWidth >= maxWidth){ // 长度超过,换行
- newStrList[index] = newStrList[index] + '\n';
- index++;
- newStrList[index] = str[i];
- }else{
- newStrList[index] = newStrList[index] + str[i];
- }
- }
- return newStrList.join('');
- }
复制代码 |
|