[{"id":"b66f8b1e-cc14-4e89-9679-abd5687d283d","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"569bb90a-ea68-46c6-96f1-ab151c120714","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5eb52f08-2d1a-4362-9ffc-4871bdc10f3f","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"341dd607-b97d-4d70-bde2-53acda6b6c95","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"05349273-414f-4208-9ea2-c4fc8f4ea2cb","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"86089f76-b778-4d52-821e-6f27de3df613","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b81e4fd6-1fc5-43a0-a258-b6e16a5cbec6","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8aa8ce31-43e4-438e-951f-241608435260","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"37343f41-6ec2-4c7e-b21d-2cc18d5ce1e0","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5915e52f-64f8-4146-b8bd-81bead6324a3","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"836ba889-af9e-460d-a4cc-c24d922795f2","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4ae14b06-bb68-4394-a210-a46b8f028346","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d8f42066-e9dc-4411-bdcf-43b1a203370c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"7ef86f16-b1a0-49f7-9592-612b9be02b25","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b29c7775-a9a4-451e-a1b5-01d19ed5ca5e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"e56f3989-8f81-46af-90fa-a4813eeb976f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"2526c963-f170-45a8-923e-91b0712a9810","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"24575cf0-501a-44f9-8426-c40f8f4b5552","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1a6f8d7f-acd6-42be-8c4f-f464c6218381","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"82b176fd-5cab-498c-909e-8fa7d29c38d8","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1708b3a9-4f37-44a8-8f0e-f9a2d2e5d940","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"c9ac246e-29fb-4bc4-8231-8439795bb590","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"94f2a5c3-2539-436a-af75-23fbbd1a3957","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d99594f4-2d40-4df4-9419-ba2ca6aa3f7f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"fd66e72d-0f10-4f57-9807-6db26290ab2e","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8f344863-503d-4bc3-a594-3815e7d55f5c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"9be82601-de9d-4c18-948a-23ab6f4dd431","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"6495f3c0-b463-47e0-b08a-ca949672211e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b4bede08-3f08-4839-ba4a-abc7ac195bde","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4afcdfc0-3ff5-4f2b-a223-f8fc042a5bbe","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"422062c4-fa40-4771-a86f-008efe6d86e5","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d87e8013-86a7-4840-8d25-6f62e14eb4ac","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a6acedfd-4043-4c64-a5d1-aec3326df9e7","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"dbdc0b24-06c4-48b9-8d6c-7455119dc773","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a3856849-954a-4cfc-96a6-382e530d3638","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4a39306b-ffa5-433d-80a3-28e41f929b72","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]}]
        
(Showing Draft Content)

使用单元格

SpreadJS 允许您在单元格上执行各种操作,如下所述:

获取和设置单元格值


此示例获取并设置单元格值。

$(document).ready(function ()
{
    var spread =
    new GC.Spread.Sheets.Workbook(document.getElementById("ss"),{sheetCount:3});
    var sheet = spread.getActiveSheet();
    sheet.getCell(0, 0).formatter("0.00_);(0.00)");
    sheet.getCell(1, 0).formatter("0.00_);(0.00)");
    sheet.getCell(0, 1).formatter("0.00_);(0.00)");
    sheet.getCell(1, 1).formatter("0.00_);(0.00)");
   
    // 设置文本属性的值
    sheet.getCell(0, 0).text("10");
   
    //通过调用SetText方法设置值
    sheet.setText(1, 0, "10");
   
    //使用Value属性设置值
    sheet.getCell(0, 1).value(10);
   
    //通过调用SetValue方法设置值。
    sheet.setValue(1, 1, 10);

    //获取单元格值
    $("#button1").click(function()
     {
       alert("Obtaining cell values by referring to Text property: " +
       sheet.getCell(0, 0).text() + "\n" +
       "Obtaining cell values by calling GetText method: " + sheet.getText(1, 0) + "\n" +
       "Obtaining cell values by referring to Value property: " +
       sheet.getCell(0, 1).value() + "\n" +
       "Obtaining cell values by calling GetValue method: " + sheet.getValue(1, 1));
     });

});

获取和设置单元格的默认值(v16.2)

当单元格中没有值时,在 SpreadJS 中它显示为空白。为了管理这种情况,SpreadJS 允许您设置单元格的默认值。您可以将任何值或公式设置为单元格的默认值。设置单元格的默认值与设置单元格公式的方式相同。默认值只能针对工作表中的视口区域进行设置,但无法为整行或整列设置默认值。

请注意,当单元格为空并且已经设置了默认值时,以下条件适用:

  • 只有该单元格的默认值或默认公式将包括在重新计算中。

  • 在编辑模式下,单元格显示默认值或使用默认公式重新计算的值。

  • 导出到Excel时,默认值将导出为单元格值。

  • 用户可以更改单元格值,但不会覆盖默认值。

您可以使用以下方法设置或获取单元格的默认值。

  • 使用 GC.Spread.Sheets.CellRange 类的defaultValue方法。

    // 使用defaultValue()方法设置或获取单元格的默认值/公式
       activeSheet.getCell(0, 0).defaultValue(20);
       console.log("使用defaultValue方法获取单元格(0,0)的默认值:" + activeSheet.getCell(0, 0).defaultValue());
       activeSheet.getCell(0, 1).defaultValue("=IF(A1>10, A1*3)");
       console.log("使用defaultValue方法获取单元格(0,1)的默认值:" + activeSheet.getCell(0, 1).defaultValue());
  • 使用 GC.Spread.Sheets.Worksheet 类的getDefaultValuesetDefaultValue方法。

    // 使用setDefaultValue或getDefaultValue()方法设置或获取单元格的默认值/公式
       activeSheet.setDefaultValue(0, 0, 20);
       activeSheet.setDefaultValue(1, 0, 40);
       console.log("使用getDefaultValue方法获取单元格(0,0)的默认值:" + activeSheet.getDefaultValue(0, 0));
       activeSheet.setDefaultValue(2, 0, "=SUM(A1,A2)");
       console.log("使用getDefaultValue方法获取单元格(2,0)的默认值:" + activeSheet.getDefaultValue(2, 0));

此外,您还可以使用 CopyToOptions 枚举的defaultValue选项复制单元格的默认值。

// 使用CopyToOption枚举复制单元格的默认值/公式。
   activeSheet.getCell(0, 0).defaultValue("2048");
   activeSheet.copyTo(0, 0, 1, 1, 2, 2, GC.Spread.Sheets.CopyToOptions.defaultValue);
   activeSheet.getCell(0, 1).defaultValue("=A1 + 1000");
   activeSheet.copyTo(0, 1, 1, 1, 2, 2, GC.Spread.Sheets.CopyToOptions.defaultValue);

注意: 当将公式设置为单元格的默认值时,SpreadJS不支持动态数组和数组公式。在此情况下,单元格中只显示左上角的值。

使用设计器

您可以使用 SpreadJS 设计器来设置默认单元格值,方法是在单元格的上下文菜单中选择“Default Value...”选项。这将打开“Default Value Dialog”,您可以在其中设置所选单元格的默认值。设置完默认值后,对话框会显示单元格的默认值。

以下演示了如何使用设计器设置默认单元格值。

SetDefaultCellValue.5e8159

设置活动单元格

此示例设置活动单元格。

$("#button1").click(function ()
{  
     // 将单元格(3,3)设置为活动单元格
     activeSheet.setActiveCell(3, 3);
 });

设置活动单元格的位置



此示例设置活动单元格的位置。

$("#button1").click(function ()
{
   // 将单元格(3,3)设置为活动单元格
   sheet.setActiveCell(3, 3);
    
   // 在左上角显示活动单元格
   sheet.showCell(3, 3,
    GC.Spread.Sheets.VerticalPosition.top,
    GC.Spread.Sheets.HorizontalPosition.left);
});

选择多个单元格



本示例创建了多个选择。

$(document).ready(function ()
{
    var spread =
    new GC.Spread.Sheets.Workbook(document.getElementById("ss"),{sheetCount:3});
    var sheet = spread.getActiveSheet();
   
    // 允许选择多个范围
    sheet.selectionPolicy(GC.Spread.Sheets.SelectionPolicy.MultiRange);
   
    // 创建两个不同的选择范围。
    sheet.addSelection(0, 0, 2, 2);
    sheet.addSelection(3, 3, 2, 2);
});

获取所选单元格范围

此示例获取选择的范围。

window.onload = function()
{
   var spread =
   new GC.Spread.Sheets.Workbook(document.getElementById("ss"),{sheetCount:3});
   var activeSheet = spread.getActiveSheet();
   activeSheet.setRowCount(5);
   activeSheet.setColumnCount(5);
   $("#button1").click(function()
   {       
     // 获取选择范围
     var selectedRanges = spread.getActiveSheet().getSelections();
     for(var i = 0; i < selectedRanges.length; i++)
        {
             console.log("---------------------------");
             console.log("Using Range class");
             console.log("-------------------------");
             console.log("Selected top row index: " + selectedRanges[i].row);
             console.log("Number of selected rows: " + selectedRanges[i].rowCount);
             console.log("Selected first column index: " + selectedRanges[i].col);
             console.log("Number of selected columns: " + selectedRanges[i].colCount);
         }
   });
}

设置单元格的条件格式

这个示例为单元格设置了条件格式样式

window.onload = function()
{
  var spread =
  new GC.Spread.Sheets.Workbook(document.getElementById("ss"),{sheetCount:3});
  var activeSheet = spread.getActiveSheet();

  // 创建各自的条件样式。
  var styleBlue = new GC.Spread.Sheets.Style();
  var stylePink = new GC.Spread.Sheets.Style();
  var styleLime = new GC.Spread.Sheets.Style();
  var styleYellow = new GC.Spread.Sheets.Style();
  var styleEmpty = new GC.Spread.Sheets.Style();
  styleBlue.backColor = "blue";
  styleBlue.foreColor = "white";
  stylePink.backColor = "pink";
  styleLime.backColor = "lime";
  styleYellow.backColor = "yellow";
  styleEmpty.backColor = undefined;
  styleEmpty.foreColor = undefined;

  // 为各自的单元格设置条件格式。
  activeSheet.conditionalFormats.addCellValueRule(
  GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.EqualsTo,
  0, undefined, styleEmpty,
  [new GC.Spread.Sheets.Range(1, 1, 1, 1)]);
  activeSheet.conditionalFormats.addCellValueRule(
  GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.LessThan,
  10,undefined,styleBlue,
  [new GC.Spread.Sheets.Range(1, 1, 1, 1)]);
  activeSheet.conditionalFormats.addCellValueRule(
  GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.Between,
  20,50,stylePink,
  [new GC.Spread.Sheets.Range(2, 1, 1, 1)]);
  activeSheet.conditionalFormats.addCellValueRule(
  GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.Between,
  50,80,styleLime,
  [new GC.Spread.Sheets.Range(3, 1, 1, 1)]);
  activeSheet.conditionalFormats.addCellValueRule(
  GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.GreaterThan,
  80, undefined, styleYellow,
  [new GC.Spread.Sheets.Range(4, 1, 1, 1)]);
  activeSheet.getCell(2, 1).value(25);
  activeSheet.getCell(3, 1).value(77);
  activeSheet.getCell(4, 1).value(88);
}

获取单元格的位置和大小

此示例获取特定单元格的位置和大小。

spread.getActiveSheet().bind(GC.Spread.Sheets.Events.CellClick, function (e, info)
{
     if(info.sheetArea ===GC.Spread.Sheets.SheetArea.viewport)
   {
     console.log("Clicked cell index (" + info.row + "," + info.col + ")");
    
     /* 获取指定的索引位置单元格的坐标信息
        */

     var cellRect = spread.getActiveSheet().getCellRect(info.row, info.col);
     console.log("X coordinate:" + cellRect.x);
     console.log("Y coordinate:" + cellRect.y);
     console.log("Cell width:" + cellRect.width);
     console.log("Cell height:" + cellRect.height);
     }
 });

鼠标点击获得单元格索引

单击单元格时可以获得单元格索引。

var spread = new GC.Spread.Sheets.Workbook($("#ss")[0]);
var activeSheet = spread.getActiveSheet();
$("#ss").click(function (e)
{
    
  /* 鼠标点击普通单元格点获取单元格索引
    既不是固定的行/列,也不是行/列标题。*/

     var offset = $("#ss").offset();
     var x = e.pageX - offset.left;
     var y = e.pageY - offset.top;
     var target = spread.getActiveSheet().hitTest(x, y);
     if(target &&
     (target.rowViewportIndex === 0 || target.rowViewportIndex === 1) &&
     (target.colViewportIndex === 0 || target.colViewportIndex === 1))
     {
         console.log("Row index of mouse-clicked cells: " + target.row);
         console.log("Column index of mouse-clicked cells: " + target.col);
     }
});