[{"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"}]}]
SpreadJS 会在依赖值(公式中引用的单元格、值或名称)发生变化时自动更新公式。然而,如果你希望控制何时重新计算公式,而不是在引用单元格发生变化后立即进行计算,你可以将自动计算切换为手动计算。SpreadJS API 中的 CalculationMode
枚举允许你设置自动或手动的计算选项。
当 CalculationMode
设置为 auto 时(默认模式),SpreadJS 会在每次引用的单元格发生改变时自动计算所有被修改的单元格。例如,在剪切粘贴或输入单元格时。
需要注意的是,SpreadJS 将修改过的单元格视为脏单元格,并且需要进行重新计算。
另一方面,当 CalculationMode
设置为手动(manual) 时,SpreadJS 仅在你明确请求时计算和更新公式。
这在你有一个包含许多复杂公式的大型工作表中,并且希望避免频繁的重新计算以提高性能和响应速度时非常有用。例如,在剪切或粘贴值时,SpreadJS会重置公式和单元格的值,但不会重新计算任何公式。
注意:SpreadJS 在导出到 Excel 时支持
CalculationMode
选项。
以下代码示例展示了如何在初始化工作簿时设置手动计算选项。
// Switch to manual mode when initializing the workbook.
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {calculationMode: GC.Spread.Sheets.CalculationMode.manual});
// Or
spread.options.calculationMode = GC.Spread.Sheets.CalculationMode.manual;
一旦切换到手动计算模式,你需要使用calculate()
方法手动更新工作表中的公式,该方法接受CalculationType
枚举值作为参数。
CalculationType
枚举指定计算类型,其可用成员如下所示:
成员 | 描述 |
---|---|
| 将范围内的所有单元格标记为需要进行计算。 这是默认的计算类型。 |
| 重建范围内的所有公式,并将它们标记为需要进行计算。 |
| 将公式标记为需要进行计算,但不会标记易变单元格和循环引用单元格。 |
| 将易变单元格和循环引用单元格标记为需要进行计算。 |
请查看下表,了解当使用自动和手动计算模式时,SpreadJS在不同的公式及其依赖关系上的行为。
使用的公式 | 自动计算(默认模式) | 手动计算 |
---|---|---|
| 禁用计算引擎,不对任何单元格进行重新计算。 | |
| 启用计算引擎,并计算易变单元格、循环引用单元格和脏单元格。 | 启用计算引擎,标记易变单元格为脏单元格,但不计算脏单元格。 |
| 启用计算引擎,并计算所有单元格。 | 启用计算引擎,将所有单元格标记为脏单元格,但不计算脏单元格。 |
和
| 计算公式及其依赖关系。 | 计算公式。 |
和
| 计算所有打开的工作表中的所有公式。 | |
和
| 计算所有被标记为脏单元格的单元格,即依赖于易变或更改的数据的单元格,以及通过编程方式标记为脏单元格的单元格。 | |
和
| 重建工作表中的所有公式,然后计算它们。 | |
和
| 将Sheet1中的所有公式标记为脏单元格,并标记范围之外的依赖关系为脏单元格。 计算所有脏单元格。 | 将Sheet1中的所有公式标记为脏单元格,并标记范围之外的依赖关系为脏单元格。 计算Sheet1中的脏单元格。 保持其他单元格的脏状态。 |
注意:从SpreadJS v16.2版本开始,已弃用了
sheet.recalcAll()
方法来重新计算单元格中的公式。你可以使用spread.calculate()
来替代。
以上是在SpreadJS中使用不同计算模式和类型进行各种计算的示例代码。
spread.sheets[0].setFormula(0,0,"RAND()"); // Sheet1!A1
spread.sheets[0].setFormula(1,0,"=Sheet2!A1"); // Sheet1!A2
spread.sheets[0].setFormula(2,0,"=1+2"); // Sheet1!A3
spread.sheets[1].setFormula(0,0,"RAND()"); // Sheet2!A1
spread.sheets[1].setFormula(1,0,"=Sheet1!A1"); // Sheet2!A2
// 重新计算所有单元格。
spread.calculate();
spread.calculate(GC.Spread.Sheets.CalculationType.regular);
// Sheet1!A1和Sheet2!A2将被计算为新的数字,并计算Sheet1!A2和Sheet1!A3。
spread.calculate(GC.Spread.Sheets.CalculationType.all, "Sheet1");
// Sheet1!A1和Sheet2!A2将被计算为新的数字。
spread.calculate(GC.Spread.Sheets.CalculationType.regular, "Sheet1!A1");
// 没有单元格会被计算。
spread.calculate(GC.Spread.Sheets.CalculationType.regular, "Sheet1!A2");
// Sheet1!A1将被计算为新的数字,Sheet1!A2和Sheet1!A3将被计算但不会发生变化,Sheet2!A2在手动模式下保持脏状态。
spread.calculate(GC.Spread.Sheets.CalculationType.all, "Sheet1");
// Sheet1!A1将被计算为新的数字,Sheet2!A2在手动模式下保持脏状态。
spread.calculate(GC.Spread.Sheets.CalculationType.regular, "Sheet1");
// Sheet2!A2将被计算,因为它是脏的。
spread.calculate(GC.Spread.Sheets.CalculationType.minimal);
您可以使用 SpreadJS 设计器设置所需的计算选项。在 公式 > 计算 选项卡组中,选择 "计算选项" 按钮。
在手动计算模式下,您需要选择要重新计算的工作簿部分。
单击 "Calculate Now" 按钮以更新所有打开的工作表中的更改。
如果只想在同一工作表中反映修改,请单击 "Calculate Sheet" 按钮。
如果您只想在同一工作表中反映修改,请单击“Calculate Sheet”按钮。