|
|
@@ -27,6 +27,31 @@ proto.dottedLine = function (x1, y1, x2, y2, interval = 4) {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+const textUtils = {
|
|
|
+ getStringListDisplayWidth: function(sheet, font, strList) {
|
|
|
+ const xs = sheet.getParent().xs;
|
|
|
+ const ctx = xs.childNodes[0].getContext("2d");
|
|
|
+ ctx.font = font;
|
|
|
+ const widths = strList.map(x => { return ctx.measureText(x).width; });
|
|
|
+ return Math.max(...widths);
|
|
|
+ },
|
|
|
+ getTextDisplayWidth: function(sheet, font, str) {
|
|
|
+ const xs = sheet.getParent().xs;
|
|
|
+ const ctx = xs.childNodes[0].getContext("2d");
|
|
|
+ ctx.font = font;
|
|
|
+ return ctx.measureText(str).width;
|
|
|
+ },
|
|
|
+ getTextAutoHeight: function(sheet, font, str, displayWidth) {
|
|
|
+ const xs = sheet.getParent().xs;
|
|
|
+ const ctx = xs.childNodes[0].getContext("2d");
|
|
|
+ ctx.font = font;
|
|
|
+ const textMertrices = ctx.measureText(str);
|
|
|
+ const fontHeight = textMertrices.fontBoundingBoxAscent + textMertrices.fontBoundingBoxDescent;
|
|
|
+ const textRow = Math.ceil(textMertrices.width / displayWidth);
|
|
|
+ return textRow * fontHeight + 2 + (textRow - 1) * 1;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// 简写Spread常量
|
|
|
const spreadNS = GC.Spread.Sheets;
|
|
|
// 授权码
|
|
|
@@ -245,6 +270,61 @@ const SpreadJsObj = {
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
+ selChangedTipsSum: function(sheet) {
|
|
|
+ sheet.bind(spreadNS.Events.SelectionChanged, function(e, info) {
|
|
|
+ let sum = 0, count = 0, average = 0;
|
|
|
+ const sel = info.newSelections[0];
|
|
|
+ if (sel) {
|
|
|
+ for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
|
|
|
+ if (!info.sheet.getRowVisible(iRow)) continue;
|
|
|
+ for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
|
|
|
+ if (!info.sheet.getColumnVisible(iCol)) continue;
|
|
|
+ const text = info.sheet.getText(iRow, iCol);
|
|
|
+ if (text) {
|
|
|
+ count++;
|
|
|
+ const value = _.toNumber(text);
|
|
|
+ sum = ZhCalc.add(sum, _.isNaN(value) ? 0 : value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (count > 1) {
|
|
|
+ const maxHintWidth = 200, indent = 10;
|
|
|
+ average = ZhCalc.div(sum, count);
|
|
|
+ const text = [`合计:${sum}`, `计数:${count}`, `平均值:${average}`];
|
|
|
+ let sumTip = $('#spread-sum-tip')[0];
|
|
|
+ if (!sumTip) {
|
|
|
+ sumTip = document.createElement("div");
|
|
|
+ $(sumTip).css("position", "absolute")
|
|
|
+ .css("border", "1px #C0C0C0 solid")
|
|
|
+ .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
|
|
|
+ .css("font", "9pt Arial")
|
|
|
+ .css("background", "white")
|
|
|
+ .css("padding", 5)
|
|
|
+ .css("z-index", 999)
|
|
|
+ .css("max-width", maxHintWidth)
|
|
|
+ .css("word-wrap", "break-word")
|
|
|
+ .attr("id", 'spread-sum-tip');
|
|
|
+ document.body.insertBefore(sumTip, null);
|
|
|
+ }
|
|
|
+ const hitinfo = info.sheet.getCellRect(sel.row, sel.col + sel.colCount);
|
|
|
+ const pos = SpreadJsObj.getObjPos(info.sheet.getParent().qo);
|
|
|
+ const showTop = Math.min(pos.y + hitinfo.y + indent, window.innerHeight - 72); // 52(w)+10(p)+10(i)
|
|
|
+ let showLeft = pos.x + hitinfo.x + indent;
|
|
|
+ if (showLeft > window.innerWidth - maxHintWidth) {
|
|
|
+ const textWidth = textUtils.getStringListDisplayWidth(info.sheet, "9pt Arial", text);
|
|
|
+ showLeft = Math.min(showLeft, window.innerWidth - textWidth - 10 - indent);
|
|
|
+ }
|
|
|
+ $(sumTip).html(text.join('<br/>')).css("top", showTop).css("left", showLeft);
|
|
|
+
|
|
|
+ setTimeout(()=>{ if (sumTip) $(sumTip).show("fast");}, 100);
|
|
|
+ // setTimeout(()=>{ if (sumTip) $(sumTip).hide();}, 5000);
|
|
|
+ } else {
|
|
|
+ const sumTip = $('#spread-sum-tip');
|
|
|
+ if (sumTip) sumTip.hide();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
selChangedRefreshBackColor: function (sheet) {
|
|
|
sheet.bind(spreadNS.Events.SelectionChanged, function (e, info) {
|
|
|
const rows = [];
|
|
|
@@ -418,6 +498,7 @@ const SpreadJsObj = {
|
|
|
if (setting.selectedBackColor) {
|
|
|
SpreadJsObj.selChangedRefreshBackColor(sheet);
|
|
|
}
|
|
|
+ if (setting.tipsSum) SpreadJsObj.selChangedTipsSum(sheet);
|
|
|
this.endMassOperation(sheet);
|
|
|
if (!setting.invalidCopyFilterHiddenRow) this.copyFilterHiddenRow(sheet);
|
|
|
},
|
|
|
@@ -972,7 +1053,7 @@ const SpreadJsObj = {
|
|
|
for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
|
|
|
sheet.getCell(i, iCol).backColor(SpreadJsObj._getBackColor(sheet, data, i, col));
|
|
|
}
|
|
|
- };
|
|
|
+ }
|
|
|
this.endMassOperation(sheet);
|
|
|
} catch (err) {
|
|
|
this.endMassOperation(sheet);
|