找回密码
 立即注册

QQ登录

只需一步,快速开始

palalpapa

注册会员

7

主题

19

帖子

68

积分

注册会员

积分
68
palalpapa
注册会员   /  发表于:2023-7-27 13:42  /   查看:829  /  回复:5
本帖最后由 palalpapa 于 2023-7-27 14:13 编辑

image.png114051512.png
在表头上面加了两个汇总行,一个是一列的总的汇总,公式是 templateSheet.setFormula(0, 6, "=SUM(TestSheet1[freight])");这个是可以的,

[size=13.0667px]还有一行汇总我想实现条件汇总,代码是 templateSheet.setFormula(1, 6, "=IF(TestSheet1[customerId]=VINET,SUM(TestSheet1[freight]),0)");
这个就不行,想问一下这个公式应该怎么写,列是动态变的,字段名是不变的。

5 个回复

倒序浏览
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2023-7-27 14:19:56
沙发
您好,请您提供下可以复现出这个问题的Demo或ssjson文件,我们这边调研一下。
回复 使用道具 举报
palalpapa
注册会员   /  发表于:2023-7-27 14:50:12
板凳
Joestar.Xu 发表于 2023-7-27 14:19
您好,请您提供下可以复现出这个问题的Demo或ssjson文件,我们这边调研一下。
  1. <template>
  2.   <div id="spread-host">
  3.     <el-button @click="download">Download</el-button>
  4.     <gc-spread-sheets hostClass="spreadHost" @workbookInitialized="initWorkbook">
  5.     </gc-spread-sheets>
  6.   </div>
  7. </template>

  8. <script>
  9. import '@grapecity/spread-sheets/styles/gc.spread.sheets.excel2016colorful.css'
  10. import '@grapecity/spread-sheets-vue'
  11. import "@grapecity/spread-sheets-tablesheet";
  12. import GC from '@grapecity/spread-sheets'

  13. export default {
  14.   name: "spreadJs",
  15.   props: {},
  16.   data() {
  17.     return {
  18.       spread:null
  19.     }
  20.   },
  21.   computed: {},
  22.   created() {

  23.   },
  24.   mounted() {

  25.   },
  26.   methods: {
  27.     //绑定数据
  28.     initWorkbook(spread){
  29.       this.spread = spread;
  30.       spread.options.scrollbarMaxAlign=true;
  31.       spread.options.newTabVisible = false;
  32.       this.handleSpreadSheet(spread)
  33.     },
  34.     handleSpreadSheet(spread){
  35.       const _this = this
  36.       spread.suspendPaint();
  37.       spread.clearSheets();
  38.       spread.clearSheetTabs();
  39.       spread.options.autoFitType = GC.Spread.Sheets.AutoFitType.cellWithHeader;

  40.       let tableHeader = this.getTableHeader()
  41.       let dataManager = spread.dataManager();
  42.       let myTable = dataManager.addTable("myTable1", {
  43.         remote:{
  44.           read:{
  45.             url:'https://demodata.grapecity.com/northwind/api/v1/orders'
  46.           }
  47.         }
  48.       })
  49.       myTable.showFooter = true
  50.       let sheet = spread.addSheetTab(0,'TestSheet1',GC.Spread.Sheets.SheetType.tableSheet)
  51.       sheet.applyTableTheme(GC.Spread.Sheets.Tables.TableThemes["light9"]);

  52.       let rowActions = GC.Spread.Sheets.TableSheet.BuiltInRowActions;
  53.       let options = sheet.rowActionOptions();
  54.       options.push(rowActions.saveRow);
  55.       sheet.options.allowAddNew = false; //hide new row
  56.       myTable.fetch().then(function () {
  57.         let view = myTable.addView("myView", tableHeader);
  58.         _this.addTotalRow(sheet,tableHeader)
  59.         sheet.setDataView(view);
  60.         if (tableHeader.length > 0) {
  61.           sheet.togglePinnedColumns([0, 1]); //冻结列
  62.         }
  63.         setTimeout(() => {
  64.           spread.refresh();
  65.         },500);
  66.       });
  67.       spread.resumePaint();
  68.     },
  69.     addTotalRow(sheet, tableHeader) {
  70.       let templateSheet = new GC.Spread.Sheets.Worksheet();
  71.       // templateSheet.options.keepUnknownFormulas = true;
  72.       templateSheet.setRowCount(2);
  73.       templateSheet.setColumnCount(tableHeader.length);

  74.       let currencyFormatterStyle = new GC.Spread.Sheets.Style();
  75.       currencyFormatterStyle.backColor = "#e0e0e0";
  76.       currencyFormatterStyle.foreColor = "#333";
  77.       currencyFormatterStyle.locked = true;
  78.       templateSheet.setValue(0, 0, "Total");
  79.       templateSheet.addSpan(0, 0, 1, 5);
  80.       templateSheet.setStyle(0, 0, currencyFormatterStyle);

  81.       let style = new GC.Spread.Sheets.Style();
  82.       style.backColor = "green";
  83.       style.borderBottom = new GC.Spread.Sheets.LineBorder("black", GC.Spread.Sheets.LineStyle.thin);
  84.       templateSheet.setStyle(0, 6, style);
  85.       templateSheet.setFormula(0, 6, "=SUM(TestSheet1[freight])");

  86.       templateSheet.setStyle(0, 5, style);
  87.       templateSheet.setFormula(0, 5, "=SUM(TestSheet1[shipVia])");

  88.       //row 1
  89.       let currencyFormatterStyle1 = new GC.Spread.Sheets.Style();
  90.       currencyFormatterStyle1.backColor = "#e0e0e0";
  91.       currencyFormatterStyle1.foreColor = "#333";
  92.       currencyFormatterStyle1.locked = true;
  93.       templateSheet.setValue(1, 0, "VINET Total");
  94.       templateSheet.addSpan(1, 0, 1, 5);
  95.       templateSheet.setStyle(1, 0, currencyFormatterStyle1);

  96.       let style1 = new GC.Spread.Sheets.Style();
  97.       style1.backColor = "red";
  98.       style1.borderBottom = new GC.Spread.Sheets.LineBorder("black", GC.Spread.Sheets.LineStyle.thin);
  99.       templateSheet.setStyle(1, 6, style1);
  100.       // templateSheet.setFormula(1, 6, "=SUMIF(TestSheet1[customerId],TestSheet1[customerId]=VINET,@freight)");

  101.       //TODO:-------------------------------公式
  102.       templateSheet.setFormula(1, 6, "=IF(TestSheet1[customerId]=VINET,SUM(TestSheet1[freight]),0)");
  103.       templateSheet.setStyle(1, 5, style1);
  104.       templateSheet.setFormula(1, 5, "=SUM(TestSheet1[shipVia])");

  105.       let template = templateSheet.toJSON();
  106.       sheet.applyFreeHeaderArea(template);
  107.     },
  108.     getTableHeader(){
  109.       return [
  110.         {
  111.           caption:'customerId',
  112.           value:'customerId',
  113.           width:200,
  114.           visible:true,
  115.           readonly:true,
  116.           style:{
  117.             backColor:'yellow',
  118.             foreColor:'blue',
  119.             locked:true
  120.           }
  121.         },
  122.         {
  123.           caption:'employeeId',
  124.           value:'employeeId',
  125.           width:200
  126.         },
  127.         {
  128.           caption:'orderDate',
  129.           value:'customerId',
  130.           width:200
  131.         },
  132.         {
  133.           caption:'requiredDate',
  134.           value:'requiredDate',
  135.           width:200,
  136.           style: {
  137.             formatter:'yyyy-mm-dd HH:mm:ss'
  138.           }
  139.         },
  140.         {
  141.           caption:'shippedDate',
  142.           value:'shippedDate',
  143.           width:200,
  144.           style: {
  145.             formatter:'yyyy-mm-dd'
  146.           }
  147.         },
  148.         {
  149.           caption:'shipVia',
  150.           value:'shipVia',
  151.           width:200
  152.         },
  153.         {
  154.           caption:'freight',
  155.           value:'=IFERROR([@shipVia] * 2,0)',
  156.           width:200
  157.         },
  158.         {
  159.           caption:'shipName',
  160.           value:'shipName',
  161.           width:200
  162.         },
  163.         {
  164.           caption:'shipAddress',
  165.           value:'shipAddress',
  166.           width:200
  167.         },
  168.         {
  169.           caption:'shipCity',
  170.           value:'shipCity',
  171.           width:200
  172.         },
  173.         {
  174.           caption:'shipRegion',
  175.           value:'shipRegion',
  176.           width:200
  177.         },
  178.         {
  179.           caption:'shipPostalCode',
  180.           value:'shipPostalCode',
  181.           width:200
  182.         },
  183.         {
  184.           caption:'shipCountry',
  185.           value:'shipCountry',
  186.           width:200
  187.         },
  188.       ]
  189.     },
  190.     download(){
  191.       let fileName = '111.xlsx'
  192.       this.spread.export(function (blob) {
  193.         // save blob to a file
  194.         saveAs(blob, fileName);
  195.       }, function (e) {
  196.         console.log(e);
  197.       }, {
  198.         fileType: GC.Spread.Sheets.FileType.excel,
  199.         includeBindingSource: true
  200.       });
  201.     }
  202.   }
  203. }
  204. </script>

  205. <style scoped>
  206. .spreadHost {
  207.   width: 100%;
  208.   height: 800px;
  209. }

  210. </style>
复制代码
回复 使用道具 举报
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2023-7-27 17:31:12
地板
您好,我在尝试运行您的Demo时遇到了以下问题:

image.png433154906.png

我应该如何修改才能复现您的问题呢?
回复 使用道具 举报
palalpapa
注册会员   /  发表于:2023-7-28 09:56:03
5#
Joestar.Xu 发表于 2023-7-27 17:31
您好,我在尝试运行您的Demo时遇到了以下问题:

@grapecity/spread-sheets-tablesheet   这个库你没引入啊
回复 使用道具 举报
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2023-7-28 11:47:11
6#
image.png372035216.png

image.png466992797.png

这边已经引入了这个库。

image.png305024108.png

仍然报错。

image.png633040160.png

请您提供一个完整的可以运行的Demo,这边好进一步调研。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部