【问题标题】:How do I know which cell is highlighted in jexcell javascript library?我如何知道在 jexcell javascript 库中突出显示了哪个单元格?
【发布时间】:2020-05-29 05:14:04
【问题描述】:

我想合并标有 jexcel 的单元格。我不知道该怎么做。更具体地说,我想将单元格合并为动态状态并将它们写入 MySQL 中的合并单元格。怎么做

<div id="example"></div>
<script>

var data = [
  ['', 'Ford', 'Tesla', 'Toyota', 'Honda'],
  ['2017', 10, 11, 12, 13],
  ['2018', 20, 11, 14, 13],
  ['2019', 30, 15, 12, 13]
];

var container = document.getElementById('example');
var hot = new Handsontable(container, {
  data: data,
  rowHeaders: true,
  colHeaders: true,
  filters: true,
  dropdownMenu: true,
  contextMenu: true
});

</script>

【问题讨论】:

  • 你试过这些吗? $('#spreasheet').jexcel('getSelection'); $('#spreasheet').jexcel('getCellCursor'); $('#spreasheet').jexcel('getSelectedCells');
  • 我编辑了这个问题。看一看。下面是如何识别这段代码上用鼠标标记的位置?我是一名 PHP 后端程序员。我不能在 JavaScript 中做很多事情
  • 我很困惑,您使用的是 Handsontable 还是 jExcel?
  • 检查我的答案,如果有什么不清楚的地方请告诉我。

标签: javascript jexcelapi jexceljs


【解决方案1】:

您的问题不清楚,但我会尽力回答。

如果我忽略您提供的示例并专注于 jExcel,我们可以使用 setMerge 合并单元格,为此我们需要知道:

  • 第一个单元格的地址
  • 所选行数
  • 选定的列数

我们可以从以下位置提取此信息:

$("#spreadsheet").jexcel("getSelectedRows", true);
$("#spreadsheet").jexcel("getSelectedColumns", true);

理论上,仅此一项就可以工作,但是 jExcel 在失去焦点时(即当用户单击按钮时)会取消选择单元格,这就是为什么我使用变通方法将单元格的选择存储在对象中,然后使用该对象来合并单元格.

HTML:

<div id="spreadsheet"></div>
<br>
<input id="myB" type="button" value="Merge Selected" />

Javascript:

var mySlection = {};
var options = {
  minDimensions: [10, 10],
  onselection: storeSelection
};

$(document).ready(function() {
  var mySpreadsheet = $("#spreadsheet").jexcel(options);


  $("#myB").click(function() {
    //Merge Cells using the stored selection
    $("#spreadsheet").jexcel("setMerge", mySlection.firstcell, mySlection.colspan, mySlection.rowspan)
    /*
    you may now store the following values to your MySQL
    mySlection.firstcell
    mySlection.colspan
    mySlection.rowspan
    */
  });
});


function storeSelection() {
  var sRows = $("#spreadsheet").jexcel("getSelectedRows", true);
  var sCols = $("#spreadsheet").jexcel("getSelectedColumns", true);

  mySlection.firstcell = [sCols[0], sRows[0]];
  mySlection.colspan = sCols.length;
  mySlection.rowspan = sRows.length;
}

要求: jQuery、jExcel 和 jSuites v3

Here's a Working Example at CodePen

【讨论】:

  • 谢谢,我会查的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
相关资源
最近更新 更多